Due to unoptimized algorithm (as also discussed in #12), encode is a memory hog (I have not looked at decode yet).
I decided to post this as a separate issue, since the other issue's title does not capture the problem, and the discussion mostly focusses on execution speed, not on memory issues.
In my case, I am sending data with socket.io, and this is my journey:
- I am sending about 100M values (nested in one object)
- Initially it crashed on me because during
encode it ran out of memory. I had to increase node's RAM limit to --max-old-space-size=8192.
- The final buffer size is
298,406,623
- It turns out that the recursive
_encode call itself required 4GB of additional memory (even though, as mentioned above, buffer size is less than 300MB total).
- It went from 1.2GB in the beginning to 5.2GB in the end. Afterwards, all mem pressure disappeared again. I'm rather confident, the problem is in the
encode algorithm itself.
- NOTE: I measured this via
process.memoryUsage(). All three (rss, heapTotal, and heapUsed) show the same trend.
Possible Solution
I strongly suggest to heed manast's suggestion to use a direct buffer allocation approach. In case that buffer size is unknown, just run the algorithm once to compute buffer size and index positions, then re-run to actually populate, rather than using the current approach of creating temporary utility objects. This should come at a much lower memory (and probably CPU) cost, than the current version.
I know the owner currently does not have time to work on this, but one can dream :)
Due to unoptimized algorithm (as also discussed in #12),
encodeis a memory hog (I have not looked atdecodeyet).I decided to post this as a separate issue, since the other issue's title does not capture the problem, and the discussion mostly focusses on execution speed, not on memory issues.
In my case, I am sending data with
socket.io, and this is my journey:encodeit ran out of memory. I had to increase node's RAM limit to--max-old-space-size=8192.298,406,623_encodecall itself required 4GB of additional memory (even though, as mentioned above, buffersizeis less than 300MB total).encodealgorithm itself.process.memoryUsage(). All three (rss,heapTotal, andheapUsed) show the same trend.Possible Solution
I strongly suggest to heed manast's suggestion to use a direct buffer allocation approach. In case that buffer size is unknown, just run the algorithm once to compute buffer size and index positions, then re-run to actually populate, rather than using the current approach of creating temporary utility objects. This should come at a much lower memory (and probably CPU) cost, than the current version.
I know the owner currently does not have time to work on this, but one can dream :)