Remove redundant NDEBUG preprocessor checks #17459

pull theStack wants to merge 1 commits into bitcoin:master from theStack:20191112-remove-redundant-ndebug-checks changing 2 files +0 −8
  1. theStack commented at 12:11 AM on November 13, 2019: member

    Since commit 7cee85807c4db679003c6659d247a2fe74c2464a ("Add compile time verification of assumptions we're currently making implicitly/tacitly", PR #15391), this check is done in compat/assumptions.h: https://github.com/bitcoin/bitcoin/blob/8237889e8d0fb7542669a9098516c96da91913f0/src/compat/assumptions.h#L13-L18 This header is included by util/system.h, which is in turn included by most .cpp files, including the ones this commit modifies (validation.cpp and net_processing.cpp).

  2. Remove redundant NDEBUG preprocessor checks
    Since commit 7cee85807c4db679003c6659d247a2fe74c2464a ("Add compile time
    verification of assumptions we're currently making implicitly/tacitly"), this
    check is done in compat/assumptions.h which is included by util/system.h,
    which is in turn included by most .cpp files, including the ones this commit
    modifies.
    7f3397b234
  3. theStack force-pushed on Nov 13, 2019
  4. DrahtBot added the label P2P on Nov 13, 2019
  5. DrahtBot added the label Validation on Nov 13, 2019
  6. MarcoFalke added the label Refactoring on Nov 13, 2019
  7. practicalswift commented at 10:17 PM on November 13, 2019: contributor

    @theStack

    I'm all for removing redundancies in the general case, but in this specific case the redundancy is intentional:

    Note that the inclusion of assumptions.h could be removed accidentally from net_processing.cpp and validation.cpp without breaking the build.

    Thus we cannot assume that assumptions.h will always be included :)

    I think of it this way:

    • Enforcement: The NDEBUG checks in net_processing.cpp and validation.cpp make sure that we enforce this project wide (since at least one of those files likely will be part of all relevant future builds of the project where this should be enforced).
    • Documentation: The NDEBUG check in assumptions.h serves as a documentation of an important assumption we make.

    Does that make sense? :)

  8. theStack commented at 12:02 AM on November 14, 2019: member

    @practicalswift: Thanks for clarifying! My strong assumption was that in any possible build, one of the dozens .cpp files which currently include the compat/assumptions.h header (which after all says "compile-time verification", implying more than merely documentation) would also do so in the future, triggering the error in case NDEBUG is set. Also it seemed quite fishy to me that the check was even present in two .cpp files, not just one. Looking deeper into it, that seems to have historic reasons: introduced originally by gmaxwell with commit 9b59e3bda8c137bff885db5b1f9150346e36e076, the NDEBUG check was only present in main.cpp. Then later when main.cpp was split up into validation.cpp and net_processing.cpp it got copied over into both files (commits e736772c56a883e2649cc8534dd7857a0718ec56, 76faa3cdfedbd3fc91be4ecfff77fc6dc18134fb by TheBlueMatt; part of PR #9260 with a very entertaining title/description :laughing: ).

    Still feels odd to have the exact same preprocessor construct and error string scattered aorund in three places in the same project, but I get your point.

  9. laanwj commented at 11:46 AM on November 15, 2019: member

    Tend toward NACK. This code can go when the run-time critical assumptions stop using assertions. Then it is fully safe to define NDEBUG. Until then, belts and suspenders. (@MarcoFalke is working on this)

  10. MarcoFalke commented at 5:23 PM on November 15, 2019: member

    Yeah, if the goal is to make sure the assert can never be disabled by accident, but also avoid these preprocessor checks, you could cherry-pick something like fa1564326499f69eeb5427262ff0f26070f86146, replace the std::abort(); with assert(false), and add the preprocessor check to ./src/util/check.h (a single place).

  11. laanwj commented at 12:30 PM on November 18, 2019: member

    Though, I'd say it's not much of a goal in itself. Wouldn't be surprised if the preprocessor is the least CPU intensive part of the C++ compilation process.

  12. fanquake commented at 8:57 AM on January 4, 2020: member

    Closing as something that we can potentially revisit in the future.

  13. fanquake closed this on Jan 4, 2020

  14. DrahtBot locked this on Feb 15, 2022

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: 2026-04-15 15:14 UTC

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