31 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Nevermind I realized that I can just cast the pointer to an integer and make it work
From what I've seen, it was easier to pass the typed array as a parameter and let the native code do the allocation directly into the typed array
now I have a new question. How do I know my FFI call is utilising Fast API?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
With
Takes
26.3ns
on average.
While
Takes 16-17ns
on average.Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
copypasted from
deno bench
returning 97
instead of -97
is twice as fast btwUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
no idea
an ACTUAL no-op takes like
4ns
with
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
and returntype
void
when defining symbolsUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ig this is actually going through fast api considering my older no-op calls (from a few weeks ago) were taking around
40ns
Pointers are supported as return types, as are 64 bit integers. Under the hood it works so that an extra buffer argument is passed into the call, our trampoline writes the pointer / integer into the buffer, and then on JS side we check (for positive integers) if the number has no upper bits in which case we return the lower 32 bits as a number. Otherwise, we take a BigInt out of the buffer and check if it can be safely represented as a number. If it can, we return a number. Else, the BigInt.
so uhh
can I make an FFI call explicitly use the slow API?
for comparison
Yes, mark it with
callback: true
. Fast API will throw if you try to call into JS so we need a way to mark functions that may call back and use slow calls for those.yea thanks that works
can see a 10x difference between the fast call and the slow call
You can also force a deopt to slow by using eg. null or Uint32Array for a buffer type, or null for pointer etc.
(that 3.61ns was a fluke)
or wait
becomes slower when called after a dummy bench?
Bench has some issues with the very first bench call.
btw, could something here cause the call to use the ops layer instead of the fast api?
If either of the first two parameters is not a Uint8Array then it'll deopt to the ops layer.
(Well, not quite ops layer but similar.)
theyre both technically Uint8Arrays
these two variables are the first two params
Okay, that should be fine. Though do note that it's been seen elsewhere that doing
new Uint8Array(buffer)
can be surprisingly slow. If you can move it up out of the loop / bench it will probably help a lot.actually now that I see it,
same params but I removed the function body from the rust function
without removing function body from rust function
okay seems some loops were taking too much time
I was iterating through a slice of 10k numbers 4 times so it took 40 microseconds for each of them. Like this:
so in the end, the call was actually going through fast API but the actual program ended up being slow
Works fine now
my dumbass didn't set
opt-level
when compiling with rustc