Skip to content

Commit 62da0f5

Browse files
committed
V8Runtime: don't init V8 until neccessary
1 parent e338561 commit 62da0f5

5 files changed

Lines changed: 34 additions & 16 deletions

File tree

crates/core/src/host/host_controller.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::db::db_metrics::DB_METRICS;
99
use crate::db::relational_db::{self, DiskSizeFn, RelationalDB, Txdata};
1010
use crate::db::{self, spawn_tx_metrics_recorder};
1111
use crate::energy::{EnergyMonitor, EnergyQuanta, NullEnergyMonitor};
12+
use crate::host::module_host::ModuleRuntime as _;
1213
use crate::host::v8::V8Runtime;
1314
use crate::messages::control_db::{Database, HostType};
1415
use crate::module_host_context::ModuleCreationContext;
@@ -113,7 +114,7 @@ struct HostRuntimes {
113114
impl HostRuntimes {
114115
fn new(data_dir: Option<&ServerDataDir>) -> Arc<Self> {
115116
let wasmtime = WasmtimeRuntime::new(data_dir);
116-
let v8 = V8Runtime::new();
117+
let v8 = V8Runtime::default();
117118
Arc::new(Self { wasmtime, v8 })
118119
}
119120
}

crates/core/src/host/module_host.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::execution_context::{ExecutionContext, ReducerContext, Workload, Workl
1212
use crate::hash::Hash;
1313
use crate::identity::Identity;
1414
use crate::messages::control_db::Database;
15+
use crate::module_host_context::ModuleCreationContext;
1516
use crate::replica_context::ReplicaContext;
1617
use crate::sql::ast::SchemaViewer;
1718
use crate::sql::parser::RowLevelExpr;
@@ -306,6 +307,12 @@ impl ReducersMap {
306307
}
307308
}
308309

310+
/// A runtime that can create modules.
311+
pub trait ModuleRuntime {
312+
/// Creates a module based on the context `mcc`.
313+
fn make_actor(&self, mcc: ModuleCreationContext<'_>) -> anyhow::Result<impl Module>;
314+
}
315+
309316
pub trait DynModule: Send + Sync + 'static {
310317
fn replica_ctx(&self) -> &Arc<ReplicaContext>;
311318
fn scheduler(&self) -> &Scheduler;

crates/core/src/host/v8/mod.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
use crate::{
22
db::datastore::locking_tx_datastore::MutTxId,
33
host::{
4-
module_host::{DynModule, Module, ModuleInfo, ModuleInstance},
4+
module_host::{DynModule, Module, ModuleInfo, ModuleInstance, ModuleRuntime},
55
Scheduler,
66
},
77
module_host_context::ModuleCreationContext,
88
replica_context::ReplicaContext,
99
};
1010
use anyhow::anyhow;
11-
use std::sync::{Arc, Once};
11+
use std::sync::{Arc, LazyLock};
1212

1313
use super::module_host::CallReducerParams;
1414

15+
/// The V8 runtime, for modules written in e.g., JS or TypeScript.
16+
#[derive(Default)]
1517
pub struct V8Runtime {
1618
_priv: (),
1719
}
1820

19-
impl V8Runtime {
21+
impl ModuleRuntime for V8Runtime {
22+
fn make_actor(&self, mcc: ModuleCreationContext<'_>) -> anyhow::Result<impl Module> {
23+
V8_RUNTIME_GLOBAL.make_actor(mcc)
24+
}
25+
}
26+
27+
static V8_RUNTIME_GLOBAL: LazyLock<V8RuntimeInner> = LazyLock::new(V8RuntimeInner::new);
28+
29+
/// The actual V8 runtime, with initialization of V8.
30+
struct V8RuntimeInner {
31+
_priv: (),
32+
}
33+
34+
impl V8RuntimeInner {
2035
#[allow(clippy::new_without_default)]
21-
pub fn new() -> Self {
22-
static V8_INIT: Once = Once::new();
23-
V8_INIT.call_once(|| {
24-
// TODO
25-
});
36+
const fn new() -> Self {
37+
// TODO: actually setup V8.
2638

2739
Self { _priv: () }
2840
}
2941

30-
pub fn make_actor(&self, _: ModuleCreationContext<'_>) -> anyhow::Result<impl Module> {
42+
fn make_actor(&self, _: ModuleCreationContext<'_>) -> anyhow::Result<impl Module> {
3143
Err::<JsModule, _>(anyhow!("v8_todo"))
3244
}
3345
}

crates/core/src/host/wasm_common/module_host_actor.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ pub enum DescribeError {
122122
Decode(#[from] DecodeError),
123123
#[error(transparent)]
124124
RuntimeError(anyhow::Error),
125-
#[error("unimplemented RawModuleDef version")]
126-
UnimplementedRawModuleDefVersion,
127125
}
128126

129127
impl<T: WasmModule> WasmModuleHostActor<T> {

crates/core/src/host/wasmtime/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use wasmtime::{Engine, Linker, Module, StoreContext, StoreContextMut};
77

88
use crate::energy::{EnergyQuanta, ReducerBudget};
99
use crate::error::NodesError;
10+
use crate::host::module_host::ModuleRuntime;
1011
use crate::module_host_context::ModuleCreationContext;
1112

1213
mod wasm_instance_env;
@@ -80,11 +81,10 @@ impl WasmtimeRuntime {
8081
config.cache_config_load(tmpfile.path())?;
8182
Ok(())
8283
}
84+
}
8385

84-
pub fn make_actor(
85-
&self,
86-
mcc: ModuleCreationContext,
87-
) -> Result<impl super::module_host::Module, ModuleCreationError> {
86+
impl ModuleRuntime for WasmtimeRuntime {
87+
fn make_actor(&self, mcc: ModuleCreationContext) -> anyhow::Result<impl super::module_host::Module> {
8888
let module = Module::new(&self.engine, &mcc.program.bytes).map_err(ModuleCreationError::WasmCompileError)?;
8989

9090
let func_imports = module

0 commit comments

Comments
 (0)