Friday, 9 August 2013

Can Iterable.filter() skip processing with "constant" (including short circuited) Predicates?

Can Iterable.filter() skip processing with "constant" (including short
circuited) Predicates?

I just looked at the AndPredicate and the ALWAYS_FALSE predicates and
didn't find any indication that an AndPredicate noticed an ALWAYS_FALSE
component to short circuit itself.
It seems like there may be a number of optimization opportunities if a
Predicate is known to be ALWAYS_TRUE or ALWAYS_FALSE. For example, could
Iterables.filter(Iterable,Predicate) just return the input
Iterable(perhaps an unmodifiable view of it) with an ALWAYS_TRUE
Predicate, and an empty Iterable with an ALWAYS_FALSE Predicate?
Similarly an AndPredicate which contains an ALWAYS_FALSE could be
considered an ALWAYS_FALSE for this type of optimization (and OrPredicate
with an ALWAYS_TRUE, etc.)
Is this optimization already in place, and I missed it in my casual
search, or are there reasons (side effect behavior changed?) why this
would not be a good idea?

No comments:

Post a Comment