Skip to content

Commit b6422f7

Browse files
authored
Merge pull request #1274 from evanlinjin/avoid_btreemap_append
Avoid using `BTreeMap::append`
2 parents cd60243 + eb1714a commit b6422f7

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

crates/chain/src/keychain.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ impl<K: Ord> Append for ChangeSet<K> {
5858
*index = other_index.max(*index);
5959
}
6060
});
61-
62-
self.0.append(&mut other.0);
61+
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
62+
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
63+
self.0.extend(other.0);
6364
}
6465

6566
/// Returns whether the changeset are empty.

crates/chain/src/tx_data_traits.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ pub trait Append {
123123
}
124124

125125
impl<K: Ord, V> Append for BTreeMap<K, V> {
126-
fn append(&mut self, mut other: Self) {
127-
BTreeMap::append(self, &mut other)
126+
fn append(&mut self, other: Self) {
127+
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
128+
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
129+
BTreeMap::extend(self, other)
128130
}
129131

130132
fn is_empty(&self) -> bool {
@@ -133,8 +135,10 @@ impl<K: Ord, V> Append for BTreeMap<K, V> {
133135
}
134136

135137
impl<T: Ord> Append for BTreeSet<T> {
136-
fn append(&mut self, mut other: Self) {
137-
BTreeSet::append(self, &mut other)
138+
fn append(&mut self, other: Self) {
139+
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
140+
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
141+
BTreeSet::extend(self, other)
138142
}
139143

140144
fn is_empty(&self) -> bool {

crates/chain/src/tx_graph.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,10 +1271,12 @@ impl<A> ChangeSet<A> {
12711271
}
12721272

12731273
impl<A: Ord> Append for ChangeSet<A> {
1274-
fn append(&mut self, mut other: Self) {
1275-
self.txs.append(&mut other.txs);
1276-
self.txouts.append(&mut other.txouts);
1277-
self.anchors.append(&mut other.anchors);
1274+
fn append(&mut self, other: Self) {
1275+
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
1276+
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
1277+
self.txs.extend(other.txs);
1278+
self.txouts.extend(other.txouts);
1279+
self.anchors.extend(other.anchors);
12781280

12791281
// last_seen timestamps should only increase
12801282
self.last_seen.extend(

0 commit comments

Comments
 (0)