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

Skip cache invalidation for complete traces

Synchronous WASM loads cannot populate lazy query caches while parsing, but every 2 MiB batch still walked changed spans and ancestors to invalidate them. Preserve invalidation for the live native reader and skip the empty walks for complete inputs, reducing this trace from 39.2s to 28.5s.
Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent f203b8f8
...@@ -113,7 +113,7 @@ fn read_trace_bytes_impl( ...@@ -113,7 +113,7 @@ fn read_trace_bytes_impl(
} }
let store = Arc::new(StoreContainer::new()); let store = Arc::new(StoreContainer::new());
let mut parser = TraceParser::new(store.clone()); let mut parser = TraceParser::new_complete(store.clone());
let mut uncompressed_bytes_read = 0; let mut uncompressed_bytes_read = 0;
if bytes.starts_with(GZIP_MAGIC) { if bytes.starts_with(GZIP_MAGIC) {
......
...@@ -103,6 +103,7 @@ enum InternalRowType { ...@@ -103,6 +103,7 @@ enum InternalRowType {
pub struct TurbopackFormat { pub struct TurbopackFormat {
store: Arc<StoreContainer>, store: Arc<StoreContainer>,
invalidate_caches: bool,
id_mapping: IdMapping, id_mapping: IdMapping,
dropped_ids: FxHashSet<u64>, dropped_ids: FxHashSet<u64>,
remaining_ids_to_drop: usize, remaining_ids_to_drop: usize,
...@@ -115,13 +116,14 @@ pub struct TurbopackFormat { ...@@ -115,13 +116,14 @@ pub struct TurbopackFormat {
} }
impl TurbopackFormat { impl TurbopackFormat {
pub fn new(store: Arc<StoreContainer>) -> Self { pub fn new(store: Arc<StoreContainer>, invalidate_caches: bool) -> Self {
let drop_ids = std::env::var("DROP_SPANS") let drop_ids = std::env::var("DROP_SPANS")
.ok() .ok()
.and_then(|v| v.parse::<usize>().ok()) .and_then(|v| v.parse::<usize>().ok())
.unwrap_or_default(); .unwrap_or_default();
Self { Self {
store, store,
invalidate_caches,
id_mapping: IdMapping::default(), id_mapping: IdMapping::default(),
dropped_ids: FxHashSet::with_capacity_and_hasher(drop_ids, Default::default()), dropped_ids: FxHashSet::with_capacity_and_hasher(drop_ids, Default::default()),
remaining_ids_to_drop: drop_ids, remaining_ids_to_drop: drop_ids,
...@@ -555,7 +557,9 @@ impl TraceFormat for TurbopackFormat { ...@@ -555,7 +557,9 @@ impl TraceFormat for TurbopackFormat {
for row in iter.by_ref() { for row in iter.by_ref() {
self.process(&mut store, row); self.process(&mut store, row);
} }
store.invalidate_outdated_spans(&self.outdated_spans); if self.invalidate_caches {
store.invalidate_outdated_spans(&self.outdated_spans);
}
self.outdated_spans.clear(); self.outdated_spans.clear();
} }
} }
......
...@@ -75,6 +75,7 @@ impl ObjectSafeTraceFormat for ErasedTraceFormat { ...@@ -75,6 +75,7 @@ impl ObjectSafeTraceFormat for ErasedTraceFormat {
/// the caller so the same parser can be used by native file readers and WASM. /// the caller so the same parser can be used by native file readers and WASM.
pub struct TraceParser { pub struct TraceParser {
store: Arc<StoreContainer>, store: Arc<StoreContainer>,
invalidate_caches: bool,
format: Option<(ErasedTraceFormat, ErasedReused)>, format: Option<(ErasedTraceFormat, ErasedReused)>,
buffer: Vec<u8>, buffer: Vec<u8>,
consumed: usize, consumed: usize,
...@@ -82,8 +83,18 @@ pub struct TraceParser { ...@@ -82,8 +83,18 @@ pub struct TraceParser {
impl TraceParser { impl TraceParser {
pub fn new(store: Arc<StoreContainer>) -> Self { pub fn new(store: Arc<StoreContainer>) -> Self {
Self::new_with_cache_invalidation(store, true)
}
#[allow(dead_code, reason = "unused by the native binary's streaming reader")]
pub(crate) fn new_complete(store: Arc<StoreContainer>) -> Self {
Self::new_with_cache_invalidation(store, false)
}
fn new_with_cache_invalidation(store: Arc<StoreContainer>, invalidate_caches: bool) -> Self {
Self { Self {
store, store,
invalidate_caches,
format: None, format: None,
buffer: Vec::new(), buffer: Vec::new(),
consumed: 0, consumed: 0,
...@@ -100,7 +111,10 @@ impl TraceParser { ...@@ -100,7 +111,10 @@ impl TraceParser {
if self.format.is_none() && self.buffer.len() >= 8 { if self.format.is_none() && self.buffer.len() >= 8 {
let erased_format = if self.buffer.starts_with(b"TRACEv0") { let erased_format = if self.buffer.starts_with(b"TRACEv0") {
self.consumed = 7; self.consumed = 7;
ErasedTraceFormat(Box::new(TurbopackFormat::new(self.store.clone()))) ErasedTraceFormat(Box::new(TurbopackFormat::new(
self.store.clone(),
self.invalidate_caches,
)))
} else if self.buffer.starts_with(b"[{\"name\"") { } else if self.buffer.starts_with(b"[{\"name\"") {
ErasedTraceFormat(Box::new(NextJsFormat::new(self.store.clone()))) ErasedTraceFormat(Box::new(NextJsFormat::new(self.store.clone())))
} else if self.buffer.starts_with(b"v ") { } else if self.buffer.starts_with(b"v ") {
...@@ -108,7 +122,10 @@ impl TraceParser { ...@@ -108,7 +122,10 @@ impl TraceParser {
} else { } else {
// Preserve the native reader's compatibility with old // Preserve the native reader's compatibility with old
// Turbopack traces that predate the magic header. // Turbopack traces that predate the magic header.
ErasedTraceFormat(Box::new(TurbopackFormat::new(self.store.clone()))) ErasedTraceFormat(Box::new(TurbopackFormat::new(
self.store.clone(),
self.invalidate_caches,
)))
}; };
let reuse = erased_format.create_reused(); let reuse = erased_format.create_reused();
self.format = Some((erased_format, reuse)); self.format = Some((erased_format, reuse));
......
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