Switch from epoch to fuel

This commit is contained in:
Isaac Clayton 2022-07-13 11:09:06 +02:00
parent 7f11a32364
commit d04c3388b4
4 changed files with 12 additions and 41 deletions

View file

@ -60,7 +60,7 @@ fn main() {
fn create_default_engine() -> Engine { fn create_default_engine() -> Engine {
let mut config = Config::default(); let mut config = Config::default();
config.async_support(true); config.async_support(true);
config.epoch_interruption(true); config.consume_fuel(true);
Engine::new(&config).expect("Could not create engine") Engine::new(&config).expect("Could not create engine")
} }

View file

@ -23,7 +23,7 @@ mod tests {
} }
async { async {
let (mut runtime, incrementer) = PluginBuilder::new_with_default_ctx() let mut runtime = PluginBuilder::new_with_default_ctx()
.unwrap() .unwrap()
.host_function("mystery_number", |input: u32| input + 7) .host_function("mystery_number", |input: u32| input + 7)
.unwrap() .unwrap()
@ -53,8 +53,6 @@ mod tests {
.await .await
.unwrap(); .unwrap();
std::thread::spawn(move || incrementer.block_on());
let plugin = TestPlugin { let plugin = TestPlugin {
noop: runtime.function("noop").unwrap(), noop: runtime.function("noop").unwrap(),
constant: runtime.function("constant").unwrap(), constant: runtime.function("constant").unwrap(),

View file

@ -62,8 +62,7 @@ pub struct PluginBuilder {
wasi_ctx: WasiCtx, wasi_ctx: WasiCtx,
engine: Engine, engine: Engine,
linker: Linker<WasiCtxAlloc>, linker: Linker<WasiCtxAlloc>,
delta: u64, fuel_refill: u64,
epoch: std::time::Duration,
} }
/// Creates a default engine for compiling Wasm. /// Creates a default engine for compiling Wasm.
@ -73,7 +72,7 @@ pub struct PluginBuilder {
pub fn create_default_engine() -> Result<Engine, Error> { pub fn create_default_engine() -> Result<Engine, Error> {
let mut config = Config::default(); let mut config = Config::default();
config.async_support(true); config.async_support(true);
config.epoch_interruption(true); config.consume_fuel(true);
Engine::new(&config) Engine::new(&config)
} }
@ -88,8 +87,7 @@ impl PluginBuilder {
wasi_ctx, wasi_ctx,
engine, engine,
linker, linker,
delta: 1, fuel_refill: 1000,
epoch: std::time::Duration::from_millis(100),
}) })
} }
@ -245,17 +243,7 @@ impl PluginBuilder {
/// Initializes a [`Plugin`] from a given compiled Wasm module. /// Initializes a [`Plugin`] from a given compiled Wasm module.
/// Both binary (`.wasm`) and text (`.wat`) module formats are supported. /// Both binary (`.wasm`) and text (`.wat`) module formats are supported.
pub async fn init<T: AsRef<[u8]>>( pub async fn init<T: AsRef<[u8]>>(self, precompiled: bool, module: T) -> Result<Plugin, Error> {
self,
precompiled: bool,
module: T,
) -> Result<
(
Plugin,
std::pin::Pin<Box<dyn Future<Output = ()> + Send + 'static>>,
),
Error,
> {
Plugin::init(precompiled, module.as_ref().to_vec(), self).await Plugin::init(precompiled, module.as_ref().to_vec(), self).await
} }
} }
@ -320,13 +308,7 @@ impl Plugin {
precompiled: bool, precompiled: bool,
module: Vec<u8>, module: Vec<u8>,
plugin: PluginBuilder, plugin: PluginBuilder,
) -> Result< ) -> Result<Self, Error> {
(
Self,
std::pin::Pin<Box<dyn Future<Output = ()> + Send + 'static>>,
),
Error,
> {
// initialize the WebAssembly System Interface context // initialize the WebAssembly System Interface context
let engine = plugin.engine; let engine = plugin.engine;
let mut linker = plugin.linker; let mut linker = plugin.linker;
@ -348,15 +330,9 @@ impl Plugin {
Module::new(&engine, module)? Module::new(&engine, module)?
}; };
// set up automatic yielding after given duration // set up automatic yielding after fuel expires
store.epoch_deadline_async_yield_and_update(plugin.delta); store.out_of_fuel_async_yield(u64::MAX, plugin.fuel_refill);
let epoch = plugin.epoch; store.add_fuel(plugin.fuel_refill).unwrap();
let incrementer = Box::pin(async move {
loop {
smol::Timer::after(epoch).await;
engine.increment_epoch();
}
});
// load the provided module into the asynchronous runtime // load the provided module into the asynchronous runtime
linker.module_async(&mut store, "", &module).await?; linker.module_async(&mut store, "", &module).await?;
@ -371,9 +347,7 @@ impl Plugin {
free_buffer, free_buffer,
}); });
let plugin = Plugin { store, instance }; Ok(Plugin { store, instance })
Ok((plugin, incrementer))
} }
/// Attaches a file or directory the the given system path to the runtime. /// Attaches a file or directory the the given system path to the runtime.

View file

@ -9,7 +9,7 @@ use std::{any::Any, path::PathBuf, sync::Arc};
use util::ResultExt; use util::ResultExt;
pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> { pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> {
let (plugin, incrementer) = PluginBuilder::new_with_default_ctx()? let plugin = PluginBuilder::new_with_default_ctx()?
.host_function_async("command", |command: String| async move { .host_function_async("command", |command: String| async move {
let mut args = command.split(' '); let mut args = command.split(' ');
let command = args.next().unwrap(); let command = args.next().unwrap();
@ -26,7 +26,6 @@ pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> {
) )
.await?; .await?;
executor.spawn(incrementer).detach();
PluginLspAdapter::new(plugin, executor).await PluginLspAdapter::new(plugin, executor).await
} }