Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: [Memory optimization] Separate chain trust from the block index #1961

Merged

Conversation

cyrossignol
Copy link
Member

This is part of a series of changes that optimize the memory usage of Gridcoin's block index. According to Valgrind's heap profiler (Massif), these changes will decrease memory usage of the application by over 500 MB after the final PR merges. Because these optimizations apply to every block, memory savings will continue to accrue as the chain grows. I'm breaking-up the commits into separate PRs because the branch has become too unwieldy to review in one submission.


This replaces the chain trust state in the block index with a custom cache to save 32 bytes of memory per block.

The cache contains two levels. The first provides quick access to trust values near the chain tip. Nodes use these values when validating new blocks. The second level stores historical chain trust values at height intervals for reporting. The trust value for a historical block can be recomputed on-demand. By tracking the historical values periodically, we can start calculating chain trust from a cached height to reduce the effort needed to reproduce it for a particular block.

This adopts Peercoin's change to CBlockIndex::GetBlockTrust() to use
Bitcoin's arith_uint256 type for big-integer arithmetic to avoid the
conversion from the old CBigNum type.
This removes a piece of state that tracks the invalid chain with the
most chain trust and writes it to the database. Nothing uses it.
This replaces the nChainTrust field in the block index with a cache that
provides quick access to chain trust values at the chain tip. Nodes only
use historical chain trust values for reporting, so the cache stores the
exact values at block height intervals and recomputes chain trust values
in between.

This saves 32 bytes of memory per block.
Copy link
Member

@jamescowens jamescowens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clever! :)

Comment on lines +306 to +307
// Invalidate cached snapshots greater than this height:
m_cache.erase(m_cache.begin() + offset + 1, m_cache.end());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is critical for a reorg that crosses the 10000 block ladder... good.

@jamescowens
Copy link
Member

One comment (not necessary for approval). Any particulars on the choices of the size of the short cache, and the granularity of the long cache? Did you do any performance profiling to determine these values versus the balance to memory usage, or are they educated guesses? 32 seems pretty good for the short cache, as we rarely see head of the chain forks longer than that under normal circumstances.

@jamescowens jamescowens added this to the Hilda milestone Nov 8, 2020
@jamescowens jamescowens merged commit 93f3867 into gridcoin-community:development Nov 8, 2020
@cyrossignol
Copy link
Member Author

I haven't had a chance to tune these for optimal performance yet. For the short cache, it's possible that we can store more than 32 entries with no observable impact. The MightContain() check could be completely unnecessary because the cost of dereferencing the pointers may cancel out the effect of the early exit. I wouldn't expect the implementation to be much of a hotspot... the chain trust comparison runs just once per block and may not even register a blip in a profiler compared to all of the other validation that happens around it. I considered 32 entries as the minimum needed for typical network behavior, and I think returns diminish sharply as we increase the size.

The long cache is more interesting. The spacing directly influences the performance of getblock and co. RPCs which might matter for services like explorers that need to query historical blocks. A shorter interval means that we can recompute chain trust more quickly for a random block. We may want to add a configuration option that enables someone to choose how the cache behaves. Setting the interval to 1 makes it as fast as the original index (with the same memory overhead). Most users won't notice the difference, though.

@jamescowens
Copy link
Member

jamescowens commented Nov 9, 2020

I don't think the juice is worth the squeeze on configurability of the long cache. You could do that on a test mule and run some tests to fine tune the value if you have the time and then hard code it.

My tests of getblock were almost instantaneous. Also the chaintrust calc for init was faster with this algorithm than the original, at least on my main workstation, which is a pretty good Intel CPU.

