Skip to content

Conversation

kazcw
Copy link
Contributor

@kazcw kazcw commented May 5, 2016

A tweak to IsInitialBlockDownload prevents taking cs_main most of the time: it already remembers a negative result to save some work; doing so per-thread allows the check to be pulled out of the lock.

cs_feeFilter is used only to guard minFeeFilter, which is an integer. This is the simplest use case of an atomic variable: the only guarantee needed is atomicity.

@sipa
Copy link
Member

sipa commented May 5, 2016

utACK 2a4338c5054906b664e027b1b4bb9c6b4ed50f05

@dcousens
Copy link
Contributor

dcousens commented May 6, 2016

utACK 2a4338c

@kazcw
Copy link
Contributor Author

kazcw commented May 7, 2016

Force-pushed a new head commit: I just noticed that minFeeFilter is actually only accessed in the message handling thread and doesn't need any kind of synchronization.

The IsInitialBlockChanges commit is unmodified.

@sipa
Copy link
Member

sipa commented May 7, 2016

@kazcw I prefer seeing correct synchronization, even for variables that are only accessed from one thread. There are probably several such violations already, but over time, I think we'll want multiple message handler threads.

@arowser
Copy link
Contributor

arowser commented May 25, 2016

Can one of the admins verify this patch?

@sipa
Copy link
Member

sipa commented May 26, 2016

@kazcw Can you add some form of synchronization back to CNode::minFeeFilter?

@kazcw
Copy link
Contributor Author

kazcw commented May 29, 2016

Sure, I popped off the minFeeFilter commit.

@laanwj
Copy link
Member

laanwj commented May 30, 2016

Is there any advantage to making this boolean thread-local instead of atomic?

@sipa
Copy link
Member

sipa commented May 31, 2016

@laanwj I assume a thread-local is slightly faster as it doesn't need memory synchronization (but that's a pure guess).

@sipa
Copy link
Member

sipa commented Jun 1, 2016

On the other hand, an atomic would be nice to guarantee monotonicity...

@kazcw
Copy link
Contributor Author

kazcw commented Jun 1, 2016

@laanwj I'd thought it was a little simpler to avoid the need for cross-thread reasoning, but I'm not so sure; an atomic seems to map more directly to the intent here. I'd also made a similar performance assumption to @sipa, but looking in to it now there are apparently some thorny thread-local storage implementations in the wild. So, I've reimplemented it with an atomic for clarity and consistent performance.

@sipa
Copy link
Member

sipa commented Jun 1, 2016

utACK b07b3f9c1a4b1a0b5f7189f9a0e45cf7547582cf

@@ -1574,18 +1575,24 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
bool IsInitialBlockDownload()
{
const CChainParams& chainParams = Params();

// Once IIBD is first found to be false, it must remain false.
Copy link
Contributor

Choose a reason for hiding this comment

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

IIBD -> IBD

@paveljanik
Copy link
Contributor

utACK a91c897
Please squash and fix the typo.

Optimistically test the latch bool before taking the lock.
For all IsInitialBlockDownload calls after the first to return false,
this avoids the need to lock cs_main.
@kazcw
Copy link
Contributor Author

kazcw commented Jun 5, 2016

Squashed & fixed comment

@laanwj
Copy link
Member

laanwj commented Jun 6, 2016

utACK b07b3f9

but looking in to it now there are apparently some thorny thread-local storage implementations in the wild. So, I've reimplemented it with an atomic for clarity and consistent performance.

Right. On hardware that natively supports atomic operations (which are most modern CPUs), I'd expect an atomic to be slightly more performant. Thread-local variables, classically at least, come with serious overhead, like having to set up a special segment. That's worth it for larger per-thread data structures, but not one boolean.

@laanwj laanwj merged commit f0fdda0 into bitcoin:master Jun 6, 2016
laanwj added a commit that referenced this pull request Jun 6, 2016
f0fdda0 IsInitialBlockDownload: usually avoid locking (Kaz Wesley)
codablock pushed a commit to codablock/dash that referenced this pull request Dec 22, 2017
f0fdda0 IsInitialBlockDownload: usually avoid locking (Kaz Wesley)
zkbot added a commit to zcash/zcash that referenced this pull request Jul 16, 2018
InitialBlockDownload upstream changes

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#7208
- bitcoin/bitcoin#8007
- bitcoin/bitcoin#9053
  - Excluding second commit (requires bitcoin/bitcoin#8865)
- bitcoin/bitcoin#10388
zkbot added a commit to zcash/zcash that referenced this pull request Jul 17, 2018
InitialBlockDownload upstream changes

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#7208
- bitcoin/bitcoin#8007
- bitcoin/bitcoin#9053
  - Excluding second commit (requires bitcoin/bitcoin#8865)
- bitcoin/bitcoin#10388
zkbot added a commit to zcash/zcash that referenced this pull request Jul 17, 2018
InitialBlockDownload upstream changes

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#7208
- bitcoin/bitcoin#8007
- bitcoin/bitcoin#9053
  - Excluding second commit (requires bitcoin/bitcoin#8865)
- bitcoin/bitcoin#10388
andvgal pushed a commit to energicryptocurrency/gen2-energi that referenced this pull request Jan 6, 2019
f0fdda0 IsInitialBlockDownload: usually avoid locking (Kaz Wesley)
furszy added a commit to PIVX-Project/PIVX that referenced this pull request Jul 7, 2020
9e378e4 IsInitialBlockDownload: usually avoid locking (furszy)

Pull request description:

  Straightforward, coming from [btc@8007](bitcoin#8007)

ACKs for top commit:
  random-zebra:
    Nice! utACK 9e378e4
  Fuzzbawls:
    utACK 9e378e4

Tree-SHA512: 3f5ebfbafa64ca7ff5397405c87f0030d81a4342486e420c7f3bf03daef08947afb22aed9d20bff02b4376a64e02902b9882c637e7a94ec235c4a5d4e1280391
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants