Skip to content
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

Merged
merged 2 commits into from
Apr 11, 2024
Merged

Reuse available string length #385

merged 2 commits into from
Apr 11, 2024

Conversation

mmd-osm
Copy link
Collaborator

@mmd-osm mmd-osm commented Apr 9, 2024

No description provided.

mmd-osm added 2 commits April 9, 2024 20:20
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);
Copy link
Contributor

@Woazboat Woazboat Apr 9, 2024

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

Copy link
Collaborator Author

@mmd-osm mmd-osm Apr 10, 2024

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.

Copy link
Contributor

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.

Copy link
Collaborator Author

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

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

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)

@mmd-osm mmd-osm merged commit 4509dca into zerebubuth:master Apr 11, 2024
6 checks passed
@mmd-osm mmd-osm added this to the v0.9.1 milestone Apr 12, 2024
@mmd-osm mmd-osm deleted the patch/sv2 branch July 13, 2024 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants