more-executors icon indicating copy to clipboard operation
more-executors copied to clipboard

Collapse f_and/f_or for better performance?

Open rohanpm opened this issue 3 years ago • 2 comments

Consider the following:

f = f_and(f1, f2)
f = f_and(f, something)
f = f_and(f, other)
f = f_and(f, foo)
f = f_and(f, bar)
# ...

That will create various chained objects (AndOperation under the hood). In practice code might be written in a way such that the chain ends up thousands of items deep.

Consider if it's practical & useful to collapse the objects in this case. e.g. is it possible to arrange it so that, in f_and(a, f_and(b, c)), the outermost f_and could generate an object equally as efficient as f_and(a, b, c) ?

rohanpm avatar Feb 25 '22 04:02 rohanpm

I looked into it and don't think there's much that can be done.

  • In the case where futures have already completed by the time f_and is called, 100,000 chained calls already seem to be about as efficient as a single call with 100,000 futures.
  • In the case where futures haven't completed, there doesn't seem to be any reasonable way to avoid needing O(N) frames on the stack where N is the number of futures.

rohanpm avatar Feb 27 '22 00:02 rohanpm

Reopened after changing my mind about it. I think it should be possible to do something about this after all.

rohanpm avatar Mar 08 '22 06:03 rohanpm