Skip to content

Commit

Permalink
OnceCell on Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
smklein committed Dec 13, 2023
1 parent 879468b commit d348f3a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions analyzeme/src/profiling_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,7 @@ use std::{error::Error, path::PathBuf};
#[derive(Debug)]
pub struct ProfilingData {
event_decoder: Box<dyn EventDecoder>,
metadata: OnceCell<Metadata>,
}

impl ProfilingData {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -302,6 +308,7 @@ impl ProfilingDataBuilder {
)
.unwrap(),
),
metadata: OnceCell::new(),
}
}

Expand Down

0 comments on commit d348f3a

Please sign in to comment.