Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Jan 24, 2023

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
Copy link
Contributor

DrahtBot commented Jan 24, 2023

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
Contributor

Concept ACK

Copy link
Contributor

@stickies-v stickies-v left a comment

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
Copy link
Member Author

maflcko commented Jan 25, 2023

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.

Copy link
Contributor

@aureleoules aureleoules left a comment

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
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
Copy link
Member Author

maflcko commented Jan 25, 2023

Thanks, done in #23363 (comment)

Copy link
Contributor

@theStack theStack left a comment

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
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jan 26, 2023
fab9582 refactor: Remove c_str from util/check (MarcoFalke)

Pull request description:

  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`.

ACKs for top commit:
  stickies-v:
    ACK fab9582
  aureleoules:
    ACK fab9582
  theStack:
    ACK fab9582

Tree-SHA512: ae39812c6bb8e2ef095e1b843774af2718f48404cb848c3e43b16d3c22240557d69d54a13a038a4a9c48b3ba0e4523e1f87abdd60f91486092f50fd43c0e8483
@maflcko
Copy link
Member Author

maflcko commented Dec 14, 2023

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