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

feat(ext/ffi): Make External pointers keep reference to V8 buffer #17955

Merged

Conversation

aapoalas
Copy link
Collaborator

Previously when pointers were simply numbers, there was no way for a pointer number created from a buffer to keep the original buffer alive. This risked Deno.UnsafePointer.of() created pointers becoming dangling if the user did not manually make sure to save the original buffer somewhere.

I have personally run into this previously, to disastrous (segfaulty) results. Based on this I felt the need to explicitly warn of this issue on the Denonomicon: https://denonomicon.deno.dev/types/pointers#dangling-pointers.

Now that pointers are objects themselves, we can get rid of this issue by using a WeakMap to hold the of() buffer parameter with the pointer object acting as the key. This ensures that the buffer stays alive as long as the pointer object does, while holding a weak reference to the pointer object itself to avoid a memory leak.

@aapoalas aapoalas requested a review from littledivy February 26, 2023 09:41
@aapoalas aapoalas force-pushed the feat/ext-ffi/pointer-avoid-dangling branch from 9e5544f to 70ed20b Compare February 26, 2023 11:39
@littledivy
Copy link
Member

@aapoalas What is the performance impact?

@aapoalas aapoalas closed this Mar 5, 2023
@aapoalas aapoalas reopened this Mar 5, 2023
@aapoalas
Copy link
Collaborator Author

aapoalas commented Mar 5, 2023

From about 100ns to 300ns, IIRC. But this is not a particularly hot code path generally.

Copy link
Member

@littledivy littledivy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! IMO Fair perf tradeoff since this is rarely used in a hot path.

@aapoalas aapoalas enabled auto-merge (squash) March 5, 2023 07:46
@aapoalas aapoalas merged commit da201d9 into denoland:main Mar 5, 2023
@aapoalas aapoalas deleted the feat/ext-ffi/pointer-avoid-dangling branch March 5, 2023 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants