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(ext/ffi): support Uint8Array in fast calls #15319

Merged
merged 5 commits into from
Jul 27, 2022

Conversation

littledivy
Copy link
Member

Closes #15218

This patch replaces the fast call path for pointer types
from uintptr_t to Uint8Array sequence. There may be
API changes in the future to avoid deopt in number paths.

@aapoalas
Copy link
Collaborator

Suggestion: Lets define a "buffer" type for this use and keep the "pointer" path for numbers.

Bonus points for defining "u8*", "u32*", "i32*", etc. to control which type of TypedArray the fast path expects :)

@@ -32,7 +32,8 @@ fn native_arg_to_c(ty: &NativeType) -> &'static str {
NativeType::I64 => "int64_t",
NativeType::ISize => "intptr_t",
NativeType::USize => "uintptr_t",
NativeType::Pointer | NativeType::Function => "void*",
NativeType::Pointer => "struct FastApiTypedArray*",
NativeType::Function => "void*",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we change this to use the void(*pN)() type while we're here?

Copy link
Member

Choose a reason for hiding this comment

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

Please elaborate why it should be such type

Copy link
Collaborator

Choose a reason for hiding this comment

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

Because there's no guarantee on a language level that a data pointer is valid as a function pointer.

It is so for all the envs that Deno supports, but technically they may not be the same.

Copy link
Member

Choose a reason for hiding this comment

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

@littledivy please take a look

Copy link
Collaborator

Choose a reason for hiding this comment

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

Admittedly this is very rare: Embedded systems using the Harvard architecture have smaller function pointer size. Some others might also be like this but it's really not a big consideration.

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have a const assert for this assumption

@aapoalas
Copy link
Collaborator

Seems like the assembly for handling the buffers isn't too bad at all:

trampoline:
        mov     rdi, qword ptr [rsi + 8]
        jmp     qword ptr [rip + actual@GOTPCREL]

@littledivy littledivy marked this pull request as ready for review July 27, 2022 13:18
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM

@littledivy littledivy merged commit fad8d7d into denoland:main Jul 27, 2022
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.

Support Uint8Array parameters in FFI fast calls
3 participants