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
parent 3dc39cefbc
commit e904a9fce6
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)
hashes.push_back(hashes.back());
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());
sha256n_way(hashes);
hashes.resize(hashes.size() / 2);
}
return hashes.empty() ? uint256{} : hashes[0];

View file

@ -11,3 +11,11 @@ uint256 CalcHash(SHA256_CTX* sha)
SHA256_Final(result.begin(), sha);
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 <functional>
#include <vector>
#include <uints.h>
uint256 CalcHash(SHA256_CTX* sha);
@ -25,4 +28,6 @@ uint256 Hash(TIterator begin, TIterator end, Args... args)
return CalcHash(&sha, begin, end, args...);
}
extern std::function<void(std::vector<uint256>&)> sha256n_way;
#endif // CLAIMTRIE_HASHES_H

View file

@ -16,6 +16,7 @@
#include <chain.h>
#include <chainparams.h>
#include <claimtrie/forks.h>
#include <claimtrie/hashes.h>
#include <clientversion.h>
#include <compat/sanity.h>
#include <consensus/validation.h>
@ -1580,6 +1581,13 @@ bool AppInitMain(InitInterfaces& interfaces)
break;
}
// 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();
if (tip && !CClaimTrieCache(pclaimTrie).validateDb(tip->nHeight, tip->hashClaimTrie)) {
strLoadError = _("Error validating the claim trie from disk");