As IsInitialBlockDownload latches to false only once the Tip is sufficiently advanced there is no need to check the Tip everytime IsIBD is called.
By caching this in advance we can avoid extra work and more importantly a lock.
As IsInitialBlockDownload latches to false only once the Tip is sufficiently advanced there is no need to check the Tip everytime IsIBD is called.
By caching this in advance we can avoid extra work and more importantly a lock.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32885.
See the guideline for information on the review process.
| Type | Reviewers |
|---|---|
| Concept ACK | luke-jr |
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.
Reviewers, 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.
🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/runs/45440913265
LLM reason (✨ experimental): The CI failure is caused by compilation errors due to missing mutex lock assertions in validation.cpp.
Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
Conceptually not a bad idea to cache and lock less, but imo this makes the code more brittle (and harder to understand), e.g. if any tip updates happen without the cache being updated separately.
Do you have any data as to the actual performance improvements from this PR?
Conceptually not a bad idea to cache and lock less, but imo this makes the code more brittle (and harder to understand), e.g. if any tip updates happen without the cache being updated separately.
Do you have any data as to the actual performance improvements from this PR?
I’m (very) open to suggestions on how to make the caching call more robust. (Indeed I expected some.)
There’s no performance improvement from this PR, it’s the first in a series of proposed changes I’ll be making to remove locking where it’s not necessary, with the end goal being some form of concurrency being possible in message processing.
I think something like this, if properly implemented (I haven’t thought much about the code yet), would reduce the GUI freezes during IBD in a noticeable manner.
I hadn’t even considered that, but certainly that’s a possible direct improvement.
tight polling of is_ibd seems like a mistake in the first place, so i am not sure if this is something to optimize for.
Looking at the remaining call sites of the ibd check, most have cs_main already, so they won’t be affected by this? The remaining ones (I only found MaybeSendFeefilter), if they are relevant, could either re-order their code to call it less often, or cache the bool themselves? For the gui, see also https://github.com/bitcoin/bitcoin/issues/17145
Concept ACK, but I’m not convinced this implementation is safe as-is. If we want to maintain the current behaviour, it’s not sufficient to update only when the tip changes. We also need to re-check when importing/reindexing completes, and schedule an update timer if max_tip_age is the final cause of not exiting IBD.
This made me revisit the function and consider what we’re trying to achieve.
The function is only interesting when it can latch to the IBD finished state.
That’s only possible when all four conditions are met, which can only happen when the tip is updated.
The final time based condition can only change when the tip changes as it gets further away with time, not closer.
3078@@ -3089,6 +3079,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
3079 }
3080
3081 m_chain.SetTip(*pindexDelete->pprev);
3082+ m_chainman.CacheIsInitialBlockDownload();
It’s technically possible for disconnecting a block to get us out of IBD, though I really don’t think that particular edge case is super important.
I was just trying to be thorough.
2043+ if (m_cached_finished_ibd.load(std::memory_order_relaxed)) return;
2044+
2045+ if (m_blockman.LoadingBlocks()) return;
2046+
2047+ {
2048+ AssertLockHeld(cs_main);
EXCLUSIVE_LOCKS_REQUIRED(cs_main) anyway, why not put it to the beginning of the function, as it is done in most other places?
The function is only interesting when it can latch to the IBD finished state.
That’s only possible when all four conditions are met, which can only happen when the tip is updated.
The final time based condition can only change when the tip changes as it gets further away with time, not closer.
I think that @luke-jr is right. If we reindex, we set m_importing to true in ImportBlocks, so any blocks we connect there can never result in getting out of IBD due to the m_blockman.LoadingBlocks() early return.
Therefore we need a call to CacheIsInitialBlockDownload() after ImportingNow goes out of scope in ImportBlocks().
Ok I thought about it and it just wasn’t obviously correct enough.
So I’ve rewritten into three commits to be simpler.
On systems with sane clocks the chain tip checks can only change when the tip
changes. The gap between the chain tip and the current time only grows.