-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reuse available string length #385
Conversation
Indirect approach based on c_str() had to determine the string length again, although libpqxx already has that info.
Enables reuse of already available string length info
bool> = true> | ||
void entry(T&& s) | ||
{ | ||
auto sv = std::string_view(s); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to use std::forward<T>(s)
to properly pass on the forwarding reference.
(s
binds to everything, but is itself a lvalue if referenced)
I don't think the template has any benefit over just inline void entry(std::string_view sv)
though if a string_view is always constructed from s
anyway. The function parameter will already use the appropriate constructor for string views depending on what's passed in and it should also accept a char *
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here was to avoid char* string literals ending up in entry(bool): https://godbolt.org/z/1PKz9o6dT vs. https://godbolt.org/z/ffqhjjbdh - Besides, to avoid strlen() operations in entry(const char*), which are still neded at this time, but are really pointless for string literals.
At least the generated code looks exactly as it should now. I need to check the std::forward bit still.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here was to avoid char* string literals ending up in entry(bool)
Ah, yeah then that's probably required to do it this way.
The generated code will likely be the same even without std::forward since I don't believe std::string_view uses any optimization for rvalue moves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the optimizer even completely removes the intermediate string_view, and turns printing of string literals into something like:
899f0: 48 8b 7d 08 mov 0x8(%rbp),%rdi
899f4: ba 1e 00 00 00 mov $0x1e,%edx
899f9: 48 8d 35 08 1b 07 00 lea 0x71b08(%rip),%rsi
89a00: e8 5b 73 f9 ff call 20d60 <yajl_gen_string@plt>
@@ -40,7 +40,8 @@ enum class action_type { | |||
|
|||
namespace { | |||
|
|||
const char* element_type_name(element_type elt) { | |||
template <typename T = const char *> | |||
T element_type_name(element_type elt) noexcept { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could probably always return a std::string_view and be made constexpr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xml_writer::attribute expects null terminated strings at the moment, so string_view alone would trigger some intermediate object generation. We need to find a good approach that works both for json and xml.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, using std::string_view wouldn't work for functions expecting null terminated C strings. (Well, technically you could call .data() on it, but that's best avoided since in general string_view strings don't have to be null terminated)
No description provided.