Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions crates/core/src/host/v8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ fn env_on_isolate_unwrap(isolate: &mut Isolate) -> &mut JsInstanceEnv {
struct JsInstanceEnv {
instance_env: InstanceEnv,
module_def: Option<Arc<ModuleDef>>,
/// Last used-heap sample captured by the worker's periodic heap checks.
cached_used_heap_size: usize,

/// The slab of `BufferIters` created for this instance.
iters: RowIters,
Expand All @@ -353,6 +355,7 @@ impl JsInstanceEnv {
Self {
instance_env,
module_def: None,
cached_used_heap_size: 0,
call_times: CallTimes::new(),
iters: <_>::default(),
chunk_pool: <_>::default(),
Expand Down Expand Up @@ -407,6 +410,16 @@ impl JsInstanceEnv {
}
}

/// Refresh the cached heap usage after an explicit V8 heap sample.
fn set_cached_used_heap_size(&mut self, bytes: usize) {
self.cached_used_heap_size = bytes;
}

/// Return the last heap sample without forcing a fresh V8 query.
fn cached_used_heap_size(&self) -> usize {
self.cached_used_heap_size
}

fn set_module_def(&mut self, module_def: Arc<ModuleDef>) {
self.module_def = Some(module_def);
}
Expand Down Expand Up @@ -924,7 +937,10 @@ fn adjust_gauge(gauge: &IntGauge, delta: i64) {
}

fn sample_heap_stats(scope: &mut PinScope<'_, '_>, metrics: &mut V8HeapMetrics) -> v8::HeapStatistics {
// Whenever we sample heap statistics, we cache them on the isolate so that
// the per-call execution stats can avoid querying them on each invocation.
let stats = scope.get_heap_statistics();
env_on_isolate_unwrap(scope).set_cached_used_heap_size(stats.used_heap_size());
metrics.observe(&stats);
stats
}
Expand Down Expand Up @@ -1788,9 +1804,8 @@ where
// Derive energy stats.
let energy = energy_from_elapsed(budget, timings.total_duration);

// Fetch the currently used heap size in V8.
// The used size is ostensibly fairer than the total size.
let memory_allocation = scope.get_heap_statistics().used_heap_size();
// Reuse the last periodic heap sample instead of querying V8 on every call.
let memory_allocation = env_on_isolate_unwrap(scope).cached_used_heap_size();

let stats = ExecutionStats {
energy,
Expand Down
Loading