-
Notifications
You must be signed in to change notification settings - Fork 7
Copy buffers to JS memory and free #55
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
Copy buffers to JS memory and free #55
Conversation
🦋 Changeset detectedLatest commit: d1dae0e The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| const view = secretBufferToBuffer(byteBuffer) | ||
| const result = Buffer.from(view) | ||
| this.nativeAriesAskar.askar_buffer_free(byteBuffer) | ||
|
|
||
| return new Uint8Array(result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does .from copy the buffer? Is the behavior different from new Uint8Array?
How does it differ from:
| const view = secretBufferToBuffer(byteBuffer) | |
| const result = Buffer.from(view) | |
| this.nativeAriesAskar.askar_buffer_free(byteBuffer) | |
| return new Uint8Array(result) | |
| const result = new Uint8Array(secretBufferToBuffer(byteBuffer)) | |
| this.nativeAriesAskar.askar_buffer_free(byteBuffer) | |
| return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you maybe right. I was doing it explicitly to understand that the buffer was getting copied into JS memory, but I should be able to do it in a single line. I'll test it out.
|
This is great @jamshale! How about the remaining memory usage? Is that normal or could there br other leaks? Like the scan? |
Good point. I thought the scan and entry_list operations should be ok because the have |
b5df484 to
7591baa
Compare
|
I think this should be good now. It was a bit challenging to get things lined up right because I am developing against the v0.5.x credo mediator which uses the old @hyperledger/aries-askar-nodejs libraries. I'm curious if there's a way to target those for a release with these changes or get credo v0.5.x to use the new @openwallet ones. For the other memory allocations I think there is still some memory being leaked although the seem very small. One has to do with the These other potential leaks are small and may not need to be fixed and could be tackled in separately if someone wants to try and fix them. |
|
Would it be possible to first create a PR targetting main? Then we at least have the code for the latest release track. I don't think we can backport this easily to 0.2.4 (since it's under a different scope, and would require quite some setup on our side) 🤔 I'll think a bit more about how to release this for 0.5.x. Updating to the new askar packages would be a breaking change for 0.5.x. |
|
I'll rebase this to main. I can probably create a patch for it until we can move the mediator to version 0.6.x |
5c10b88 to
6bf4351
Compare
Signed-off-by: Timo Glastra <timo@animo.id>
Signed-off-by: jamshale <jamiehalebc@gmail.com>
Signed-off-by: jamshale <jamiehalebc@gmail.com>
Signed-off-by: jamshale <jamiehalebc@gmail.com>
5f1c5bb to
ee612b0
Compare
I think we'll need to find a way to get a new Askar release compatible with Credo 0.5.x, since we are very close to the deadline imposed by Android for 16KB page size support. There is a @openwallet-foundation/askar 0.2.5 released from this repo (0.2.4 branch). Does it work? Can we do something similar to that? Of course this alone will not be enough, since we'll need a backport of Askar 0.3.x as well. |
|
Should hear back from @andrewwhitehead soon about a 0.3.3 release of Askar, |
|
Hmm i think it might be easier to just integrate the latest owf askar release into 0.5.x and support both askar interfaces in the 0.5.x stream. There's not a lot / any breaking api changes besides the naming ariesAskar vs askar. By supporting both it won't be a breaking change, but it does allow 0.5.x users to move towards the new askar release |
|
Just as a small note, we are looking into replacing the wrapper to not use a deprecated library and to make it work with the latest node.js. So, I would not spend too much time on adding more and more tests for ffi-napi specific things :). |
That's fair, but we should make sure we fix the memory leaks if we switch over. So it's good to already identify the root issues (and I suspect the migration will probably take quite a while still) |
|
FYI @jamshale, we're working to add the latest askar release to Credo 0.5.x. This means we won't have to backport it, but can directly merge it into the main branch, and then can be used with the next Credo 0.5.x release. |
|
We're looking into the React Native side as well now since it has the same issue, and then we can get this merged |
b544c9c to
ee612b0
Compare
Signed-off-by: Timo Glastra <timo@animo.id>
…ed buffers Signed-off-by: Timo Glastra <timo@animo.id>
Signed-off-by: Timo Glastra <timo@animo.id>
|
Okay updated the code a bit and added React Native support, this should now be ready. Probably good to test it first with the alpha release in React Native / Credo, and then we can make a new stable release |
TODO: More testing of the ffi wrapper and binary. Currently testing via the compiled javascript in my project.
This copies the buffers into JS memory and frees it from rust memory. Otherwise this leads to a memory leak which can become important for heavy traffic scenarios like a mediator server under load.
Here is a snippet from allocation growth without freeing the buffers
And here is roughly the same load with the buffers being freed. This did run for a longer time.
The
SecretBuffer::from_secretno longer show up.Meaning the allocations are not contributing to memory growth. This prevents OOM when node is trying to use a high percentage of it's allocated memory and for this memory to be reclaimed when load stops or memory limits are being reached.