Skip to content

Commit

Permalink
Add ability to configure allocation_strategy (#323)
Browse files Browse the repository at this point in the history
* Add `PoolingAllocationConfig`

* Update magnus and rb-sys
  • Loading branch information
ianks committed May 11, 2024
1 parent ffd785c commit c73fff8
Show file tree
Hide file tree
Showing 7 changed files with 561 additions and 9 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

require "bundler/setup"
require "wasmtime/rb"
require "wasmtime"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand Down
40 changes: 38 additions & 2 deletions ext/src/ruby_api/config.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
mod tracked_memory_creator;
pub(crate) use self::tracked_memory_creator::TrackedMemoryCreator;
use crate::{define_rb_intern, helpers::SymbolEnum};
use crate::{define_rb_intern, helpers::SymbolEnum, PoolingAllocationConfig};
use lazy_static::lazy_static;
use magnus::{
exception::{arg_error, type_error},
prelude::*,
r_hash::ForEach,
try_convert,
typed_data::Obj,
Error, RHash, Symbol, TryConvert, Value,
};
use std::{
convert::{TryFrom, TryInto},
ops::Deref,
sync::Arc,
};
use wasmtime::{Config, OptLevel, ProfilingStrategy, Strategy, WasmBacktraceDetails};
use wasmtime::{
Config, InstanceAllocationStrategy, OptLevel, ProfilingStrategy, Strategy, WasmBacktraceDetails,
};

define_rb_intern!(
DEBUG_INFO => "debug_info",
Expand All @@ -39,6 +44,9 @@ define_rb_intern!(
AUTO => "auto",
CRANELIFT => "cranelift",
WINCH => "winch",
ALLOCATION_STRATEGY => "allocation_strategy",
POOLING => "pooling",
ON_DEMAND => "on_demand",
);

lazy_static! {
Expand Down Expand Up @@ -123,6 +131,8 @@ pub fn hash_to_config(hash: RHash) -> Result<Config, Error> {
}
} else if *GENERATE_ADDRESS_MAP == id {
config.generate_address_map(entry.try_into()?);
} else if *ALLOCATION_STRATEGY == id {
config.allocation_strategy(entry.try_into()?);
} else {
return Err(Error::new(
arg_error(),
Expand Down Expand Up @@ -205,3 +215,29 @@ impl TryFrom<ConfigEntry> for wasmtime::Strategy {
STRATEGY_MAPPING.get(value.1)
}
}

impl TryFrom<ConfigEntry> for InstanceAllocationStrategy {
type Error = magnus::Error;

fn try_from(value: ConfigEntry) -> Result<Self, Error> {
if let Ok(strategy) = Obj::<PoolingAllocationConfig>::try_convert(value.1) {
return Ok(InstanceAllocationStrategy::Pooling(strategy.to_inner()?));
}

if let Ok(symbol) = Symbol::try_convert(value.1) {
if symbol.equal(Symbol::new("pooling"))? {
return Ok(InstanceAllocationStrategy::Pooling(Default::default()));
} else if symbol.equal(Symbol::new("on_demand"))? {
return Ok(InstanceAllocationStrategy::OnDemand);
}
}

Err(Error::new(
arg_error(),
format!(
"invalid instance allocation strategy: {}",
value.1.inspect()
),
))
}
}
3 changes: 3 additions & 0 deletions ext/src/ruby_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod linker;
mod memory;
mod module;
mod params;
mod pooling_allocation_config;
mod store;
mod table;
mod trap;
Expand All @@ -36,6 +37,7 @@ pub use linker::Linker;
pub use memory::Memory;
pub use module::Module;
pub use params::Params;
pub use pooling_allocation_config::PoolingAllocationConfig;
pub use store::Store;
pub use trap::Trap;
pub use wasi_ctx::WasiCtx;
Expand Down Expand Up @@ -86,6 +88,7 @@ pub fn init(ruby: &Ruby) -> Result<(), Error> {
table::init()?;
global::init()?;
wasi_ctx::init()?;
pooling_allocation_config::init()?;

Ok(())
}
Loading

0 comments on commit c73fff8

Please sign in to comment.