From e395d9bd1581d055b49d9fc185be2aca2237ebf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Sun, 7 Jan 2024 14:39:31 +0800 Subject: [PATCH] fix(chain): avoid using `BTreeMap::append` The implementation of `BTreeMap::append` is non-performant making merging changesets very slow. We use `Extend::extend` instead. Refer to: https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420 --- crates/chain/src/keychain.rs | 5 +++-- crates/chain/src/tx_data_traits.rs | 12 ++++++++---- crates/chain/src/tx_graph.rs | 10 ++++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/crates/chain/src/keychain.rs b/crates/chain/src/keychain.rs index 63972a0ade..d443ee440b 100644 --- a/crates/chain/src/keychain.rs +++ b/crates/chain/src/keychain.rs @@ -58,8 +58,9 @@ impl Append for ChangeSet { *index = other_index.max(*index); } }); - - self.0.append(&mut other.0); + // We use `extend` instead of `BTreeMap::append` due to performance issues with `append`. + // Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420 + self.0.extend(other.0); } /// Returns whether the changeset are empty. diff --git a/crates/chain/src/tx_data_traits.rs b/crates/chain/src/tx_data_traits.rs index 5c854a9569..8fa17ff904 100644 --- a/crates/chain/src/tx_data_traits.rs +++ b/crates/chain/src/tx_data_traits.rs @@ -123,8 +123,10 @@ pub trait Append { } impl Append for BTreeMap { - fn append(&mut self, mut other: Self) { - BTreeMap::append(self, &mut other) + fn append(&mut self, other: Self) { + // We use `extend` instead of `BTreeMap::append` due to performance issues with `append`. + // Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420 + BTreeMap::extend(self, other) } fn is_empty(&self) -> bool { @@ -133,8 +135,10 @@ impl Append for BTreeMap { } impl Append for BTreeSet { - fn append(&mut self, mut other: Self) { - BTreeSet::append(self, &mut other) + fn append(&mut self, other: Self) { + // We use `extend` instead of `BTreeMap::append` due to performance issues with `append`. + // Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420 + BTreeSet::extend(self, other) } fn is_empty(&self) -> bool { diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index ef15d7e7d8..d43c160d75 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -1271,10 +1271,12 @@ impl ChangeSet { } impl Append for ChangeSet { - fn append(&mut self, mut other: Self) { - self.txs.append(&mut other.txs); - self.txouts.append(&mut other.txouts); - self.anchors.append(&mut other.anchors); + fn append(&mut self, other: Self) { + // We use `extend` instead of `BTreeMap::append` due to performance issues with `append`. + // Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420 + self.txs.extend(other.txs); + self.txouts.extend(other.txouts); + self.anchors.extend(other.anchors); // last_seen timestamps should only increase self.last_seen.extend(