Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: possible mem leak in v8.serialize() #86

Closed
ppedziwiatr opened this issue Jan 19, 2022 · 8 comments
Closed

perf: possible mem leak in v8.serialize() #86

ppedziwiatr opened this issue Jan 19, 2022 · 8 comments
Assignees

Comments

@ppedziwiatr
Copy link
Contributor

ppedziwiatr commented Jan 19, 2022

Issue raised by Pianity.
In some environments the rss memory usage grows to very large values (it does not happen on my M1 though).
This is probably related to nodejs/node#40828

Removing the v8's serialization (https://github.com/redstone-finance/redstone-smartcontracts/pull/84/files#diff-5acab41990585fa68ae01ff7b2e2915628d19237840c3061b31fcadc1c0f09b8) fixed the memory leak issues, but at a cost of performance - therefore a better solution needs to be developed.

@ppedziwiatr
Copy link
Contributor Author

switch to https://developer.mozilla.org/en-US/docs/Web/API/structuredClone when it will be implemented by most of the browsers.

@balthazar
Copy link
Contributor

👍🏻 Subscribing

@ppedziwiatr
Copy link
Contributor Author

ppedziwiatr commented Mar 5, 2022

Performance comparison ("state evaluation" column) for "loot" contract:

  1. @ungap/structured-clone

image

  1. v8.deserialize(v8.serialize(input))

image

  1. lodash/cloneDeep

image

  1. JSON.parse(JSON.stringify(input, mapReplacer), mapReviver);

image

@balthazar
Copy link
Contributor

Maybe I'm not reading the bench correctly, but isn't lodash more performant than ungap here?

@ppedziwiatr
Copy link
Contributor Author

ppedziwiatr commented Mar 5, 2022

Maybe I'm not reading the bench correctly, but isn't lodash more performant than ungap here?

You're reading the bench correctly ;-) lodash indeed is more performant than ungap and v8 in this case. Though I believe that for "bigger" states - v8 was the winner. Anyway, I'll stick with lodash for now...

@ppedziwiatr
Copy link
Contributor Author

Just a word of comment if anyone was interested - deepCopy-ing of the state is required for each interaction - before the state is passed to the contract's handler function. Contracts usually modify the state directly and without deepCopy - there would be risk of changing values somewhere in the host code (eg. values stored in cache).

v1 performed here simple stringify/parse https://github.com/ArweaveTeam/SmartWeave/blame/master/src/contract-step.ts#L56 - which obv. is least performant.

This won't be an issue in case of WASM contract - which store their state in the wasm module memory.

@ppedziwiatr
Copy link
Contributor Author

fixed in #108, released in 0.4.36

@balthazar
Copy link
Contributor

Ah ok, I was confused with the commits being referenced using ungap, but they were probably old

🎉 Will try this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants