Skip to content

Commit 92dc9b3

Browse files
committed
perf(batcher): do not serialize to cbor on batch size calculation
1 parent 95e99c2 commit 92dc9b3

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

crates/batcher/src/types/batch_queue.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use aligned_sdk::{
33
constants::CBOR_ARRAY_MAX_OVERHEAD,
44
types::{NoncedVerificationData, VerificationDataCommitment},
55
},
6-
communication::serialization::cbor_serialize,
76
};
87
use ethers::types::{Address, Signature, U256};
98
use priority_queue::PriorityQueue;
@@ -124,14 +123,9 @@ pub(crate) type BatchQueue = PriorityQueue<BatchQueueEntry, BatchQueueEntryPrior
124123
/// Calculates the size of the batch represented by the given batch queue.
125124
pub(crate) fn calculate_batch_size(batch_queue: &BatchQueue) -> Result<usize, BatcherError> {
126125
let folded_result = batch_queue.iter().try_fold(0, |acc, (entry, _)| {
127-
if let Ok(verification_data_bytes) =
128-
cbor_serialize(&entry.nonced_verification_data.verification_data)
129-
{
130-
let current_batch_size = acc + verification_data_bytes.len();
131-
ControlFlow::Continue(current_batch_size)
132-
} else {
133-
ControlFlow::Break(())
134-
}
126+
let verification_data_size = entry.nonced_verification_data.cbor_size_upper_bound();
127+
let current_batch_size = acc + verification_data_size;
128+
ControlFlow::Continue(current_batch_size)
135129
});
136130

137131
if let ControlFlow::Continue(batch_size) = folded_result {
@@ -178,10 +172,7 @@ pub(crate) fn extract_batch_directly(
178172
let (rejected_entry, rejected_priority) = batch_queue.pop().unwrap();
179173

180174
// Update batch size
181-
let verification_data_size =
182-
cbor_serialize(&rejected_entry.nonced_verification_data.verification_data)
183-
.unwrap()
184-
.len();
175+
let verification_data_size = rejected_entry.nonced_verification_data.cbor_size_upper_bound();
185176
batch_size -= verification_data_size;
186177

187178
rejected_entries.push((rejected_entry, rejected_priority));

crates/sdk/src/common/types.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub struct VerificationData {
7272
pub proof_generator_addr: Address,
7373
}
7474

75+
7576
#[derive(Debug, Serialize, Deserialize, Clone)]
7677
pub struct NoncedVerificationData {
7778
pub verification_data: VerificationData,
@@ -97,6 +98,34 @@ impl NoncedVerificationData {
9798
payment_service_addr,
9899
}
99100
}
101+
102+
/// Returns an upper bound for the CBOR encoding size without performing serialization.
103+
/// Sums the length of all Vec<u8> fields in the inner VerificationData and adds 256 bytes as overhead.
104+
pub fn cbor_size_upper_bound(&self) -> usize {
105+
const CBOR_OVERHEAD_BYTES: usize = 256;
106+
let mut total_size = 0;
107+
108+
// Add length of proof Vec<u8>
109+
total_size += self.verification_data.proof.len();
110+
111+
// Add length of pub_input Option<Vec<u8>>
112+
if let Some(ref pub_input) = self.verification_data.pub_input {
113+
total_size += pub_input.len();
114+
}
115+
116+
// Add length of verification_key Option<Vec<u8>>
117+
if let Some(ref verification_key) = self.verification_data.verification_key {
118+
total_size += verification_key.len();
119+
}
120+
121+
// Add length of vm_program_code Option<Vec<u8>>
122+
if let Some(ref vm_program_code) = self.verification_data.vm_program_code {
123+
total_size += vm_program_code.len();
124+
}
125+
126+
// Add overhead bytes for the full NoncedVerificationData structure
127+
total_size + CBOR_OVERHEAD_BYTES
128+
}
100129
}
101130

102131
// Defines a price estimate type for the user.

0 commit comments

Comments
 (0)