From f81708ac8046681dbadb74c6c0d29fa2a3f70edc Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 13 Jul 2026 05:10:41 +0000 Subject: [PATCH 1/2] starknet_os: preallocate decompress result vectors to avoid reallocation `decompress` runs once per block over the full state diff. Both `unique_values` and `result` grow via push/extend from `Vec::new()` even though their final lengths are already known from the decoded header (sum of unique-value bucket lengths, and data_len, respectively), causing avoidable reallocations for large state diffs. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01YHx5w4Xp5DBX5oBBTTFEvt --- .../hints/hint_implementation/stateless_compression/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs b/crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs index 52c9a949eb2..1644f04bce3 100644 --- a/crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs +++ b/crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs @@ -575,7 +575,7 @@ pub fn decompress(compressed: &mut impl Iterator) -> Vec { let unique_value_bucket_lengths: Vec = header[2..2 + N_UNIQUE_BUCKETS].to_vec(); let n_repeating_values = &header[2 + N_UNIQUE_BUCKETS]; - let mut unique_values = Vec::new(); + let mut unique_values = Vec::with_capacity(unique_value_bucket_lengths.iter().sum()); unique_values.extend(compressed.take(unique_value_bucket_lengths[0])); // 252 bucket. unique_values.extend(unpack_chunk::<125>(compressed, unique_value_bucket_lengths[1])); unique_values.extend(unpack_chunk::<83>(compressed, unique_value_bucket_lengths[2])); @@ -605,7 +605,7 @@ pub fn decompress(compressed: &mut impl Iterator) -> Vec { let mut bucket_offset_trackers: Vec<_> = bucket_offsets; - let mut result = Vec::new(); + let mut result = Vec::with_capacity(*data_len); for bucket_index in bucket_index_per_elm { let offset = &mut bucket_offset_trackers[bucket_index]; let value = all_values[*offset]; From 5a9a4193a24bb3d42300c71be92daaa7ef11d6c0 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Aug 2026 14:57:10 +0000 Subject: [PATCH 2/2] starknet_os: preallocate CompressionSet vectors to avoid reallocation Follow-up to #14779, which preallocated the two Vecs in decompress(). The sibling compress() path (CompressionSet::new, invoked once per block via the compression_hint Cairo VM hint) has the same pattern: bucket_index_per_elm and repeating_value_bucket grow from Vec::new() via push in a per-value loop, even though both are bounded by values.len() before the loop starts. --- .../hints/hint_implementation/stateless_compression/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs b/crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs index 1644f04bce3..fc4ac096315 100644 --- a/crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs +++ b/crates/starknet_os/src/hints/hint_implementation/stateless_compression/utils.rs @@ -360,8 +360,8 @@ impl CompressionSet { pub fn new(values: &[Felt]) -> Self { let mut obj = Self { unique_value_buckets: Buckets::new(), - repeating_value_bucket: Vec::new(), - bucket_index_per_elm: Vec::new(), + repeating_value_bucket: Vec::with_capacity(values.len()), + bucket_index_per_elm: Vec::with_capacity(values.len()), }; let repeating_values_bucket_index = N_UNIQUE_BUCKETS;