diff --git a/analyzeme/src/profiling_data.rs b/analyzeme/src/profiling_data.rs index 02ec91e..a3b4c59 100644 --- a/analyzeme/src/profiling_data.rs +++ b/analyzeme/src/profiling_data.rs @@ -7,6 +7,7 @@ use measureme::file_header::{ use measureme::{ EventId, PageTag, RawEvent, SerializationSink, SerializationSinkBuilder, StringTableBuilder, }; +use std::cell::OnceCell; use std::fs; use std::path::Path; use std::sync::Arc; @@ -15,6 +16,7 @@ use std::{error::Error, path::PathBuf}; #[derive(Debug)] pub struct ProfilingData { event_decoder: Box, + metadata: OnceCell, } impl ProfilingData { @@ -84,11 +86,15 @@ impl ProfilingData { } }; - Ok(ProfilingData { event_decoder }) + Ok(ProfilingData { + event_decoder, + metadata: OnceCell::new(), + }) } - pub fn metadata(&self) -> Metadata { - self.event_decoder.metadata() + pub fn metadata(&self) -> &Metadata { + // Cache the metadata during the first access + self.metadata.get_or_init(|| self.event_decoder.metadata()) } pub fn iter<'a>(&'a self) -> ProfilerEventIterator<'a> { @@ -302,6 +308,7 @@ impl ProfilingDataBuilder { ) .unwrap(), ), + metadata: OnceCell::new(), } }