Skip to content

Conversation

sipa
Copy link
Member

@sipa sipa commented Jun 8, 2020

It seems SHA256AutoDetect() was not being called in benchmarks, making the numbers only reflect the naive implementation. Fix this by calling it in bench_bitcoin's setup.

@pstratem
Copy link
Contributor

pstratem commented Jun 8, 2020

comparing this to current master this seems to actually be slower (if only very slightly), running debian 10.4 on an i7-8550U

bench_master.txt:SHA256, 5, 340, 4.45805, 0.0026092, 0.00263955, 0.00262045
bench_master.txt:SHA256D64_1024, 5, 7400, 4.45677, 0.000120143, 0.000120585, 0.000120541
bench_master.txt:SHA256_32b, 5, 4700000, 4.68355, 1.98788e-07, 1.99912e-07, 1.99391e-07
bench_19214.txt:SHA256, 5, 340, 4.57454, 0.00268855, 0.00269248, 0.00269149
bench_19214.txt:SHA256D64_1024, 5, 7400, 4.55315, 0.000122395, 0.000123309, 0.000123203
bench_19214.txt:SHA256_32b, 5, 4700000, 4.97084, 2.09111e-07, 2.13025e-07, 2.11958e-07

@fjahr
Copy link
Contributor

fjahr commented Jun 8, 2020

tested ACK addf18d

For me, the SHA256 tests are speeding up significantly after this.

@pstratem
Copy link
Contributor

pstratem commented Jun 8, 2020

I must have gotten something wrong, doing the benchmarks again after git clean shows this pr being about 6x faster

ACK addf18d

@maflcko
Copy link
Member

maflcko commented Jun 9, 2020

Can hashing be made to fail when SHA256AutoDetect hasn't been called?

@Sjors
Copy link
Member

Sjors commented Jun 9, 2020

On a 2019 Macbook Pro:

src/bench/bench_bitcoin -filter=SHA256.*

Before:

# Benchmark, evals, iterations, total, min, max, median
SHA256, 5, 340, 6.08992, 0.00350198, 0.00370616, 0.0035939
SHA256D64_1024, 5, 7400, 22.9059, 0.000614125, 0.00062785, 0.000618134
SHA256_32b, 5, 4700000, 6.0593, 2.55725e-07, 2.59171e-07, 2.58255e-07

After (addf18d):

# Benchmark, evals, iterations, total, min, max, median
SHA256, 5, 340, 4.12459, 0.00240666, 0.00244616, 0.00242406
SHA256D64_1024, 5, 7400, 3.56757, 9.53814e-05, 9.75219e-05, 9.61168e-05
SHA256_32b, 5, 4700000, 4.29699, 1.76951e-07, 1.91434e-07, 1.80197e-07

@laanwj
Copy link
Member

laanwj commented Jun 9, 2020

# Benchmark, evals, iterations, total, min, max, median
SHA256, …
SHA256D64_1024, …
SHA256_32b, …

Maybe it would be useful to specify here what SHA256 implementation is benchmarked. This makes comparisons slightly more meaningful.

@maflcko
Copy link
Member

maflcko commented Jun 10, 2020

From IRC:

[16:51] <phantomcircuit> sipa, oh do any of the other benchmarks maybe end up calling something that would call the auto detect?

If another benchmark spins up a testing setup, that testing setup will call auto detect. So this explains the confusing results where the naive implementation is faster than avx2.

@luke-jr
Copy link
Member

luke-jr commented Jun 11, 2020

There should probably be a way to force a specific implementation?

(I think always defaulting to the generic implementation makes sense...)

@maflcko
Copy link
Member

maflcko commented Jun 11, 2020

In the functional tests we use

if self.is_foobar_compiled():
  self.test_foobar()

Something along those lines could also be used to bench the different hash impls. here.

@laanwj
Copy link
Member

laanwj commented Jun 11, 2020

Yes, benchmarking all the different SHA356 implementations could be useful as well. I think this was the case in one of the initial PRs that introduced more of them.

That said that still leaves open what to do for other benchmarks that might depend on the SHA256 implementation. We don't want to re-run all the benchmarks for all the supported implementations ofc.

@maflcko
Copy link
Member

maflcko commented Jun 11, 2020

That said that still leaves open what to do for other benchmarks that might depend on the SHA256 implementation. We don't want to re-run all the benchmarks for all the supported implementations ofc.

I generally don't like using globals to magically change control flow, especially in tests. There have been enough cases in the past where global state in tests has lead to confusing results. (Including this very benchmark: #19214 (comment))

Which is why I suggested to force a decision before hashing is used: #19214 (comment) The silent fallback shouldn't be needed, or am I missing something obvious?

@laanwj
Copy link
Member

laanwj commented Jun 16, 2020

The silent fallback shouldn't be needed, or am I missing something obvious?

I think there's something of an initialization order issue here. Some of the objects initialized before main() might make (light, non-performance-critical) use of SHA256 to do initialization. We don't want to move the processor detection that soon due to logging / potential failure modes.

@laanwj
Copy link
Member

laanwj commented Jul 15, 2020

ACK addf18d
I'm going to merge this, It has enough ACKs and I think this is a clear improvement to before. Additional suggestions can be done in later PRs.

@laanwj laanwj merged commit 7ebc365 into bitcoin:master Jul 15, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 16, 2020
addf18d Call SHA256AutoDetect in benchmark setup (Pieter Wuille)

Pull request description:

  It seems `SHA256AutoDetect()` was not being called in benchmarks, making the numbers only reflect the naive implementation. Fix this by calling it in bench_bitcoin's setup.

ACKs for top commit:
  fjahr:
    tested ACK addf18d
  pstratem:
    ACK addf18d
  laanwj:
    ACK addf18d

Tree-SHA512: 3ba4b068145942df1429bf5913e3f685511e6ebeae2c1a3f9b8ac0144f6db1c7df456f88f480a2129f3e1602e3bf6a39530bb96e2c74c03ddb19324cec6799c7
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 1, 2021
Summary:
> It seems SHA256AutoDetect() was not being called in benchmarks, making the numbers only reflect the naive implementation. Fix this by calling it in bench_bitcoin's setup.

This is a backport of [[bitcoin/bitcoin#19214 | core#19214]]

Test Plan:
`ninja bench-bitcoin`

I don't see a significant difference in the SHA256 becnhmarks before or
after this commit.

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D10004
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants