Skip to content

Commit 4568c22

Browse files
committed
Use Vec::with_capacity where applicable, and pass owned PendingLogEntry to avoid clone
1 parent 2470f8d commit 4568c22

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

crates/ct_worker/src/ctlog.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ impl PoolState {
105105
}
106106
}
107107
// Add a new entry to the pool.
108-
fn add(&mut self, key: LookupKey, entry: &PendingLogEntry) -> AddLeafResult {
108+
fn add(&mut self, key: LookupKey, entry: PendingLogEntry) -> AddLeafResult {
109109
if self.pending_entries.len() >= MAX_POOL_SIZE {
110110
return AddLeafResult::RateLimited;
111111
}
112112
let (tx, rx) = channel((0, 0));
113-
self.pending_entries.push((entry.clone(), tx));
113+
self.pending_entries.push((entry, tx));
114114
self.pending.insert(key, rx.clone());
115115

116116
AddLeafResult::Pending {
@@ -388,7 +388,7 @@ pub(crate) enum PendingSource {
388388
pub(crate) fn add_leaf_to_pool(
389389
state: &mut PoolState,
390390
cache: &impl CacheRead,
391-
entry: &PendingLogEntry,
391+
entry: PendingLogEntry,
392392
) -> AddLeafResult {
393393
let hash = entry.lookup_key();
394394

@@ -546,8 +546,8 @@ async fn sequence_entries(
546546
}
547547
let mut overlay = HashMap::new();
548548
let mut n = old_size;
549-
let mut sequenced_entries: Vec<LogEntry> = Vec::new();
550-
let mut sequenced_metadata = Vec::new();
549+
let mut sequenced_entries: Vec<LogEntry> = Vec::with_capacity(entries.len());
550+
let mut sequenced_metadata = Vec::with_capacity(entries.len());
551551

552552
for (entry, sender) in entries {
553553
let sequenced_entry = LogEntry {
@@ -917,7 +917,7 @@ async fn read_and_verify_tiles(
917917
}
918918

919919
// Fetch all the tile data.
920-
let mut data = Vec::new();
920+
let mut data = Vec::with_capacity(tiles.len());
921921
for tile in &tiles {
922922
let result = object
923923
.fetch(&tile.path())
@@ -1124,7 +1124,7 @@ mod tests {
11241124
certificate,
11251125
..Default::default()
11261126
};
1127-
add_leaf_to_pool(&mut log.pool_state, &log.cache, &leaf);
1127+
add_leaf_to_pool(&mut log.pool_state, &log.cache, leaf);
11281128
}
11291129
log.sequence().unwrap();
11301130
}
@@ -1914,7 +1914,7 @@ mod tests {
19141914

19151915
block_on(upload_issuers(&self.object, issuers, &self.config.name)).unwrap();
19161916

1917-
add_leaf_to_pool(&mut self.pool_state, &self.cache, &leaf)
1917+
add_leaf_to_pool(&mut self.pool_state, &self.cache, leaf)
19181918
}
19191919

19201920
fn check(&self, size: u64) -> u64 {
@@ -2034,7 +2034,7 @@ mod tests {
20342034
let (tiles_with_bytes, index_tile_order) =
20352035
block_on(read_and_verify_tiles(object, tree_size, tree_hash, indexes))?;
20362036

2037-
let mut hashes = Vec::new();
2037+
let mut hashes = Vec::with_capacity(indexes.len());
20382038
for (i, &x) in indexes.iter().enumerate() {
20392039
let j = index_tile_order[i];
20402040
let h = tiles_with_bytes[j]

crates/ct_worker/src/sequencer_do.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl DurableObject for Sequencer {
8282
"/add_batch" => {
8383
endpoint = "add_batch";
8484
let pending_entries: Vec<PendingLogEntry> = req.json().await?;
85-
self.add_batch(&pending_entries).await
85+
self.add_batch(pending_entries).await
8686
}
8787
_ => {
8888
endpoint = "unknown";
@@ -220,15 +220,17 @@ impl Sequencer {
220220
// Add a batch of entries, returning a Response with metadata for
221221
// successfully sequenced entries. Entries that fail to be added (e.g., due to rate limiting)
222222
// are omitted.
223-
async fn add_batch(&mut self, pending_entries: &[PendingLogEntry]) -> Result<Response> {
223+
async fn add_batch(&mut self, pending_entries: Vec<PendingLogEntry>) -> Result<Response> {
224224
// Safe to unwrap config here as the log must be initialized.
225225
let mut futures = Vec::with_capacity(pending_entries.len());
226+
let mut lookup_keys = Vec::with_capacity(pending_entries.len());
226227
for pending_entry in pending_entries {
227228
let typ = if pending_entry.is_precert {
228229
"add-pre-chain"
229230
} else {
230231
"add-chain"
231232
};
233+
lookup_keys.push(pending_entry.lookup_key());
232234

233235
let add_leaf_result = ctlog::add_leaf_to_pool(
234236
&mut self.pool_state,
@@ -243,14 +245,14 @@ impl Sequencer {
243245

244246
futures.push(add_leaf_result.resolve());
245247
}
246-
let cache_values = join_all(futures).await;
248+
let entries_metadata = join_all(futures).await;
247249

248250
// Zip the cache keys with the cache values, filtering out entries that
249251
// were not sequenced (e.g., due to rate limiting).
250-
let result = pending_entries
252+
let result = lookup_keys
251253
.iter()
252-
.zip(cache_values.iter())
253-
.filter_map(|(entry, value)| value.as_ref().map(|v| (entry.lookup_key(), v)))
254+
.zip(entries_metadata.iter())
255+
.filter_map(|(key, value_opt)| value_opt.as_ref().map(|metadata| (key, metadata)))
254256
.collect::<Vec<_>>();
255257

256258
Response::from_json(&result)

0 commit comments

Comments
 (0)