Hey friends! I'm familiarising myself with the component model by building a simple plugin system. At the moment, I have a single Store that is shared between my plugins. This store tracks the main application state, which is primarily registrations of data coming from the plugins.
The basic pattern I have is: host calls plugin.call_init(), plugin calls host_please_register_stuff(some_stuff).
In the exported host_please_register_stuff function I need to identify from which plugin a given call came from, so that I can tie the registration to a plugin. As such, I have a comparatively large amount of global state, and a single integer plugin identifier that should be plugin-local state.
What's the most idiomatic way to do this with Wasmtime? The main ways I considered were:
Store per plugin that stores the ID. Each store references the larger global state, which uses RefCell to allow mutating.Store per plugin that stores the ID. After init calls, do some merging work to collate the registrations from all the plugins.Store and track a currently_executing_plugin ID on it. Update this when calling init and read from it when host_please_register_stuff is called.I'm not sure I follow your application architecture exactly, but as a general rule of thumb: the less you can put in a single Store, and the shorter-lived they are, the better. An instance will not be dropped until its Store is dropped, for example, so if you keep a Store around for a long time and reuse it for many instances, you will hold memory alive longer than needed. Stores and Instances are built to be disposable, and can be instantiated quickly: https://docs.wasmtime.dev/examples-fast-instantiation.html
so given that, I think this is your best choice, of those presented:
Sy Brand said:
- Having one
Storeper plugin that stores the ID. Each store references the larger global state, which usesRefCellto allow mutating.
Makes sense, thank you!
Last updated: Dec 06 2025 at 06:05 UTC