lucet icon indicating copy to clipboard operation
lucet copied to clipboard

C API - faster calls

Open gektorbr opened this issue 4 years ago • 3 comments

As I understand lucet_instance_run is the only way of calling a wasm function from the a C "host". It "resolves" the function and makes a call to it. Unfortunately this API is extremely slow (~ 10 milliseconds in my case). Is there a way to resolve a function "in advance" and to make fast calls to it later?

gektorbr avatar Jan 19 '20 23:01 gektorbr

The short answer is that currently, there is no pre-resolved API (even for non-C users of lucet-runtime). Out of curiosity, about how many functions are in the wasm module you're resolving out of? 10 milliseconds is pretty slow, but that sounds about right from that API in the worst circumstances.

Either way, there should be a faster API, but it hasn't been exposed yet.

It sounds like you might have taken a look at Instance::run, which is the right lifecycle, just all in one function.

So to expose a pre-resolved run_func we'd need to:

  • expose ModuleInternal::get_export_func somehow (probably through Instance? Instance::get_func_by_name?)
  • expose Instance::run_func (which now takes a FunctionHandle - a bit more misuse-resistant than it once was)
  • expose both of these through the C API.

If you'd like to put a PR together to do the above, that would be fantastic! I'd be happy to work with you on that. I'm not sure when we'll get to this, otherwise.

iximeow avatar Jan 21 '20 18:01 iximeow

Out of curiosity, about how many functions are in the wasm module you're resolving

Just a couple of functions. The idea is to resolve the functions only once and to call them a lot.

There is lucet_instance_run_func_idx, although there is no API to get function index. Wouldn't it be faster?

gektorbr avatar Jan 21 '20 18:01 gektorbr

lucet_instance_run_func_idx is specifically for functions in wasm tables - the missing API to get those indices is because they're determined by the wasm module, we just ensure the wasm module's indices remain valid through compilation. lucet_instance_run_func_idx won't be able to run functions that aren't in the wasm module's indirection table.

That said, if you can add functions to the wasm table you could use the index you know them to be at for lucet_runtime_run_func_idx, though I imagine this would be kind of brittle and toolchain-dependent. I don't entirely recommend this.

iximeow avatar Jan 21 '20 18:01 iximeow