Skip to content

consensus: remove redundant checks in merkle root computation - #22046

Closed
zefir-k wants to merge 1 commit into
bitcoin:masterfrom
zefir-k:master
Closed

zefir-k wants to merge 1 commit into
bitcoin:masterfrom
zefir-k:master

Conversation

@zefir-k

@zefir-k zefir-k commented May 24, 2021

Copy link
Copy Markdown

In ComputeMerkleRoot, the mutated flag is only set to
true but never becomes false thereafter. Therefore
all following tests after it became true are wasted
computation. Fix this by doing the tests only while
the flag is false.

While at that, drop the temporary local variable and
use the provided parameter in place.

Signed-off-by: Zefir Kurtisi [email protected]

@practicalswift

practicalswift commented May 24, 2021 •

Copy link
Copy Markdown
Contributor

Personally I find the existing version easier to reason about. I think I'd prefer leaving this as-is, especially considering that this is consensus critical code :)

@promag promag left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you compare the benchmark before and after?

Comment thread src/consensus/merkle.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just && !mutation here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two optimizations included here:

  1. stop wasting computation time once mutated gets set
  2. use the parameter value directly instead of local copy

While the second saves one compare+assign operation and might be insignificant, the first effectively saves useless processing as the result is already known.

To me it is not only optimization but causing a trigger that something was forgotten when reading the code.

@practicalswift : would you support @promag's suggestion to limit the proposal to include only the first part, which would limit the change to one line and gain the most potential savings, or do you oppose the change generally?

@maflcko

maflcko commented May 25, 2021

Copy link
Copy Markdown
Member

Does this improve IBD speed significantly? Otherwise it might not be worth it the review effort.

In ComputeMerkleRoot, the mutated flag is only set to
true but never becomes false thereafter. Therefore
all following tests after it became true are wasted
computation. Fix this by doing the tests only while
the flag is false.

Signed-off-by: Zefir Kurtisi <[email protected]>
@zefir-k

zefir-k commented May 25, 2021

Copy link
Copy Markdown
Author

Does this improve IBD speed significantly? Otherwise it might not be worth it the review effort.

I reduced the change to the relevant (one-line) part, which should be trivial to review.

Maybe the overall speed improvement is not significant, but the local is obvious: if the rightest-most bottom-leaf is a copy, the flag becomes true immediately, but the (then waste) checks continue for every next level up to the root.

As kernel developer I might be more waste sensitive than required here, so if you think it is not worth it, I'm fine with closing - and sorry for noise.

@promag promag left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see significant gains. I've even tried other tweaks with the same result:

tweak 1 - break loop if more iterations aren't needed

                  if (hashes[pos] == hashes[pos + 1]) {
                      mutation = true;
                      break;
                  }

tweak 2: - check mutated once:

    if (mutated) {
        while (hashes.size() > 1) {
            // …
        }
    } else { 
        while (hashes.size() > 1) {
            // …
        }
    }

@zefir-k

zefir-k commented May 25, 2021

Copy link
Copy Markdown
Author

I don't see significant gains. I've even tried other tweaks with the same result:

Thanks, the hashing cost in fact is so dominant that the comparison operation remains below measurement accuracy:

|             ns/leaf |              leaf/s |    err% |     total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
|               60.88 |       16,424,775.25 |    0.1% |      0.04 | `MerkleRoot`
|               60.30 |       16,584,351.40 |    0.0% |      0.04 | `MerkleRoot_no_mutation`
|               60.71 |       16,471,608.02 |    0.1% |      0.04 | `MerkleRoot_patch`

... not worth it.

@zefir-k zefir-k closed this May 25, 2021
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants