Skip to content

Commit 88dec94

Browse files
committed
Address comments
1 parent 8fce6b9 commit 88dec94

File tree

4 files changed

+49
-67
lines changed

4 files changed

+49
-67
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ fn common_call<'scope, R>(
619619
let mut tx_slot = env.instance_env.tx.clone();
620620

621621
// Start the timer.
622-
// We'd like this tightly around `__call_reducer__`.
622+
// We'd like this tightly around `call`.
623623
env.start_funcall(name, timestamp, func_type);
624624

625625
// Call the function with `tx` provided.

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

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -398,18 +398,8 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
398398

399399
let call_result = call_result.map(|code| handle_error_sink_code(code, error));
400400

401-
// Compute fuel and heap usage.
402-
let remaining_fuel = get_store_fuel(store);
403-
let remaining: FunctionBudget = remaining_fuel.into();
404-
let energy = module_host_actor::EnergyStats { budget, remaining };
405-
let memory_allocation = store.data().get_mem().memory.data_size(&store);
406-
407401
module_host_actor::ReducerExecuteResult {
408-
stats: ExecutionStats {
409-
energy,
410-
timings,
411-
memory_allocation,
412-
},
402+
stats: get_execution_stats(store, budget, timings),
413403
call_result,
414404
}
415405
}
@@ -432,11 +422,7 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
432422

