Skip to content

Commit

Permalink
Remove BTreeSet from canonicalization sorting for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxxep committed Nov 26, 2024
1 parent 3d5c4e8 commit 5b28f9b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ciborium/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,11 @@ macro_rules! end_map {
keys.len(), values.len(),
"ciborium error: canonicalization failed, different number of keys and values?");

let pairs: std::collections::BTreeMap<_, _> =
let mut pairs: Vec<_> =
keys.iter().zip(values.iter()).collect();

pairs.sort();

for (key, value) in pairs.iter() {
self.serializer.encoder.write_all(&key)?;
self.serializer.encoder.write_all(&value)?;
Expand All @@ -408,12 +410,14 @@ macro_rules! end_map {
keys.len(), values.len(),
"ciborium error: canonicalization failed, different number of keys and values?");

let pairs: std::collections::BTreeMap<_, _> =
let mut pairs: Vec<_> =
keys.iter()
.map(|key| (key.len(), key)) // length-first ordering
.zip(values.iter())
.collect();

pairs.sort();

for ((_, key), value) in pairs.iter() {
self.serializer.encoder.write_all(&key)?;
self.serializer.encoder.write_all(&value)?;
Expand Down

0 comments on commit 5b28f9b

Please sign in to comment.