jamescowens added a commit that referenced this pull request Mar 1, 2021
 Added
 - gui: Add RAC column to wizard summary page projects table #1951 (@cyrossignol)
 - rpc: clean up the superblocks function and add magnitude to getmininginfo #1966 (@jamescowens)
 - rpc: Add transaction size to RPC output #1971 (@cyrossignol)
 - voting: Add user-facing support for poll response types #1976 (@cyrossignol)
 - gui: Port Bitcoin Intro class (implement the ability to choose a data directory via the GUI) #1978 (@jamescowens)
 - gui: Port Bitcoin MacOS app nap manager #1991 (@jamescowens)
 - mining, rpc: Implement staking efficiency measure and improve SelectCoinsForStaking and CreateCoinStake #1992 (@jamescowens)
 - accrual, rpc: Implement auditsnapshotaccruals #2001 (@jamescowens)
 - docs: add doxygen support #2000 (@div72)
 - beacon: Specialized beacon storage in leveldb #2009 (@jamescowens)
 - rpc: Add a call to dump contracts in binary form #2011 (@div72)
 - rpc: Add boolean option to report active beacons only in beaconreport #2013 (@jamescowens)
 - consensus: Set Hilda mainnet hardfork height to 2197000 #2022 (@jamescowens)

 Changed
 - refactor: [Memory optimization] Block index duplicate PoS state #1945 (@cyrossignol)
 - refactor: [Memory optimization] Block index superblock and contract flags #1950 (@cyrossignol)
 - refactor: [Memory optimization] Remove stake modifier checksums #1954 (@cyrossignol)
 - refactor: [Memory optimization] Block index allocation overhead #1957 (@cyrossignol)
 - refactor: [Memory optimization] Remove block index subsidy fields #1960 (@cyrossignol)
 - refactor: [Memory optimization] Separate chain trust from the block index #1961 (@cyrossignol)
 - refactor: [Memory optimization] Eliminate padding between block index fields #1962 (@cyrossignol)
 - beacon, gui: Add check for presence of beacon private key to updateBeacon() #1968 (@jamescowens)
 - util: Enhance ETTS calculation #1973 (@jamescowens)
 - refactor: Use new clamp in util.h #1975 (@jamescowens)
 - gui: Redo global status for overview #1983 (@jamescowens)
 - util: Improvements to MilliTimer class and use in the miner and init #1987 (@jamescowens)
 - rpc: Move rpc files to directory #1995 (@Pythonix)
 - rpc: Enhance consolidateunspent and fix fee calculation #1994 (@jamescowens)
 - contract: Double the lookback scope of contract replay #1998 (@jamescowens)
 - net: Don't rely on external IP resolvers #2002 (@Tetrix42)
 - beacon: Change beacon map to pointers #2008 (@jamescowens)
 - gui: Update bitcoin_sv.ts #2014 (@sweede-se)
 - util: Update snapshot URLs and add accrual directory #2019 (@jamescowens)
 - beacon: Tweak BeaconRegistry::Revert #2020 (@jamescowens)
 - rpc, qt: bump fees @2023 (@div72)

 Removed
 - researcher: Remove automatic legacy beacon key import #1963 (@cyrossignol)
 - util: Revert "Close LevelDB after loading the block index" #1969 (@cyrossignol)
 - ci: Fix python symlink issue & remove travis #1990 (@div72)
 - ci: remove python workaround #2005 (@div72)

 Fixed
 - gui: fix mandatory/leisure detection of upgrade check #1959 (@Pythonix)
 - voting: Fix title in "gettransaction" RPC for legacy poll contracts @1970 (@cyrossignol)
 - gui: Fix missing menu items on macOS #1972 (@scribblemaniac)
 - rpc: Fix answer offset in "votedetails" #1974 (@cyrossignol)
 - voting: Implement missing try-catch in VotingVoteDialog::vote #1980 (@jamescowens)
 - scraper: Add check for minimum housekeeping complete in scraper #1977 (@jamescowens)
 - voting: Fix nonsense vote weights for legacy polls #1988 (@cyrossignol)
 - voting: Fix incorrect field returned in ResolveMoneySupplyForPoll() #1989 (@cyrossignol)
 - consensus, accrual: Fix newbie accrual #2004 (@jamescowens)
 - log: grammar correction #2016 (@nathanielcwm)
 - wallet: Correct nMinFee fee calculation in CreateTransaction #2021 (@jamescowens)
 - rpc, miner: Correct GetLastStake #2026 (@jamescowens)
 - wallet: Fix bug in CreateTransaction causing insufficient fees #2029 (@jamescowens)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants