D-Programming-Language/phobos — pull request #1987 by schuetzm (2014-03-09 15:13:44+00:00)
Title: Initialize `std.algorithm.filter` lazily

Timon Gehr recently complained [1] about the fact that `std.algorithm.filter` initializes itself in its constructor, i.e. potentially performs a lot of computation by calling `popFront()` repeatedly.

With this PR, I reworked `filter` and `filterBidirectional` to initialize themselves only when they are accessed the first time.

I don't know however whether it is actually worth it. Usually, a constructed filter range will be used at least once. This implementation needs to carry around an additional flag, and needs to check it before each operation, which will probably degrade performance slightly. On the other hand, there might be corner cases where initializing upon construction is too expensive, like when you construct hundreds of filters, but use only a handful of them and can't tell which one until you actually need them.

Comments?

[1] http://forum.dlang.org/thread/lfap6l$2eb1$1@digitalmars.com?page=3#post-lfenmi:241lve:241:40digitalmars.com