spacebudz / lucid

Lucid is a library, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript, Deno and Node.js.
https://lucid.spacebudz.io
MIT License
339 stars 139 forks source link

Free unused memory in Data.from #232

Closed yHSJ closed 11 months ago

yHSJ commented 11 months ago

At JPG Store, we have been dealing with memory leaks in our code that uses either Lucid or the CML. After some research, we discovered that the CML does a lot of clones to avoid issues with freed memory across the WASM boundary ( https://github.com/dcSpark/cardano-multiplatform-lib/issues/142#issuecomment-1281810732).

Any object that is returned from WASM to JavaScript seems to never get garbage collected. Thus, all objects returned from wasm to the js runtime must be freed, including intermediary values when chaining function calls together.

This PR specifically only fixes the memory usage in the Data.from function to resolve the following issue: https://github.com/spacebudz/lucid/issues/222

I will continue to fix the memory management over time, as I have free time. However, I would argue it is best to merge these smaller pieces whenever they are done, as they will reduce the memory footprint of several applications relying on Lucid.

yHSJ commented 11 months ago

We might also want to add a comment to the freeable class cause other people might not have the context we have due to our internal discussions

Good point, I went ahead and added some comments to the freeables.ts file that should provide context.

yHSJ commented 11 months ago

Closing in favor of https://github.com/spacebudz/lucid/pull/234 which includes managing WASM memory everywhere it's used.