peterhuene edited PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Enginerather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with anEngineand then use aLinker(or directly with
Store::get_engine_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene updated PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Enginerather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with anEngineand then use aLinker(or directly with
Store::get_engine_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene updated PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Enginerather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with anEngineand then use aLinker(or directly with
Store::get_engine_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene edited PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Configrather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with anConfigand then use aLinker(or directly with
Store::get_host_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
I've added a comment where we have the unsafe impls for
HostFunc.
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
I'm now in favor of keeping the interning as the map can be cloned into multiple engines from a single config.
Thoughts with the recent changes?
peterhuene requested alexcrichton for a review on PR #2625.
peterhuene requested alexcrichton and fitzgen for a review on PR #2625.
peterhuene updated PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Configrather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with anConfigand then use aLinker(or directly with
Store::get_host_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
alexcrichton submitted PR Review.
alexcrichton created PR Review Comment:
Hm we may wish to brainstorm a different API for this. I'm worried about this causing unexpected panics in applications because you couldn't, for example, call this method recursively twice. I fear that would hamper usage of this API quite a lot. For example if you're implementing a WASI API which calls back into the module to malloc you'd need to be extremely careful to not
with_contextwhile malloc is being run.
alexcrichton created PR Review Comment:
Would it be possible to move this logic into
self.getdirectly? That internally handles the module-linking feature where if you import an instance it can be satisfied with a function definition of a two-level import. I think that ifself.storewere consulted inself.getthat it'd naturally support that though.
alexcrichton created PR Review Comment:
Ah ok makes sense! I don't feel that this should be removed really, only that I wasn't sure if the complexity was worth it (not even sure it's entirely worth it in
Linker, not like I've profiled anything...). Seems fine to leave though!
alexcrichton created PR Review Comment:
We could unify these two actually by fetching the
Storefrom the same location, the location in TLS post-dates the original storage into the function's environment here. I think then the two could be shared?
alexcrichton submitted PR Review.
alexcrichton created PR Review Comment:
I think the ideal API here would be
fn get<T>(&self) -> Option<&T>perhaps.Could we not support
removeand if consumers want it then they could haveT = RefCell<Option<U>>perhaps?
fitzgen submitted PR Review.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
Does this mean that we need to check that this store is using an engine that is using this
HostFunc's config? is it possible for these things to get out of sync (which seems like it would lead to unsafety)
fitzgen created PR Review Comment:
+1 for exposing
&Tinstead of&mut Tif users are the ones adding ref cells, then they are aware of the re-entrancy concerns and can design around that, rather than being surprised when they finally hit a corner case and have already designed and implemented a bunch of stuff that now needs to be redone
fitzgen created PR Review Comment:
Seems like, based on this call site, they should stay in-sync, but that perhaps
to_funcisn't always guaranteed to be safe. If I'm understanding all this correctly, then I thinkto_funcshould beunsafeand this call site should be the place with theunsafeblock that has a comment like "we know this store is using the same config as the host func".
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
Could we not support
removeand if consumers want it then they could haveT = RefCell<Option<U>>perhaps?Yeah, good point. I guess switching to
&Talone isn't enough, we also have to not support removal to make reentrancy panic-free.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
Worth documenting for users how they would get mutability and the ability to remove context via
RefCell<Option<T>>in the doc comments.
fitzgen edited PR Review Comment.
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
If we want to do that rather than sourcing it from the instance data, I'll make the shim shared.
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
I think you're right,
getmakes more sense for this implementation as it should behave as if the function were already defined in the linker even though it comes from the store.I'll make that change.
alexcrichton submitted PR Review.
alexcrichton created PR Review Comment:
Yeah I think we may as well change to that. It also has the added benefit of where the host state can remain 0-sized if
Selfis 0-sized (as is the case with all wasi functions). Additionally I think you could actually go ahead and removeinto_funcand simply haveinto_host_func, and thenFunc::wrapwould have a small amount more glue to handle the add-the-instance-to-the-store case?
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
That's a good point about reentrancy and moving the
RefCellinto the context value would give the callers more control over mutation scope.This will necessitate a method on
WasiorWasiCtxto set the context in the store with the exact type the generatedWasihost functions expect, though (it's how I had it in the RFC, but this was a poor attempt at a "cleaner" API).I'll get this fixed.
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
I will add a debug assertion just to double check that the
HostFuncis indeed related to the associated config.
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
I'll make it unsafe and comment as to the safety of the call.
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
That works. Thoughts on renaming the trait then?
peterhuene edited PR Review Comment.
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
Actually, the lookup can't be done quickly as the
HostFuncdoesn't store the requisite information to query the passed store.I think making this unsafe as suggested in the other comment is the best approach.
peterhuene updated PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Configrather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with anConfigand then use aLinker(or directly with
Store::get_host_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene updated PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Configrather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with anConfigand then use aLinker(or directly with
Store::get_host_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene requested alexcrichton for a review on PR #2625.
alexcrichton submitted PR Review.
peterhuene updated PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Configrather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with anConfigand then use aLinker(or directly with
Store::get_host_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene updated PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Configrather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with anConfigand then use aLinker(or directly with
Store::get_host_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene edited PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Configrather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with aConfigand then use aLinker(or directly with
Store::get_host_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene updated PR #2625 from engine-funcs to main:
This PR introduces defining host functions at the
Configrather than with
Functied to aStore.The intention here is to enable a host to define all of the functions once at
with aConfigand then use aLinker(or directly with
Store::get_host_func) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Storeis
short-lived and redefining the functions at every module instantiation a
noticeable performance hit.See bytecodealliance/rfcs#9 regarding these changes.
peterhuene updated PR #2625 from engine-funcs to main.
peterhuene updated PR #2625 from engine-funcs to main.
peterhuene updated PR #2625 from engine-funcs to main.
peterhuene updated PR #2625 from engine-funcs to main.
peterhuene updated PR #2625 from engine-funcs to main.
alexcrichton submitted PR Review.
peterhuene updated PR #2625 from engine-funcs to main.
alexcrichton submitted PR Review.
alexcrichton submitted PR Review.
alexcrichton created PR Review Comment:
Hm although then you'd have to also validate that no functions were defined yet, which is a bit wonky...
Do you have thoughts on this? I think it's fine either way myself.
alexcrichton created PR Review Comment:
The purpose of this being a constructor on
Storewas primarily to avoid switching at runtime, but becauseConfigis intended as a builder and it's "frozen" as anEngineI think this is fine to simply have as an extra method onConfig(probably namedasync_)
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
I'm leaning towards making it a setting like any other, rather than having
Config::new_async, and having to set "async" prior to defining any async shared host functions.With the instance allocator and now host functions in
Config, I'm concerned thatConfigis too stateful now. There's a similar problem withasync_stack_sizenot being able to be set after an instance allocation strategy was set because the pooling allocator relies on that setting.I'm wondering if we should move the instantiated instance allocator and shared host functions into
Engine(like we have forCompiler), leavingConfiga bunch of settings and only concerned with conflicts rather than ordering of the settings. This wouldn't be a breaking change to the API.This would change the semantics a little as each engine would now have its own instance allocator, but to me that makes more sense than having each engine share the instance allocator from a "configuration".
We'd still have to panic when you attempt to construct an
Enginefrom an incorrectConfig, though (e.g. you defined an async host function without setting the "async" setting, but now the order of those operations doesn't matter).What's your take?
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
However, we'd also have to panic when constructing an engine if we can't allocate enough memory for the pooling instance allocator (when used) instead of what we do currently which is return error from
Config::with_allocation_strategy.Given this would ever likely be done once early on and isn't recoverable in a way that would make progress (out of memory :shrug:), I'm not terribly concerned with a panicking if it improves
Config.
peterhuene edited PR Review Comment.
peterhuene edited PR Review Comment.
peterhuene edited PR Review Comment.
peterhuene updated PR #2625 from engine-funcs to main.
peterhuene submitted PR Review.
peterhuene created PR Review Comment:
I've pushed up a change for the instance allocator to get a sense of what I'm talking about above. It also removes that confusing "default" instance allocator in the process.
I'll see about the same for moving "async" to a setting and making instantiating the host functions into the engine rather than config.
If we don't like it this way, I'll remove the commits.
peterhuene updated PR #2625 from engine-funcs to main.
peterhuene updated PR #2625 from engine-funcs to main.
peterhuene updated PR #2625 from engine-funcs to main.
alexcrichton merged PR #2625.
Last updated: Dec 06 2025 at 06:05 UTC