Skip to content

Commit c73fff8

Browse files
authored
Add ability to configure allocation_strategy (#323)
* Add `PoolingAllocationConfig` * Update magnus and rb-sys
1 parent ffd785c commit c73fff8

File tree

7 files changed

+561
-9
lines changed

7 files changed

+561
-9
lines changed

Cargo.lock

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

bin/console

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# frozen_string_literal: true
33

44
require "bundler/setup"
5-
require "wasmtime/rb"
5+
require "wasmtime"
66

77
# You can add fixtures and/or initialization code here to make experimenting
88
# with your gem easier. You can also use a different console, if you like.

ext/src/ruby_api/config.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
mod tracked_memory_creator;
22
pub(crate) use self::tracked_memory_creator::TrackedMemoryCreator;
3-
use crate::{define_rb_intern, helpers::SymbolEnum};
3+
use crate::{define_rb_intern, helpers::SymbolEnum, PoolingAllocationConfig};
44
use lazy_static::lazy_static;
55
use magnus::{
66
exception::{arg_error, type_error},
77
prelude::*,
88
r_hash::ForEach,
9+
try_convert,
10+
typed_data::Obj,
911
Error, RHash, Symbol, TryConvert, Value,
1012
};
1113
use std::{
1214
convert::{TryFrom, TryInto},
15+
ops::Deref,
1316
sync::Arc,
1417
};
15-
use wasmtime::{Config, OptLevel, ProfilingStrategy, Strategy, WasmBacktraceDetails};
18+
use wasmtime::{
19+
Config, InstanceAllocationStrategy, OptLevel, ProfilingStrategy, Strategy, WasmBacktraceDetails,
20+
};
1621

1722
define_rb_intern!(
1823
DEBUG_INFO => "debug_info",
@@ -39,6 +44,9 @@ define_rb_intern!(
3944
AUTO => "auto",
4045
CRANELIFT => "cranelift",
4146
WINCH => "winch",
47+
ALLOCATION_STRATEGY => "allocation_strategy",
48+
POOLING => "pooling",
49+
ON_DEMAND => "on_demand",
4250
);
4351

4452
lazy_static! {
@@ -123,6 +131,8 @@ pub fn hash_to_config(hash: RHash) -> Result<Config, Error> {
123131
}
124132
} else if *GENERATE_ADDRESS_MAP == id {
125133
config.generate_address_map(entry.try_into()?);
134+
} else if *ALLOCATION_STRATEGY == id {
135+
config.allocation_strategy(entry.try_into()?);
126136
} else {
127137
return Err(Error::new(
128138
arg_error(),
@@ -205,3 +215,29 @@ impl TryFrom<ConfigEntry> for wasmtime::Strategy {
205215
STRATEGY_MAPPING.get(value.1)
206216
}
207217
}
218+
219+
impl TryFrom<ConfigEntry> for InstanceAllocationStrategy {
220+
type Error = magnus::Error;
221+
222+
fn try_from(value: ConfigEntry) -> Result<Self, Error> {
223+
if let Ok(strategy) = Obj::<PoolingAllocationConfig>::try_convert(value.1) {
224+
return Ok(InstanceAllocationStrategy::Pooling(strategy.to_inner()?));
225+
}
226+
227+
if let Ok(symbol) = Symbol::try_convert(value.1) {
228+
if symbol.equal(Symbol::new("pooling"))? {
229+
return Ok(InstanceAllocationStrategy::Pooling(Default::default()));
230+
} else if symbol.equal(Symbol::new("on_demand"))? {
231+
return Ok(InstanceAllocationStrategy::OnDemand);
232+
}
233+
}
234+
235+
Err(Error::new(
236+
arg_error(),
237+
format!(
238+
"invalid instance allocation strategy: {}",
239+
value.1.inspect()
240+
),
241+
))
242+
}
243+
}

ext/src/ruby_api/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod linker;
2222
mod memory;
2323
mod module;
2424
mod params;
25+
mod pooling_allocation_config;
2526
mod store;
2627
mod table;
2728
mod trap;
@@ -36,6 +37,7 @@ pub use linker::Linker;
3637
pub use memory::Memory;
3738
pub use module::Module;
3839
pub use params::Params;
40+
pub use pooling_allocation_config::PoolingAllocationConfig;
3941
pub use store::Store;
4042
pub use trap::Trap;
4143
pub use wasi_ctx::WasiCtx;
@@ -86,6 +88,7 @@ pub fn init(ruby: &Ruby) -> Result<(), Error> {
8688
table::init()?;
8789
global::init()?;
8890
wasi_ctx::init()?;
91+
pooling_allocation_config::init()?;
8992

9093
Ok(())
9194
}

0 commit comments

Comments
 (0)