Skip to content

Commit

Permalink
feat: enable pooling allocator
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
  • Loading branch information
rvolosatovs committed Nov 24, 2024
1 parent 5db5a6d commit 8124272
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions crates/wasmtime-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ wasmtime = { workspace = true, features = [
"threads",
"wat",
] }
wasmtime-cli-flags = { workspace = true, features = [
"async",
"cache",
"component-model",
"coredump",
"cranelift",
"gc",
"memory-protection-keys",
"parallel-compilation",
"pooling-allocator",
"threads",
] }
wasmtime-wasi = { workspace = true }
wasmtime-wasi-http = { workspace = true }
wit-component = { workspace = true }
Expand Down
33 changes: 27 additions & 6 deletions crates/wasmtime-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::type_complexity)]

use core::iter;
use core::pin::pin;
use core::time::Duration;

Expand Down Expand Up @@ -88,6 +89,24 @@ impl<C: Invoke> WasiHttpView for Ctx<C> {
}
}

// https://github.com/bytecodealliance/wasmtime/blob/b943666650696f1eb7ff8b217762b58d5ef5779d/src/commands/serve.rs#L641-L656
fn use_pooling_allocator_by_default() -> anyhow::Result<Option<bool>> {
const BITS_TO_TEST: u32 = 42;
let mut config = wasmtime::Config::new();
config.wasm_memory64(true);
config.static_memory_maximum_size(1 << BITS_TO_TEST);
let engine = wasmtime::Engine::new(&config)?;
let mut store = wasmtime::Store::new(&engine, ());
// NB: the maximum size is in wasm pages to take out the 16-bits of wasm
// page size here from the maximum size.
let ty = wasmtime::MemoryType::new64(0, Some(1 << (BITS_TO_TEST - 16)));
if wasmtime::Memory::new(&mut store, ty).is_ok() {
Ok(Some(true))
} else {
Ok(None)
}
}

#[instrument(level = "trace", skip(adapter, cx))]
async fn instantiate_pre<C>(
adapter: &[u8],
Expand All @@ -98,12 +117,14 @@ where
C: Invoke,
C::Context: Clone + 'static,
{
let engine = Engine::new(
wasmtime::Config::new()
.async_support(true)
.wasm_component_model(true),
)
.context("failed to initialize Wasmtime engine")?;
let mut opts = wasmtime_cli_flags::CommonOptions::try_parse_from(iter::empty::<&'static str>())
.context("failed to construct common Wasmtime options")?;
let mut config = opts
.config(None, use_pooling_allocator_by_default().unwrap_or(None))
.context("failed to construct Wasmtime config")?;
config.wasm_component_model(true);
config.async_support(true);
let engine = wasmtime::Engine::new(&config).context("failed to initialize Wasmtime engine")?;

let wasm = if workload.starts_with('.') || workload.starts_with('/') {
fs::read(&workload)
Expand Down

0 comments on commit 8124272

Please sign in to comment.