refactor: Remove c_str from util/check #26960

pull maflcko wants to merge 1 commits into bitcoin:master from maflcko:2301-c-str-🔋 changing 3 files +10 −7
  1. maflcko commented at 11:04 am on January 24, 2023: member

    Seems confusing and fragile to require calling code to call c_str() when passing a read-only view of a std::string.

    Fix that by using std::string_view, which can be constructed from string literals and std::string.

    Also, remove the now unused c_str() from src/wallet/bdb.cpp.

  2. DrahtBot commented at 11:04 am on January 24, 2023: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK stickies-v, aureleoules, theStack

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #26763 (ci: Treat IWYU violations as errors by hebasto)

    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.

  3. DrahtBot added the label Refactoring on Jan 24, 2023
  4. refactor: Remove c_str from util/check fab958290b
  5. maflcko force-pushed on Jan 24, 2023
  6. stickies-v commented at 11:05 am on January 25, 2023: contributor
    Concept ACK
  7. stickies-v approved
  8. stickies-v commented at 12:19 pm on January 25, 2023: contributor

    ACK fab958290b84037fad1d78ffb2bce34d2b0e8c36

    Pretty straightforward change that makes the code more robust. I’m curious why you reverted from fab5500 to fab9582, I think inline_check_non_fatal and inline_assertion_check could benefit from string_view too? Compilation and unit tests seem fine here - so I’m probably missing some nuance?

  9. maflcko commented at 12:24 pm on January 25, 2023: member
    Yeah, it might be fine, but I’d prefer to wait for C++23 consteval. Otherwise there seems to be the risk that an Assert will eat linear time at runtime in the length of the message, even if the assert is not hit.
  10. aureleoules approved
  11. aureleoules commented at 12:25 pm on January 25, 2023: member
    ACK fab958290b84037fad1d78ffb2bce34d2b0e8c36
  12. stickies-v commented at 12:56 pm on January 25, 2023: contributor

    Yeah, it might be fine, but I’d prefer to wait for C++23 consteval. Otherwise there seems to be the risk that an Assert will eat linear time at runtime in the length of the message, even if the assert is not hit.

    If I understand correctly: because if we start using these helpers to take string_view, they’d have to calculate the length of the array at run-time every time Assert/Assume functions are called, and since those are called quite a lot and most of the time we expect them not to fail (i.e. the string_views go completely unused), that would be a waste. With consteval (I think it’s since C++20 already?) that run-time complexity would be avoided since everything is inlined and calculated at compile-time.

    If so, nit: worth adding a little // TODO comment to document that with C++20 we can use consteval and string_view? Personally, I find those quite helpful.

  13. maflcko commented at 1:37 pm on January 25, 2023: member
    Thanks, done in #23363 (comment)
  14. theStack approved
  15. theStack commented at 4:05 pm on January 25, 2023: contributor
    ACK fab958290b84037fad1d78ffb2bce34d2b0e8c36
  16. maflcko merged this on Jan 26, 2023
  17. maflcko closed this on Jan 26, 2023

  18. maflcko deleted the branch on Jan 26, 2023
  19. sidhujag referenced this in commit 35721c5644 on Jan 26, 2023
  20. maflcko commented at 10:08 pm on December 14, 2023: member

    If so, nit: worth adding a little // TODO comment to document that with C++20 we can use consteval and string_view? Personally, I find those quite helpful.

    I tried this and it seems to compile, but I don’t think it is worth it:

     0diff --git a/src/util/check.h b/src/util/check.h
     1index a02a1de8dc..f15117daef 100644
     2--- a/src/util/check.h
     3+++ b/src/util/check.h
     4@@ -21,9 +21,11 @@ public:
     5     NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
     6 };
     7 
     8+consteval std::string_view inline_String_View(const char* sv) { return std::string_view{sv}; }
     9+
    10 /** Helper for CHECK_NONFATAL() */
    11 template <typename T>
    12-T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
    13+T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, std::string_view file, int line, std::string_view func, std::string_view assertion)
    14 {
    15     if (!val) {
    16         throw NonFatalCheckError{assertion, file, line, func};
    17@@ -40,7 +42,7 @@ void assertion_fail(std::string_view file, int line, std::string_view func, std:
    18 
    19 /** Helper for Assert()/Assume() */
    20 template <bool IS_ASSERT, typename T>
    21-T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const char* file, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* assertion)
    22+T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] std::string_view file, [[maybe_unused]] int line, [[maybe_unused]] std::string_view func, [[maybe_unused]] std::string_view assertion)
    23 {
    24     if constexpr (IS_ASSERT
    25 #ifdef ABORT_ON_FAILED_ASSUME
    26@@ -74,7 +76,7 @@ T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const char* f
    27     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
    28 
    29 /** Identity function. Abort if the value compares equal to zero */
    30-#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
    31+#define Assert(val) inline_assertion_check<true>(val, inline_String_View(__FILE__), __LINE__, __func__, #val)
    32 
    33 /**
    34  * Assume is the identity function.
    

    Seems better to just remove the helper functions and macros and use a plain C++20 function, which would also make the compiler warning more useful, see #28999 (comment)


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-09-29 01:12 UTC

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