4 ms·
Some implementations use reference counting. So in your example, for + the output requires as much memory as the inputs, and if at least b or c have a ref count
by i_don_t_know 7y ago
Some implementations use reference counting. So in your example, for + the output requires as much memory as the inputs, and if at least b or c have a ref count of 1, then you can reuse one of the inputs for the output. Otherwise you have to allocate memory to hold the sum of b and c. But that intermediate sum has a ref count of 1 and can be reused for the final output.
The key is to code your primitives in such a way that you can reuse one of the inputs as the output.
You can also do clever tricks that describe the array transformations rather than perform them. It’s similar to what happens during stream fusion in Haskell. See https://news.ycombinator.com/item?id=17506789 https://news.ycombinator.com/item?id=17506789 for more details.
- beagle3 7y agoIIRC numexpr got about x2-x5 faster when they switched from numpy's implementation (which naively mostly works as you describe, one op at a time, refcounted, though no good reuse) to a VM that reuses temporary result memory and works in 8KB pages (thus getting way, way better cache use). But I'm not familiar with an APL/J/K implementation that does.
- i_don_t_know 7y agoI didn’t know about numexpr. That looks really interesting. Thanks.