Skip to content
Merged
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
33 changes: 16 additions & 17 deletions crates/optimism/trie/src/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,15 @@ impl<Tx: DbTx + Sync, S: OpProofsStore + OpProofsInitialStateStore + Send>
/// logging.
fn initialize<
I: Iterator<Item = Result<(Key, Value), DatabaseError>> + InitTable<Key = Key, Value = Value>,
Key: CompletionEstimatable + Clone + 'static,
Value: Clone + 'static,
Key: CompletionEstimatable + 'static,
Value: 'static,
>(
&self,
name: &str,
source: I,
storage_threshold: usize,
log_threshold: usize,
) -> Result<u64, OpProofsStorageError> {
let mut entries = Vec::new();

let mut total_entries: u64 = 0;

info!("Starting {} initialization", name);
let start_time = Instant::now();

Expand All @@ -165,15 +161,16 @@ impl<Tx: DbTx + Sync, S: OpProofsStore + OpProofsInitialStateStore + Send>
};

let storage = &self.storage;
let source_size_hint = source.size_hint().0;
let mut batch = Vec::with_capacity(source_size_hint.min(storage_threshold));
let mut total_entries: usize = 0;

for entry in source {
let entry = entry?;

entries.push(entry.clone());
batch.push(entry?);
total_entries += 1;

if total_entries.is_multiple_of(log_threshold as u64) {
let progress = entry.0.estimate_progress();
if total_entries.is_multiple_of(log_threshold) {
let progress = batch.last().expect("non-empty batch").0.estimate_progress();
let elapsed = start_time.elapsed();
let elapsed_secs = elapsed.as_secs_f64();

Expand All @@ -194,20 +191,22 @@ impl<Tx: DbTx + Sync, S: OpProofsStore + OpProofsInitialStateStore + Send>
);
}

if entries.len() >= storage_threshold {
if batch.len() >= storage_threshold {
info!("Storing {} entries, total entries: {}", name, total_entries);
I::store_entries(storage, entries)?;
entries = Vec::new();
I::store_entries(storage, batch)?;
batch = Vec::with_capacity(
(source_size_hint.saturating_sub(total_entries)).min(storage_threshold),
);
}
}

if !entries.is_empty() {
if !batch.is_empty() {
info!("Storing final {} entries", name);
I::store_entries(storage, entries)?;
I::store_entries(storage, batch)?;
}

info!("{} initialization complete: {} entries", name, total_entries);
Ok(total_entries)
Ok(total_entries as u64)
}

/// Initialize hashed accounts data
Expand Down
Loading