Closed GoogleCodeExporter closed 9 years ago
Actually, I posted the root cause, rather than an actual performance bug in the
library code.
Here's a less artificial example, where creating Filtered Collections exposes
the performance issue in the slow predicate:
______________________________________________
public static void testCollections2Filter() {
ArrayList<Integer> filterCollection=new ArrayList<Integer>();
ArrayList<Integer> toAddCollection=new ArrayList<Integer>();
for(int i=0;i<500000;++i) {
filterCollection.add(new Integer(i));
toAddCollection.add(new Integer(i));
}
// Predicate<Integer> inPred=optimizedIn(filterCollection);
Predicate<Integer> inPred=Predicates.in(filterCollection);
Collection<Integer> filteredCollection=Collections2.filter(new ArrayList<Integer>(),inPred);
filteredCollection.addAll(toAddCollection);
}
_______________
Here are the results I'm getting with the original and optimized versions of
the "in" predicate:
// Size Original Optimized
Size Original Optimized
10 0m1.124s 0m1.096s
100 0m1.112s 0m1.088s
1000 0m1.164s 0m1.152s
10000 0m1.216s 0m1.160s
100000 0m9.421s 0m1.228s
200000 0m50.611s 0m1.252s
500000 > 10 min 0m1.324s
Regards,
Oswaldo.
Original comment by ozzy...@gmail.com
on 21 Oct 2014 at 10:39
There's a couple points here:
- The documentation of Predicates.in specifies, "It does not defensively copy the collection passed in, so future changes to it will alter the behavior of the predicate." That's a non-@Beta method contract that we would almost certainly not change, and there's not really any way to satisfy that contract and do anything like what you're proposing.
- We could document better than the collection's own contains() method is used, and that the performance of the predicate will be equivalent to the performance of the collection's contains() method.
- For very short lists, it is not always the case that converting to a HashSet will help performance.
- It's possible that it might make sense as a warning or an error in error-prone (http://errorprone.info/) to pass a List to Predicates.in, but we'd have to investigate this -- and that'd be more appropriate as a bug filed against error-prone, rather than Guava.
Original comment by lowas...@google.com
on 21 Oct 2014 at 4:23
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<issue id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:08
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:17
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:07
Original issue reported on code.google.com by
ozzy...@gmail.com
on 21 Oct 2014 at 4:27