Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert stateful predicate distinctByKey into a list method #5462

Conversation

leonardehrenfried
Copy link
Member

Summary

In a previous PR I used a stateful predicate to de-duplicate a stream by a key of the list elements. @vpaturet has correctly flagged this as problematic.

This PR converts so that it operates on List rather than Stream because an intermediate collection was required anyway.

Unit tests

Moved.

@codecov
Copy link

codecov bot commented Oct 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ea1edcd) 66.94% compared to head (da96ed6) 66.80%.
Report is 46 commits behind head on dev-2.x.

Additional details and impacted files
@@              Coverage Diff              @@
##             dev-2.x    #5462      +/-   ##
=============================================
- Coverage      66.94%   66.80%   -0.14%     
- Complexity     15546    15560      +14     
=============================================
  Files           1802     1805       +3     
  Lines          69917    69839      -78     
  Branches        7360     7358       -2     
=============================================
- Hits           46805    46659     -146     
- Misses         20650    20723      +73     
+ Partials        2462     2457       -5     
Files Coverage Δ
.../org/opentripplanner/ext/geocoder/LuceneIndex.java 87.68% <ø> (ø)
...pentripplanner/ext/geocoder/StopClusterMapper.java 96.87% <100.00%> (+0.32%) ⬆️
...pentripplanner/framework/collection/ListUtils.java 90.90% <100.00%> (+40.90%) ⬆️
...routing/graphfinder/StopFinderTraverseVisitor.java 100.00% <100.00%> (ø)

... and 30 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@t2gran
Copy link
Member

t2gran commented Oct 31, 2023

Question: There is a few things in here witch might be slower that the original code - making lists and throwing them away - but I am not sure if it matters. Is the performance important enough, and does this code run once or very often(more GC)?

@leonardehrenfried
Copy link
Member Author

I can reduce the number of collections created but it won't be quite as efficient as it was previously:

  • Use Iterables.concat rather than ListUtils.combine
  • Don't run List.copyOf in the utils class

I don't think it matters a lot either way with the current uses but the library code should probably be as efficient as possible.

vpaturet
vpaturet previously approved these changes Nov 1, 2023
@leonardehrenfried
Copy link
Member Author

There is now a JEP which adds "gatherers" to the Stream API allowing you to implement a distinctBy filter yourself: https://openjdk.org/jeps/461

It's even the example used in the document.

Comment on lines 21 to 29
void distinctByKey() {
var first = new Wrapper(10, makeHello());
var last = new Wrapper(20, "HI");
var duplicates = List.of(first, new Wrapper(20, makeHello()), last);

var deduplicated = ListUtils.distinctByKey(duplicates, w -> w.string);

assertEquals(List.of(first, last), deduplicated);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it slightly difficult to understand what this test does.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to improve the readability of the test and added comments explaining what is being tested.

@leonardehrenfried leonardehrenfried merged commit c2cb4d9 into opentripplanner:dev-2.x Nov 9, 2023
5 checks passed
@leonardehrenfried leonardehrenfried deleted the statefule-predicate branch November 9, 2023 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants