Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
This reverts commit 67d9990. Github-Pull: bitcoin#35195 Rebased-From: fb0c207
`SaltedOutpointHasher` has been `noexcept` since bitcoin#16957, which makes libstdc++ omit cached hash codes from `std::unordered_map` nodes. That saves one `size_t` per node, but it also makes `CCoinsMap` recompute the outpoint hash in table operations that could otherwise reuse the cached code. Declare the operator `noexcept(false)` to restore libstdc++ hash-code caching for this fast hash functor. The implementation still does not throw; only the exception specification used by the container policy changes. This restores a value that bitcoin#16957 deliberately removed, but the pool-backed `CCoinsMap` allocation budget still accounts for implementations storing hash values. The existing `PoolAllocator` comment says that "in some cases the hash value is stored as well" and that "sizeof(void*)*4" overhead "should thus be sufficient so that all implementations can allocate the nodes from the PoolAllocator." Add a unit test for the exception-specification contract and the libstdc++ fast-hash classification. Github-Pull: bitcoin#35195 Rebased-From: 16e77fd
There was a problem hiding this comment.
Concept ACK 16e77fd
This PR attempts to improve performance, by allowing CCoinsMap key hashes to be cached.
In general, the tradeoff between extra memory and extra CPU is not clear, but in this case benchmarks indicate that caching (more memory, less CPU) results in faster execution, therefore it should be applied.
Code-wise I don't see issues or dangers reverting the noexcept on the hasher to noexcept(false). Left some nits.
| * @see https://gcc.gnu.org/onlinedocs/gcc-13.2.0/libstdc++/manual/manual/unordered_associative.html | ||
| */ | ||
| size_t operator()(const COutPoint& id) const noexcept | ||
| size_t operator()(const COutPoint& id) const |
There was a problem hiding this comment.
fb0c207 Revert "make SaltedOutpointHasher noexcept":
Nit: In this particular case, I don't see a value in separating into 2 commits, I would prefer a single commit.
|
|
||
| #include <type_traits> | ||
|
|
There was a problem hiding this comment.
16e77fd util: allow caching outpoint hash codes:
Nit: I suggest removing the blank lines here. A blank line as a separator between project and external include makes sense, but these are not needed.
| { | ||
| constexpr bool nothrow_invocable{std::is_nothrow_invocable_v<const SaltedOutpointHasher&, const COutPoint&>}; | ||
| BOOST_CHECK(!nothrow_invocable); | ||
| #ifdef __GLIBCXX__ |
There was a problem hiding this comment.
16e77fd util: allow caching outpoint hash codes:
Nit: I suggest adding a blank line here, to show that the two parts are separate checks.
| * | ||
| * @see https://gcc.gnu.org/onlinedocs/libstdc++/manual/unordered_associative.html | ||
| */ | ||
| size_t operator()(const COutPoint& id) const noexcept(false) |
There was a problem hiding this comment.
16e77fd util: allow caching outpoint hash codes:
Have you considered controlling hash-caching via __is_fast_hash (as described in the mentioned link)? Seems more clear than abusing 'noexcept'. Or that would not work on other platforms?
Allow libstdc++ to cache `CCoinsMap` hash codes instead of recomputing them during table operations. `CCoinsMap` uses `SaltedCoinsCacheHasher`, introduced in bitcoin#35215. libstdc++ caches hash codes for a fast hasher only when it is potentially throwing, so change its outpoint overload to `noexcept(false)`. The implementation still does not throw. Keep the txid overload unchanged for the temporary txid sets used during input prefetching. The pool allocator already reserves room for cached hash values. Add a unit test for the exception specification and libstdc++ fast-hash classification.
16e77fd to
61046c5
Compare

Draft status: Current master uses
SaltedCoinsCacheHasherfor the UTXO cache after #35215. I need to retarget this patch and remeasure it before taking it out of draft. The benchmarks below use the older bases shown.Problem: On this PR’s base,
CCoinsMapusesSaltedOutpointHasherfor UTXO cache lookups. Since #16957 marked the hashernoexcept,libstdc++no longer stores cached hash codes in unordered-map nodes.#16957 optimized for memory: the original benchmark reported about
9.4%lower peak RSS, but also about1.6%slower runtime from recomputing hashes. This PR takes the opposite side of that same tradeoff for the UTXO cache: allowlibstdc++to store one cached hash code per unordered-map node, avoiding repeatedSipHashwork in rehashing and some table operations.Benchmarks consistently favored cached hash codes:
-dbcache.AssumeUTXOloading showed the strongest signal, up to about11%faster. This is the cleanest dbcache-exercising benchmark here because it avoids block download and full validation work, so the coins-cache effect is less diluted.SipHashoptimization work showed about3%faster on an Intel N150 after theSipHashchange.libstdc++by up to one cachedsize_tper node. In practice, recent Massif runs were roughly neutral at moderate dbcache values, while a very large dbcache run showed the expected higher peak.For
CCoinsMap, no allocator sizing change is needed since itsPoolAllocatorblock size already reserves implementation-defined node overhead and explicitly accounts for implementations where "the hash value is stored as well". That assumption remained in the code after the #16957noexceptchange disabled cached hash codes for this hasher, so there is no memory calculation to restore in this PR.This is also consistent with concerns raised in the original #16957 discussion: reviewers noted the
noexceptchange was slower, that it imposed a penalty when memory was not limiting, and that "it might be possible to regain the performance loss by caching the hash ourselves." This PR letslibstdc++do that caching for us.Fix: Revert #16957's
SaltedOutpointHashernoexceptchange, then document the now-intentionalnoexcept(false)contract and add a focused unit test.The implementation still does not throw. Only the exception specification used by
libstdc++'s unordered-map node policy changes: for a fast hash functor that may throw,libstdc++caches hash codes in nodes.The test checks both inputs to that policy:
SaltedOutpointHasheris not nothrow-invocablelibstdc++, it remains classified as a fast hashThis mirrors the type-level contract style used in
libstdc++'s own hash tests.Reproducer:
Fresh reindex-chainstate run
Earlier supporting measurements
Original #16957 result:
#16957 also showed that increasing dbcache on the
noexceptbranch could recover performance while staying below old memory use:Earlier cached-hash measurements:
Later
noexceptremoval measurements:Memory observations:
Compatibility with input-fetcher and `SipHash` work
This change has been in my queue for more than a year, so some supporting measurements are older and were collected while adjacent input-fetcher and
SipHashwork was still evolving. The relevant compatibility measurements are documented in this #31132 review comment.After input fetching,
SipHashinlining, andnoexceptremoval on an Intel N150:After input fetching,
SipHashinlining, andnoexceptremoval on an M4 Max:The Intel N150 comparison supports cached hash codes helping alongside the other changes. The M4 measurement does not identify the C++ library, so I cannot attribute its improvement to this mechanism.
libc++already stores hash codes regardless ofnoexcept.