Avoid allocations in IterableUtils.frequency and countMatches#698
Open
nishantmehta wants to merge 2 commits into
Open
Avoid allocations in IterableUtils.frequency and countMatches#698nishantmehta wants to merge 2 commits into
nishantmehta wants to merge 2 commits into
Conversation
frequency() handled non-Set, non-Bag iterables by building a filtered-
iterable pipeline:
size(filteredIterable(emptyIfNull(iterable), EqualPredicate.equalPredicate(obj)))
which allocates an EqualPredicate, a FluentIterable decorator and a filtered
iterator on every call just to count matching elements. Since this also backs
CollectionUtils.cardinality(), the cost is paid by a commonly used utility.
Count directly with a single pass instead. EqualPredicate.equalPredicate(obj)
evaluates Objects.equals(obj, element) (and equalPredicate(null) matches null
elements), so the loop reproduces the previous semantics exactly, including
null handling. The direct loop also lets the JIT scalar-replace the iterator.
Measured with a ThreadMXBean allocation driver (200k warmed ops):
CollectionUtils.cardinality 52 B/op -> 0 B/op. CollectionUtilsTest (161) and
IterableUtilsTest (42) pass unchanged.
Signed-off-by: Nishant Mehta <nishantmehta.n@gmail.com>
countMatches() built a filtered-iterable pipeline just to count elements:
size(filteredIterable(emptyIfNull(input), predicate))
allocating a FluentIterable decorator and a filtered iterator on every call.
Count directly in a single pass instead. FilterIterator advances using
predicate.test(element), so calling predicate.test in the loop reproduces the
previous matching semantics, and emptyIfNull's null handling becomes an
explicit null check.
Measured with a ThreadMXBean allocation driver (200k warmed ops):
IterableUtils.countMatches 66 B/op -> 0 B/op. CollectionUtilsTest (161) and
IterableUtilsTest (42) pass unchanged.
Signed-off-by: Nishant Mehta <nishantmehta.n@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
IterableUtils.frequencyandIterableUtils.countMatcheseach built a lazy filtered-iterable pipeline just to count elements:That allocates a
FluentIterabledecorator and a filtered iterator (plus anEqualPredicateforfrequency) on every call. SincefrequencybacksCollectionUtils.cardinalityandcountMatchesis a commonly used utility, the cost is paid on hot paths.This counts directly in a single pass. The matching semantics are preserved exactly:
EqualPredicate.equalPredicate(obj)evaluatesObjects.equals(obj, element), andequalPredicate(null)matchesnullelements — soObjects.equals(obj, element)reproduces it including null handling.FilterIteratoradvances usingpredicate.test(element), so the loop callspredicate.testfor the same effect.emptyIfNull's null handling becomes an explicit null check.The direct loop also lets the JIT scalar-replace the iterator.
Benchmark
Measured with a
ThreadMXBeanallocation driver (200k warmed ops):Testing
CollectionUtilsTest(161) andIterableUtilsTest(42) pass unchanged.