by repsilat · 12y ago · 0 comments
Why can't the
std::vector<int> ints =
read_ints() | cont::sort | cont::unique;
from TFA can't be done "as lazily as possible" (i.e., sort eagerly and uniq lazily). Alternatively, something like std::vector<int> ints(read_ints() | cont::sort | cont::unique);
or even std::vector<int> ints;
read_ints() | cont::sort | cont::unique > ints;
What reason could we have for making `read_ints` and `unique` eager by default?Performance-wise, the lazy filter works in constant space, will result in fewer dcache misses if the data set is large, and (I think) provides more opportunity for the optimizer to do clever things. The eager chain may have better icache performance if the filter is very complicated.
And it's easy to go from lazy to eager -- just put everything in a vector. If you want to go from eager to lazy, though, you're out of luck.
I agree with your side of the trade-off, a performance bug (result: slow application) is always preferable to a memory bug (result: failed malloc, likely crash) in production.
To make things more desirable, you could always do something even simpler than your example:
auto ints = read_ints() | cont::sort | cont::unique | cont:to_vector;
That way fixing the performance bug would only need a one-liner.Still, having the option is better than nothing.
Looking at your code:
std::vector<int> ints;
read_ints() | cont::sort | cont::unique > ints;
If `cont::unique` is lazy (non-owning), what is the lifetime of the elements allocated by `cont::sort` (or moved from `read_ints()`)? If the code had been written instead like below, who is responsible for keeping the elements alive? std::vector<int> ints;
auto tmp = read_ints() | cont::sort | cont::unique;
tmp > ints;
There's a whole host of issues here that you're not dealing with.