Avoid expensive cryptographic tree root recalculations in eq() (#7386)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
teor 2023-08-26 11:45:46 +10:00 committed by GitHub
parent ca8d529a09
commit 67e3c26190
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

@ -428,7 +428,13 @@ impl Eq for NoteCommitmentTree {}
impl PartialEq for NoteCommitmentTree {
fn eq(&self, other: &Self) -> bool {
self.hash() == other.hash()
if let (Some(root), Some(other_root)) = (self.cached_root(), other.cached_root()) {
// Use cached roots if available
root == other_root
} else {
// Avoid expensive root recalculations which use multiple cryptographic hashes
self.inner == other.inner
}
}
}

View File

@ -423,7 +423,13 @@ impl Eq for NoteCommitmentTree {}
impl PartialEq for NoteCommitmentTree {
fn eq(&self, other: &Self) -> bool {
self.hash() == other.hash()
if let (Some(root), Some(other_root)) = (self.cached_root(), other.cached_root()) {
// Use cached roots if available
root == other_root
} else {
// Avoid expensive root recalculations which use multiple cryptographic hashes
self.inner == other.inner
}
}
}

View File

@ -377,7 +377,13 @@ impl Eq for NoteCommitmentTree {}
impl PartialEq for NoteCommitmentTree {
fn eq(&self, other: &Self) -> bool {
self.hash() == other.hash()
if let (Some(root), Some(other_root)) = (self.cached_root(), other.cached_root()) {
// Use cached roots if available
root == other_root
} else {
// Avoid expensive root recalculations which use multiple cryptographic hashes
self.inner == other.inner
}
}
}