Skip to content

refactor: Remove c_str from util/check - #26960

Merged
maflcko merged 1 commit into
bitcoin:masterfrom
maflcko:2301-c-str-🔋
Jan 26, 2023

Hidden character warning

The head ref may contain hidden characters: "2301-c-str-\ud83d\udd0b"
Merged

maflcko merged 1 commit into
bitcoin:masterfrom
maflcko:2301-c-str-🔋

Conversation

@maflcko

@maflcko maflcko commented Jan 24, 2023

Copy link
Copy Markdown
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.

@DrahtBot

DrahtBot commented Jan 24, 2023 •

Copy link
Copy Markdown
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.

@stickies-v

Copy link
Copy Markdown
Contributor

Concept ACK

@stickies-v stickies-v left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK fab9582

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?

@maflcko

maflcko commented Jan 25, 2023

Copy link
Copy Markdown
Member Author

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.

@aureleoules aureleoules left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK fab9582

@stickies-v

Copy link
Copy Markdown
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.

@maflcko

maflcko commented Jan 25, 2023

Copy link
Copy Markdown
Member Author

Thanks, done in #23363 (comment)

@theStack theStack left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK fab9582

@maflcko
maflcko merged commit d4c180e into bitcoin:master Jan 26, 2023
@maflcko
maflcko deleted the 2301-c-str-🔋 branch January 26, 2023 08:12
@maflcko

maflcko commented Dec 14, 2023

Copy link
Copy Markdown
Member Author

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:

diff --git a/src/util/check.h b/src/util/check.h
index a02a1de8dc..f15117daef 100644
--- a/src/util/check.h
+++ b/src/util/check.h
@@ -21,9 +21,11 @@ public:
     NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
 };
 
+consteval std::string_view inline_String_View(const char* sv) { return std::string_view{sv}; }
+
 /** Helper for CHECK_NONFATAL() */
 template <typename T>
-T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
+T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, std::string_view file, int line, std::string_view func, std::string_view assertion)
 {
     if (!val) {
         throw NonFatalCheckError{assertion, file, line, func};
@@ -40,7 +42,7 @@ void assertion_fail(std::string_view file, int line, std::string_view func, std:
 
 /** Helper for Assert()/Assume() */
 template <bool IS_ASSERT, typename T>
-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)
+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)
 {
     if constexpr (IS_ASSERT
 #ifdef ABORT_ON_FAILED_ASSUME
@@ -74,7 +76,7 @@ T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const char* f
     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
 
 /** Identity function. Abort if the value compares equal to zero */
-#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
+#define Assert(val) inline_assertion_check<true>(val, inline_String_View(__FILE__), __LINE__, __func__, #val)
 
 /**
  * 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)

@bitcoin bitcoin locked and limited conversation to collaborators Dec 13, 2024
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.

5 participants