433423
let Some(call_view) = self.call_view.as_ref() else {
434424
return module_host_actor::ViewExecuteResult {
435-
stats: ExecutionStats {
436-
energy: module_host_actor::EnergyStats::ZERO,
437-
timings: module_host_actor::ExecutionTimings::zero(),
438-
memory_allocation: get_memory_size(store),
439-
},
425+
stats: zero_execution_stats(store),
440426
call_result: Err(anyhow::anyhow!(
441427
"Module defines view {} but does not export `{}`",
442428
op.name,
@@ -468,18 +454,8 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
468454
.and_then(|code| handle_result_sink_code(code, result_bytes).map_err(|e| anyhow::anyhow!(e)))
469455
.map(|r| r.into());
470456

471-
// Compute fuel and heap usage.
472-
let remaining_fuel = get_store_fuel(store);
473-
let remaining: FunctionBudget = remaining_fuel.into();
474-
let energy = module_host_actor::EnergyStats { budget, remaining };
475-
let memory_allocation = store.data().get_mem().memory.data_size(&store);
476-
477457
module_host_actor::ViewExecuteResult {
478-
stats: ExecutionStats {
479-
energy,
480-
timings,
481-
memory_allocation,
482-
},
458+
stats: get_execution_stats(store, budget, timings),
483459
call_result,
484460
}
485461
}
@@ -503,11 +479,7 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
503479

504480
let Some(call_view_anon) = self.call_view_anon.as_ref() else {
505481
return module_host_actor::ViewExecuteResult {
506-
stats: ExecutionStats {
507-
energy: module_host_actor::EnergyStats::ZERO,
508-
timings: module_host_actor::ExecutionTimings::zero(),
509-
memory_allocation: get_memory_size(store),
510-
},
482+
stats: zero_execution_stats(store),
511483
call_result: Err(anyhow::anyhow!(
512484
"Module defines anonymous view {} but does not export `{}`",
513485
op.name,
@@ -526,18 +498,9 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
526498
let call_result = call_result
527499
.and_then(|code| handle_result_sink_code(code, result_bytes).map_err(|e| anyhow::anyhow!(e)))
528500
.map(|r| r.into());
529-
// Compute fuel and heap usage.
530-
let remaining_fuel = get_store_fuel(store);
531-
let remaining: FunctionBudget = remaining_fuel.into();
532-
let energy = module_host_actor::EnergyStats { budget, remaining };
533-
let memory_allocation = store.data().get_mem().memory.data_size(&store);
534501

535502
module_host_actor::ViewExecuteResult {
536-
stats: ExecutionStats {
537-
energy,
538-
timings,
539-
memory_allocation,
540-
},
503+
stats: get_execution_stats(store, budget, timings),
541504
call_result,
542505
}
543506
}
@@ -567,11 +530,7 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
567530

568531
let Some(call_procedure) = self.call_procedure.as_ref() else {
569532
return module_host_actor::ProcedureExecuteResult {
570-
stats: ExecutionStats {
571-
energy: module_host_actor::EnergyStats::ZERO,
572-
timings: module_host_actor::ExecutionTimings::zero(),
573-
memory_allocation: get_memory_size(store),
574-
},
533+
stats: zero_execution_stats(store),
575534
call_result: Err(anyhow::anyhow!(
576535
"Module defines procedure {} but does not export `{}`",
577536
op.name,
@@ -608,18 +567,8 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
608567
})
609568
});
610569

611-
let remaining_fuel = get_store_fuel(store);
612-
let remaining = FunctionBudget::from(remaining_fuel);
613-
614-
let energy = module_host_actor::EnergyStats { budget, remaining };
615-
let memory_allocation = get_memory_size(store);
616-
617570
module_host_actor::ProcedureExecuteResult {
618-
stats: ExecutionStats {
619-
energy,
620-
timings,
621-
memory_allocation,
622-
},
571+
stats: get_execution_stats(store, budget, timings),
623572
call_result,
624573
}
625574
}
@@ -671,6 +620,33 @@ fn prepare_connection_id_for_call(caller_connection_id: ConnectionId) -> [u64; 2
671620
bytemuck::must_cast(caller_connection_id.as_le_byte_array())
672621
}
673622

623+
/// Compute fuel and heap usage for a call and construct the `ExecutionStats`.
624+
fn get_execution_stats(
625+
store: &Store<WasmInstanceEnv>,
626+
initial_budget: FunctionBudget,
627+
timings: module_host_actor::ExecutionTimings,
628+
) -> ExecutionStats {
629+
let remaining_fuel = get_store_fuel(store);
630+
let remaining: FunctionBudget = remaining_fuel.into();
631+
let energy = module_host_actor::EnergyStats {
632+
budget: initial_budget,
633+
remaining,
634+
};
635+
ExecutionStats {
636+
energy,
637+
timings,
638+
memory_allocation: get_memory_size(store),
639+
}
640+
}
641+
642+
fn zero_execution_stats(store: &Store<WasmInstanceEnv>) -> ExecutionStats {
643+
ExecutionStats {
644+
energy: module_host_actor::EnergyStats::ZERO,
645+
timings: module_host_actor::ExecutionTimings::zero(),
646+
memory_allocation: get_memory_size(store),
647+
}
648+
}
649+
674650
fn get_memory_size(store: &Store<WasmInstanceEnv>) -> usize {
675651
store.data().get_mem().memory.data_size(store)
676652
}

modules/module-test-ts/src/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ const hasSpecialStuffRow = {
137137
};
138138

139139
// Rust: two tables with the same row type: player & logged_out_player
140-
const playerLikeRow = {
140+
const playerLikeRow = t.row({
141141
identity: t.identity().primaryKey(),
142142
player_id: t.u64().autoInc().unique(),
143143
name: t.string().unique(),
144-
};
144+
});
145145

146146
// ─────────────────────────────────────────────────────────────────────────────
147147
// SCHEMA (tables + indexes + visibility)
@@ -214,6 +214,17 @@ const spacetimedb = schema(
214214
table({ name: 'logged_out_player', public: true }, playerLikeRow)
215215
);
216216

217+
// ─────────────────────────────────────────────────────────────────────────────
218+
// VIEWS
219+
// ─────────────────────────────────────────────────────────────────────────────
220+
221+
spacetimedb.view(
222+
'my_player',
223+
playerLikeRow.optional(),
224+
// FIXME: this should not be necessary; change `OptionBuilder` to accept `null|undefined` for `none`
225+
ctx => ctx.db.player.identity.find(ctx.sender) ?? undefined
226+
);
227+
217228
// ─────────────────────────────────────────────────────────────────────────────
218229
// REDUCERS (mirroring Rust order & behavior)
219230
// ─────────────────────────────────────────────────────────────────────────────
@@ -320,7 +331,7 @@ spacetimedb.reducer(
320331

321332
// try_insert TestE { id: 0, name: "Tyler" }
322333
try {
323-
const inserted = ctx.db.test_e.tryInsert({ id: 0n, name: 'Tyler' });
334+
const inserted = ctx.db.test_e.insert({ id: 0n, name: 'Tyler' });
324335
console.info(`Inserted: ${JSON.stringify(inserted)}`);
325336
} catch (err) {
326337
console.info(`Error: ${String(err)}`);

modules/sdk-test-ts/src/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,3 @@ spacetimedb.reducer(
10151015
}
10161016
}
10171017
);
1018-
1019-
const X = t.object('X', { a: t.i32() });
1020-
spacetimedb.view('a', X, () => {
1021-
return { a: 1 };
1022-
});

0 commit comments

Comments
 (0)