D
Deno

help

Are pointers supported in Fast API return types?

NNeTT10/15/2022
If no, how do I return pointers?
UUUnknown User10/15/2022
2 Messages Not Public
Sign In & Join Server To View
NNeTT10/15/2022
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?
UUUnknown User10/15/2022
2 Messages Not Public
Sign In & Join Server To View
NNeTT10/15/2022
#[no_mangle]
pub unsafe extern "C" fn no_op() -> isize {
-97
}
#[no_mangle]
pub unsafe extern "C" fn no_op() -> isize {
-97
}
With
no_op: {
parameters: [],
result: "i64",
},
no_op: {
parameters: [],
result: "i64",
},
Takes 26.3ns on average. While
no_op: {
parameters: [],
result: "pointer",
},
no_op: {
parameters: [],
result: "pointer",
},
Takes 16-17ns on average.
UUUnknown User10/15/2022
3 Messages Not Public
Sign In & Join Server To View
NNeTT10/15/2022
no_op_i64 26.49 ns/iter (24.97 ns … 70.14 ns) 25.66 ns 43.22 ns 48.23 ns

no_op_ptr 17.58 ns/iter (16.54 ns … 45.23 ns) 17.03 ns 27.49 ns 28.51 ns
no_op_i64 26.49 ns/iter (24.97 ns … 70.14 ns) 25.66 ns 43.22 ns 48.23 ns

no_op_ptr 17.58 ns/iter (16.54 ns … 45.23 ns) 17.03 ns 27.49 ns 28.51 ns
copypasted from deno bench returning 97 instead of -97 is twice as fast btw
UUUnknown User10/15/2022
Message Not Public
Sign In & Join Server To View
NNeTT10/15/2022
no idea an ACTUAL no-op takes like 4ns
NNeTT10/15/2022
NNeTT10/15/2022
with
#[no_mangle]
pub unsafe extern "C" fn no_op() {
}
#[no_mangle]
pub unsafe extern "C" fn no_op() {
}
UUUnknown User10/15/2022
Message Not Public
Sign In & Join Server To View
NNeTT10/15/2022
and returntype void when defining symbols
UUUnknown User10/15/2022
2 Messages Not Public
Sign In & Join Server To View
NNeTT10/15/2022
Ig this is actually going through fast api considering my older no-op calls (from a few weeks ago) were taking around 40ns
AAapoAlas10/15/2022
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.
NNeTT10/15/2022
so uhh can I make an FFI call explicitly use the slow API? for comparison
AAapoAlas10/15/2022
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.
NNeTT10/15/2022
yea thanks that works can see a 10x difference between the fast call and the slow call
AAapoAlas10/15/2022
You can also force a deopt to slow by using eg. null or Uint32Array for a buffer type, or null for pointer etc.
NNeTT10/15/2022
NNeTT10/15/2022
(that 3.61ns was a fluke) or wait
NNeTT10/15/2022
NNeTT10/15/2022
becomes slower when called after a dummy bench?
AAapoAlas10/15/2022
Bench has some issues with the very first bench call.
NNeTT10/15/2022
btw, could something here cause the call to use the ops layer instead of the fast api?
call_fn: {
parameters: ["buffer", "buffer", "u32", "u32"],
result: "pointer",
},
call_fn: {
parameters: ["buffer", "buffer", "u32", "u32"],
result: "pointer",
},
AAapoAlas10/15/2022
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.)
NNeTT10/15/2022
theyre both technically Uint8Arrays
const x_for_ffi = new Uint8Array(x.buffer);
const y_for_ffi = new Uint8Array(y.buffer);
const x_for_ffi = new Uint8Array(x.buffer);
const y_for_ffi = new Uint8Array(y.buffer);
these two variables are the first two params
AAapoAlas10/15/2022
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.
NNeTT10/15/2022
actually now that I see it, same params but I removed the function body from the rust function
NNeTT10/15/2022
without removing function body from rust function
NNeTT10/15/2022
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:
while i < n {
mean_x += x[i];
mean_y += y[i];
i += 1;
}
while i < n {
mean_x += x[i];
mean_y += y[i];
i += 1;
}
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

Looking for more? Join the community!

Recommended Posts
Reddit fetch Vs. My noob brain part. IHello! So I want to fetch Reddit posts. I use this library. I have a few questions. https://github.cHow do I detect mouse clicks and key presses with Deno.stdin?I don't even really have any base code, so I was just wondering as to how do I do the problem specifML with FFISuppose you wish to do some machine learning stuff with Deno, would it be optimal to use FFI with C Is there a way to determine what modules have been dynamically imported at runtime?The builtin tools to compile/bundle/vendor don't account for dynamic imports (understandably), but iweird interfacehow can a class implement this interface ```ts interface I { (): void type: string }``` or a `cnpm specifier import typesso when I do something like ```ts import * as rx from 'npm:rxjs' console.log(rx) ``` I do have the error Unsupported scheme node for module `nodemodule`. Supported schemesI'm trying to import a module using: ```js import { example } from "npm:example"; ``` Inside `examplesbuild_deno_loader importing Deno modulesA dependancy I use imports modules like "fs" and I want esbuild_deno_loader to treat them as externaIs there a good dataframe library for deno?I use the nodejs bindings for pola.rs for now but I'd like to move from nodejs to deno. This is my ltype a whole directory to make sure each file in the directory exports a default module of a type.Can you type a whole directory to make sure that every file in the directory is exporting a default help mehow do i get the contents of the html, i tried this, but its not loggingextract generic type of generic classhow to get typeof generic type in generic class ```ts class GenericClass<T> {} ``` i want to extractGithub Repository File Tree Generator based on Deno?Is there some project file tree generator written in TS / Deno?How to import the Image class to Deno Fresh?I'm trying to access an image's `naturalHeight` and `naturalWidth`. But when I construct the objectsub sub domain on deno.devIs it possible to create a sub sub domain with deno.dev, example, invite.my-bot.deno.dev, support.myhow to cli run node modules without --compatSo `deno run --compat` has now been removed in favor of `npm:` specifiers https://deno.com/blog/v1.2Intellisense on an import not working, works for friendI cannot for the life of me get `import * as bsdeno from "https://raw.githubusercontent.com/KivalEvaHow to create a broadcast system with streams and oak?i'm trying to create a simple "web-radio" to learn more about streams with deno and oak. i'm stuck oDeno.serveTls with self signed certificatei try to create a webserver but i cannot use self signed certificates, how can i solve this ? ```jReadableStream to JSONi cant seem to get a JSON out of the request.body i attached pictures for the front end JS request c