Skip to content

Commit 8124272

Browse files
committed
feat: enable pooling allocator
Signed-off-by: Roman Volosatovs <[email protected]>
1 parent 5db5a6d commit 8124272

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/wasmtime-cli/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ wasmtime = { workspace = true, features = [
4444
"threads",
4545
"wat",
4646
] }
47+
wasmtime-cli-flags = { workspace = true, features = [
48+
"async",
49+
"cache",
50+
"component-model",
51+
"coredump",
52+
"cranelift",
53+
"gc",
54+
"memory-protection-keys",
55+
"parallel-compilation",
56+
"pooling-allocator",
57+
"threads",
58+
] }
4759
wasmtime-wasi = { workspace = true }
4860
wasmtime-wasi-http = { workspace = true }
4961
wit-component = { workspace = true }

crates/wasmtime-cli/src/lib.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(clippy::type_complexity)]
22

3+
use core::iter;
34
use core::pin::pin;
45
use core::time::Duration;
56

@@ -88,6 +89,24 @@ impl<C: Invoke> WasiHttpView for Ctx<C> {
8889
}
8990
}
9091

92+
// https://github.com/bytecodealliance/wasmtime/blob/b943666650696f1eb7ff8b217762b58d5ef5779d/src/commands/serve.rs#L641-L656
93+
fn use_pooling_allocator_by_default() -> anyhow::Result<Option<bool>> {
94+
const BITS_TO_TEST: u32 = 42;
95+
let mut config = wasmtime::Config::new();
96+
config.wasm_memory64(true);
97+
config.static_memory_maximum_size(1 << BITS_TO_TEST);
98+
let engine = wasmtime::Engine::new(&config)?;
99+
let mut store = wasmtime::Store::new(&engine, ());
100+
// NB: the maximum size is in wasm pages to take out the 16-bits of wasm
101+
// page size here from the maximum size.
102+
let ty = wasmtime::MemoryType::new64(0, Some(1 << (BITS_TO_TEST - 16)));
103+
if wasmtime::Memory::new(&mut store, ty).is_ok() {
104+
Ok(Some(true))
105+
} else {
106+
Ok(None)
107+
}
108+
}
109+
91110
#[instrument(level = "trace", skip(adapter, cx))]
92111
async fn instantiate_pre<C>(
93112
adapter: &[u8],
@@ -98,12 +117,14 @@ where
98117
C: Invoke,
99118
C::Context: Clone + 'static,
100119
{
101-
let engine = Engine::new(
102-
wasmtime::Config::new()
103-
.async_support(true)
104-
.wasm_component_model(true),
105-
)
106-
.context("failed to initialize Wasmtime engine")?;
120+
let mut opts = wasmtime_cli_flags::CommonOptions::try_parse_from(iter::empty::<&'static str>())
121+
.context("failed to construct common Wasmtime options")?;
122+
let mut config = opts
123+
.config(None, use_pooling_allocator_by_default().unwrap_or(None))
124+
.context("failed to construct Wasmtime config")?;
125+
config.wasm_component_model(true);
126+
config.async_support(true);
127+
let engine = wasmtime::Engine::new(&config).context("failed to initialize Wasmtime engine")?;
107128

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

0 commit comments

Comments
 (0)