Minor locking improvements #8007

pull kazcw wants to merge 1 commits into bitcoin:master from kazcw:locknits changing 1 files +11 −4
  1. kazcw commented at 3:04 pm on May 5, 2016: contributor

    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.

  2. kazcw force-pushed on May 5, 2016
  3. sipa commented at 4:09 pm on May 5, 2016: member
    utACK 2a4338c5054906b664e027b1b4bb9c6b4ed50f05
  4. dcousens commented at 2:14 am on May 6, 2016: contributor
    utACK 2a4338c
  5. MarcoFalke added the label Refactoring on May 6, 2016
  6. kazcw force-pushed on May 7, 2016
  7. kazcw commented at 9:49 pm on May 7, 2016: contributor

    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.

  8. sipa commented at 10:02 pm on May 7, 2016: member
    @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.
  9. arowser commented at 8:43 am on May 25, 2016: contributor
    Can one of the admins verify this patch?
  10. sipa commented at 1:07 pm on May 26, 2016: member
    @kazcw Can you add some form of synchronization back to CNode::minFeeFilter?
  11. kazcw force-pushed on May 29, 2016
  12. kazcw commented at 6:00 pm on May 29, 2016: contributor
    Sure, I popped off the minFeeFilter commit.
  13. laanwj commented at 10:54 am on May 30, 2016: member
    Is there any advantage to making this boolean thread-local instead of atomic?
  14. sipa commented at 5:33 pm on May 31, 2016: member
    @laanwj I assume a thread-local is slightly faster as it doesn’t need memory synchronization (but that’s a pure guess).
  15. sipa commented at 4:27 pm on June 1, 2016: member
    On the other hand, an atomic would be nice to guarantee monotonicity…
  16. kazcw force-pushed on Jun 1, 2016
  17. kazcw commented at 5:34 pm on June 1, 2016: contributor
    @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.
  18. sipa commented at 5:41 pm on June 1, 2016: member
    utACK b07b3f9c1a4b1a0b5f7189f9a0e45cf7547582cf
  19. in src/main.cpp: in a91c89795f outdated
    1574@@ -1574,18 +1575,24 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
    1575 bool IsInitialBlockDownload()
    1576 {
    1577     const CChainParams& chainParams = Params();
    1578+
    1579+    // Once IIBD is first found to be false, it must remain false.
    


    paveljanik commented at 5:49 am on June 2, 2016:
    IIBD -> IBD
  20. paveljanik commented at 6:04 am on June 2, 2016: contributor
  21. IsInitialBlockDownload: usually avoid locking
    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.
    f0fdda0181
  22. kazcw force-pushed on Jun 5, 2016
  23. kazcw commented at 5:20 am on June 5, 2016: contributor
    Squashed & fixed comment
  24. laanwj commented at 1:44 pm on June 6, 2016: member

    utACK https://github.com/bitcoin/bitcoin/commit/b07b3f9c1a4b1a0b5f7189f9a0e45cf7547582cf

    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.

  25. laanwj merged this on Jun 6, 2016
  26. laanwj closed this on Jun 6, 2016

  27. laanwj referenced this in commit 6b781df74f on Jun 6, 2016
  28. codablock referenced this in commit 829e3e1f01 on Sep 16, 2017
  29. codablock referenced this in commit 63797dab9c on Sep 19, 2017
  30. codablock referenced this in commit 463a68d4a5 on Dec 22, 2017
  31. zkbot referenced this in commit ebd25d1744 on Jul 16, 2018
  32. zkbot referenced this in commit 6d605ffbbe on Jul 17, 2018
  33. zkbot referenced this in commit 3835cbb57f on Jul 17, 2018
  34. andvgal referenced this in commit 6d86b1eda6 on Jan 6, 2019
  35. furszy referenced this in commit 1c472ebae8 on Jul 7, 2020
  36. MarcoFalke locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-12-18 18:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me