Stream optimization suggestions
paul.sandoz at oracle.com
Mon Jan 5 14:57:29 UTC 2015
On Dec 19, 2014, at 10:19 PM, Dan Smith <daniel.smith at oracle.com> wrote:
> I've submitted a couple of RFEs to support some simple optimizations on Streams. I noticed both issues while writing some code that forced me to choose between cluttering up my straightforward, uniform Stream logic and eating the cost of linear operations that should be O(1).
> JDK-8067969 Optimize Stream.count for SIZED Streams
That is a reasonable one and i left a comment.
> JDK-8067971 Support optimizations for always-true and always-false Predicates
Less convinced about this one.
For streams this is a micro-optimization dependent on the identity of a Predicate instance or a method on Predicate declaring it's "trueness" for all values passed to the test method. It's rather fragile and implementation dependent. There are other ways to reorganize the stream pipeline to avoid a filter operation if in certain cases it is not necessary.
Given that the expression x -> true is rather concise it is arguable that specific static methods do not carry their weight. Two counter arguments do add some weight:
- explicit naming e.g. Predicate.alwaysTrue. It may be easier to read and harder to get wrong.
- optimization. A static method can return a singleton instance. Currently the lambda meta factory does not optimise for such cases but i hope this is possible in the future.
Stil I don't think these really tip the balance in favour of adding them to the JDK.
> Certainly not high-priority, but would be nice for somebody working with java.util.stream to address someday.
More information about the core-libs-dev