Use faster continuous memory hash computation

Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
Anthony Fieroni 2019-12-20 17:59:13 +02:00 committed by Brannon King
parent f1fc3626c9
commit 5bfee60297
4 changed files with 22 additions and 4 deletions

View file

@ -236,10 +236,7 @@ uint256 ComputeMerkleRoot(std::vector<uint256> hashes)
if (hashes.size() & 1) if (hashes.size() & 1)
hashes.push_back(hashes.back()); hashes.push_back(hashes.back());
for (std::size_t i = 0, j = 0; i < hashes.size(); i += 2) sha256n_way(hashes);
hashes[j++] = Hash(hashes[i].begin(), hashes[i].end(),
hashes[i+1].begin(), hashes[i+1].end());
hashes.resize(hashes.size() / 2); hashes.resize(hashes.size() / 2);
} }
return hashes.empty() ? uint256{} : hashes[0]; return hashes.empty() ? uint256{} : hashes[0];

View file

@ -11,3 +11,11 @@ uint256 CalcHash(SHA256_CTX* sha)
SHA256_Final(result.begin(), sha); SHA256_Final(result.begin(), sha);
return result; return result;
} }
// universal N way hash function
std::function<void(std::vector<uint256>&)> sha256n_way =
[](std::vector<uint256>& hashes) {
for (std::size_t i = 0, j = 0; i < hashes.size(); i += 2)
hashes[j++] = Hash(hashes[i].begin(), hashes[i].end(),
hashes[i+1].begin(), hashes[i+1].end());
};

View file

@ -4,6 +4,9 @@
#include <openssl/sha.h> #include <openssl/sha.h>
#include <functional>
#include <vector>
#include <uints.h> #include <uints.h>
uint256 CalcHash(SHA256_CTX* sha); uint256 CalcHash(SHA256_CTX* sha);
@ -25,4 +28,6 @@ uint256 Hash(TIterator begin, TIterator end, Args... args)
return CalcHash(&sha, begin, end, args...); return CalcHash(&sha, begin, end, args...);
} }
extern std::function<void(std::vector<uint256>&)> sha256n_way;
#endif // CLAIMTRIE_HASHES_H #endif // CLAIMTRIE_HASHES_H

View file

@ -15,6 +15,7 @@
#include <chainparams.h> #include <chainparams.h>
#include <checkpoints.h> #include <checkpoints.h>
#include <claimtrie/forks.h> #include <claimtrie/forks.h>
#include <claimtrie/hashes.h>
#include <clientversion.h> #include <clientversion.h>
#include <compat/sanity.h> #include <compat/sanity.h>
#include <consensus/validation.h> #include <consensus/validation.h>
@ -1534,6 +1535,13 @@ bool AppInitMain()
assert(chainActive.Tip() != nullptr); assert(chainActive.Tip() != nullptr);
} }
// use faster N way hash function
// NOTE: it assumes memory is continuous
// that means uint256 itself should use std::array or raw pointer
sha256n_way = [](std::vector<uint256>& hashes) {
SHA256D64(hashes[0].begin(), hashes[0].begin(), hashes.size() / 2);
};
auto tip = chainActive.Tip(); auto tip = chainActive.Tip();
if (tip && !CClaimTrieCache(pclaimTrie).validateDb(tip->nHeight, tip->hashClaimTrie)) { if (tip && !CClaimTrieCache(pclaimTrie).validateDb(tip->nHeight, tip->hashClaimTrie)) {
strLoadError = _("Error validating the claim trie from disk"); strLoadError = _("Error validating the claim trie from disk");