Unverified Commit ba6b218b authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Disable stale-span tracking for complete loads

Complete trace parsing skips cache invalidation but still hashed every changed span into a set that was never read. Make stale-span collection explicitly disableable, reducing retired instructions by about 1.4% while keeping peak memory flat and preserving tracking for live readers.
Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 3f36bc1c
......@@ -10,6 +10,7 @@ use super::TraceFormat;
use crate::{
FxIndexMap,
span::{SpanArgs, SpanIndex},
store::OutdatedSpans,
store_container::StoreContainer,
timestamp::Timestamp,
};
......@@ -187,7 +188,7 @@ impl TraceFormat for HeaptrackFormat {
fn read(&mut self, mut buffer: &[u8], _reuse: &mut Self::Reused) -> anyhow::Result<usize> {
let mut bytes_read = 0;
let mut outdated_spans = FxHashSet::default();
let mut outdated_spans = OutdatedSpans::default();
let mut store = self.store.write();
'outer: while let Some(line_end) = buffer.iter().position(|b| *b == b'\n') {
let full_line = &buffer[..line_end];
......
use std::{borrow::Cow, fmt::Display, sync::Arc};
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::FxHashMap;
use serde::Deserialize;
use turbo_rcstr::{RcStr, rcstr};
use super::TraceFormat;
use crate::{FxIndexMap, span::SpanIndex, store_container::StoreContainer, timestamp::Timestamp};
use crate::{
FxIndexMap, span::SpanIndex, store::OutdatedSpans, store_container::StoreContainer,
timestamp::Timestamp,
};
pub struct NextJsFormat {
store: Arc<StoreContainer>,
......@@ -28,7 +31,7 @@ impl TraceFormat for NextJsFormat {
fn read(&mut self, mut buffer: &[u8], _reuse: &mut Self::Reused) -> anyhow::Result<usize> {
let mut bytes_read = 0;
let mut outdated_spans = FxHashSet::default();
let mut outdated_spans = OutdatedSpans::default();
while let Some(line_end) = buffer.iter().position(|b| *b == b'\n') {
let line = &buffer[..line_end];
buffer = &buffer[line_end + 1..];
......
......@@ -14,6 +14,7 @@ use turbopack_trace_utils::tracing::{TraceRow, TraceValue};
use super::TraceFormat;
use crate::{
span::{SpanArgs, SpanIndex},
store::OutdatedSpans,
store_container::{StoreContainer, StoreWriteGuard},
timestamp::Timestamp,
};
......@@ -108,7 +109,7 @@ pub struct TurbopackFormat {
dropped_ids: FxHashSet<u64>,
remaining_ids_to_drop: usize,
queued_rows: FxHashMap<u64, Vec<InternalRow>>,
outdated_spans: FxHashSet<SpanIndex>,
outdated_spans: OutdatedSpans,
thread_stacks: FxHashMap<u64, Vec<u64>>,
thread_allocation_counters: FxHashMap<u64, AllocationInfo>,
self_time_started: FxHashMap<(u64, u64), Timestamp>,
......@@ -128,7 +129,7 @@ impl TurbopackFormat {
dropped_ids: FxHashSet::with_capacity_and_hasher(drop_ids, Default::default()),
remaining_ids_to_drop: drop_ids,
queued_rows: FxHashMap::with_capacity_and_hasher(1_024, Default::default()),
outdated_spans: FxHashSet::with_capacity_and_hasher(8_192, Default::default()),
outdated_spans: OutdatedSpans::with_capacity(8_192, invalidate_caches),
thread_stacks: FxHashMap::with_capacity_and_hasher(64, Default::default()),
thread_allocation_counters: FxHashMap::with_capacity_and_hasher(64, Default::default()),
self_time_started: FxHashMap::with_capacity_and_hasher(256, Default::default()),
......
......@@ -554,10 +554,14 @@ impl SpanEventRef<'_> {
#[cfg(test)]
mod tests {
use rustc_hash::FxHashSet;
use turbo_rcstr::RcStr;
use crate::{span::SpanArgs, span_ref::SpanRef, store::Store, timestamp::Timestamp};
use crate::{
span::SpanArgs,
span_ref::SpanRef,
store::{OutdatedSpans, Store},
timestamp::Timestamp,
};
fn span_ref<'a>(store: &'a Store, idx: crate::span::SpanIndex) -> SpanRef<'a> {
SpanRef {
......@@ -570,7 +574,7 @@ mod tests {
#[test]
fn totals_aggregate_subtree() {
let mut store = Store::new();
let mut outdated = FxHashSet::default();
let mut outdated = OutdatedSpans::default();
// root → a → b
// root → c
......@@ -638,7 +642,7 @@ mod tests {
#[test]
fn totals_invalidate_and_recompute() {
let mut store = Store::new();
let mut outdated = FxHashSet::default();
let mut outdated = OutdatedSpans::default();
let s = store.add_span(
None,
Timestamp::from_micros(0),
......@@ -654,7 +658,7 @@ mod tests {
assert_eq!(before, 1000 - 32);
// Add more allocations and invalidate.
let mut outdated = FxHashSet::default();
let mut outdated = OutdatedSpans::default();
store.add_allocation(s, 200, 2, &mut outdated);
store.invalidate_outdated_spans(&outdated);
......
......@@ -33,6 +33,61 @@ type MemorySample = (Timestamp, u64, u8);
/// Maximum number of memory samples returned in a query result.
const MAX_MEMORY_SAMPLES: usize = 200;
pub struct OutdatedSpans {
spans: FxHashSet<SpanIndex>,
enabled: bool,
}
impl OutdatedSpans {
pub fn with_capacity(capacity: usize, enabled: bool) -> Self {
Self {
spans: if enabled {
FxHashSet::with_capacity_and_hasher(capacity, Default::default())
} else {
FxHashSet::default()
},
enabled,
}
}
fn insert(&mut self, span: SpanIndex) {
if self.enabled {
self.spans.insert(span);
}
}
pub fn clear(&mut self) {
self.spans.clear();
}
fn contains(&self, span: &SpanIndex) -> bool {
self.spans.contains(span)
}
fn iter(&self) -> impl Iterator<Item = &SpanIndex> {
self.spans.iter()
}
}
impl Default for OutdatedSpans {
fn default() -> Self {
Self::with_capacity(0, true)
}
}
#[cfg(test)]
mod outdated_spans_tests {
use super::*;
#[test]
fn disabled_collector_does_not_retain_spans() {
let mut outdated = OutdatedSpans::with_capacity(8, false);
outdated.insert(SpanIndex::new(1).unwrap());
assert_eq!(outdated.iter().count(), 0);
}
}
pub struct Store {
pub(crate) spans: ChunkedVec<Span>,
pub(crate) self_time_tree: Option<SelfTimeTree<SpanIndex>>,
......@@ -109,7 +164,7 @@ impl Store {
category: RcStr,
name: RcStr,
args: SpanArgs,
outdated_spans: &mut FxHashSet<SpanIndex>,
outdated_spans: &mut OutdatedSpans,
) -> SpanIndex {
let id = SpanIndex::new(self.spans.len()).unwrap();
let ignore_self_time = &name == "thread" || &name == "blocking";
......@@ -163,7 +218,7 @@ impl Store {
&mut self,
span_index: SpanIndex,
args: SpanArgs,
outdated_spans: &mut FxHashSet<SpanIndex>,
outdated_spans: &mut OutdatedSpans,
) {
let span = &mut self.spans[span_index.get()];
span.args.extend(args);
......@@ -193,7 +248,7 @@ impl Store {
start: Timestamp,
end: Timestamp,
span_index: SpanIndex,
outdated_spans: &mut FxHashSet<SpanIndex>,
outdated_spans: &mut OutdatedSpans,
) {
if let Some(tree) = self.self_time_tree.as_mut() {
if Timestamp::from_value(*self.max_self_time_lookup_time.get_mut()) >= start {
......@@ -210,7 +265,7 @@ impl Store {
span_index: SpanIndex,
start: Timestamp,
end: Timestamp,
outdated_spans: &mut FxHashSet<SpanIndex>,
outdated_spans: &mut OutdatedSpans,
) {
let event = SpanEvent::self_time(start, end);
let span = &mut self.spans[span_index.get()];
......@@ -232,7 +287,7 @@ impl Store {
span_index: SpanIndex,
start_time: Timestamp,
total_time: Timestamp,
outdated_spans: &mut FxHashSet<SpanIndex>,
outdated_spans: &mut OutdatedSpans,
) {
let span = SpanRef {
span: &self.spans[span_index.get()],
......@@ -290,7 +345,7 @@ impl Store {
&mut self,
span_index: SpanIndex,
parent: SpanIndex,
outdated_spans: &mut FxHashSet<SpanIndex>,
outdated_spans: &mut OutdatedSpans,
) {
outdated_spans.insert(span_index);
let span = &mut self.spans[span_index.get()];
......@@ -320,7 +375,7 @@ impl Store {
span_index: SpanIndex,
allocation: u64,
count: u64,
outdated_spans: &mut FxHashSet<SpanIndex>,
outdated_spans: &mut OutdatedSpans,
) {
let span = &mut self.spans[span_index.get()];
outdated_spans.insert(span_index);
......@@ -333,7 +388,7 @@ impl Store {
span_index: SpanIndex,
deallocation: u64,
count: u64,
outdated_spans: &mut FxHashSet<SpanIndex>,
outdated_spans: &mut OutdatedSpans,
) {
let span = &mut self.spans[span_index.get()];
outdated_spans.insert(span_index);
......@@ -431,7 +486,7 @@ impl Store {
span.is_complete = true;
}
pub fn invalidate_outdated_spans(&mut self, outdated_spans: &FxHashSet<SpanId>) {
pub fn invalidate_outdated_spans(&mut self, outdated_spans: &OutdatedSpans) {
fn invalidate_span(span: &mut Span) {
span.time_data.end.take();
span.time_data.total_time.take();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment