forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoin#30396: random: add benchmarks and drop unnecessary Shuf…
…fle function 6ecda04 random: drop ad-hoc Shuffle in favor of std::shuffle (Pieter Wuille) da28a26 bench random: benchmark more functions, and add InsecureRandomContext (Pieter Wuille) 0a9bbc6 random bench refactor: move to new bench/random.cpp (Pieter Wuille) Pull request description: This adds benchmarks for various operations on `FastRandomContext` and `InsecureRandomContext`, and then removes the ad-hoc `Shuffle` functions, now that it appears that standard library `std::shuffle` has comparable performance. The other reason for keeping `Shuffle`, namely the fact that libstdc++ used self-move (which debug mode panics on) has been fixed as well (see bitcoin#29625 (comment)). ACKs for top commit: achow101: ACK 6ecda04 hodlinator: ACK 6ecda04 dergoegge: Code review ACK 6ecda04 Tree-SHA512: 2560b7312410581ff2b9bd0716e0f1558d910b5eadb9544785c972384985ac0f11f72d6b2797cfe2e7eb71fa57c30cffd98cc009cb4ee87a18b1524694211417
- Loading branch information
Showing
16 changed files
with
123 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <bench/bench.h> | ||
#include <random.h> | ||
|
||
#include <cstdint> | ||
#include <numeric> | ||
|
||
namespace { | ||
|
||
template<typename RNG> | ||
void BenchRandom_rand64(benchmark::Bench& bench, RNG&& rng) noexcept | ||
{ | ||
bench.batch(1).unit("number").run([&] { | ||
rng.rand64(); | ||
}); | ||
} | ||
|
||
template<typename RNG> | ||
void BenchRandom_rand32(benchmark::Bench& bench, RNG&& rng) noexcept | ||
{ | ||
bench.batch(1).unit("number").run([&] { | ||
rng.rand32(); | ||
}); | ||
} | ||
|
||
template<typename RNG> | ||
void BenchRandom_randbool(benchmark::Bench& bench, RNG&& rng) noexcept | ||
{ | ||
bench.batch(1).unit("number").run([&] { | ||
rng.randbool(); | ||
}); | ||
} | ||
|
||
template<typename RNG> | ||
void BenchRandom_randbits(benchmark::Bench& bench, RNG&& rng) noexcept | ||
{ | ||
bench.batch(64).unit("number").run([&] { | ||
for (int i = 1; i <= 64; ++i) { | ||
rng.randbits(i); | ||
} | ||
}); | ||
} | ||
|
||
template<int RANGE, typename RNG> | ||
void BenchRandom_randrange(benchmark::Bench& bench, RNG&& rng) noexcept | ||
{ | ||
bench.batch(RANGE).unit("number").run([&] { | ||
for (int i = 1; i <= RANGE; ++i) { | ||
rng.randrange(i); | ||
} | ||
}); | ||
} | ||
|
||
template<int RANGE, typename RNG> | ||
void BenchRandom_stdshuffle(benchmark::Bench& bench, RNG&& rng) noexcept | ||
{ | ||
uint64_t data[RANGE]; | ||
std::iota(std::begin(data), std::end(data), uint64_t(0)); | ||
bench.batch(RANGE).unit("number").run([&] { | ||
std::shuffle(std::begin(data), std::end(data), rng); | ||
}); | ||
} | ||
|
||
void FastRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, FastRandomContext(true)); } | ||
void FastRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, FastRandomContext(true)); } | ||
void FastRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, FastRandomContext(true)); } | ||
void FastRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, FastRandomContext(true)); } | ||
void FastRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, FastRandomContext(true)); } | ||
void FastRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, FastRandomContext(true)); } | ||
void FastRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, FastRandomContext(true)); } | ||
void FastRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, FastRandomContext(true)); } | ||
|
||
void InsecureRandom_rand64(benchmark::Bench& bench) { BenchRandom_rand64(bench, InsecureRandomContext(251438)); } | ||
void InsecureRandom_rand32(benchmark::Bench& bench) { BenchRandom_rand32(bench, InsecureRandomContext(251438)); } | ||
void InsecureRandom_randbool(benchmark::Bench& bench) { BenchRandom_randbool(bench, InsecureRandomContext(251438)); } | ||
void InsecureRandom_randbits(benchmark::Bench& bench) { BenchRandom_randbits(bench, InsecureRandomContext(251438)); } | ||
void InsecureRandom_randrange100(benchmark::Bench& bench) { BenchRandom_randrange<100>(bench, InsecureRandomContext(251438)); } | ||
void InsecureRandom_randrange1000(benchmark::Bench& bench) { BenchRandom_randrange<1000>(bench, InsecureRandomContext(251438)); } | ||
void InsecureRandom_randrange1000000(benchmark::Bench& bench) { BenchRandom_randrange<1000000>(bench, InsecureRandomContext(251438)); } | ||
void InsecureRandom_stdshuffle100(benchmark::Bench& bench) { BenchRandom_stdshuffle<100>(bench, InsecureRandomContext(251438)); } | ||
|
||
} // namespace | ||
|
||
BENCHMARK(FastRandom_rand64, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(FastRandom_rand32, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(FastRandom_randbool, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(FastRandom_randbits, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(FastRandom_randrange100, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(FastRandom_randrange1000, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(FastRandom_randrange1000000, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(FastRandom_stdshuffle100, benchmark::PriorityLevel::HIGH); | ||
|
||
BENCHMARK(InsecureRandom_rand64, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(InsecureRandom_rand32, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(InsecureRandom_randbool, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(InsecureRandom_randbits, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(InsecureRandom_randrange100, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(InsecureRandom_randrange1000, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(InsecureRandom_randrange1000000, benchmark::PriorityLevel::HIGH); | ||
BENCHMARK(InsecureRandom_stdshuffle100, benchmark::PriorityLevel::HIGH); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters