http_proto icon indicating copy to clipboard operation
http_proto copied to clipboard

`fields_base::set` avoiding temporaries when possible

Open ashtum opened this issue 1 year ago • 3 comments

Temporary strings are sometimes required to call fields_base::set. For example:

request.set(field::host, url.authority().encoded_host_and_port().decode());
std::string field = cookie_jar->make_field(url); // temporary 
field.append(explicit_cookies); // ideally append in place
request.set(field::cookie, field);

It would be useful to provide an interface that enables this.

ashtum avatar Dec 06 '24 06:12 ashtum

I'm not quite sure what you are asking. request::set does append to the internal string, and it does so in a single call to resize the buffer if needed. Or do you mean that you want some kind of way to write the result of cookie_jar::make_field directly into the destination container? That's an interesting idea but could be an example of going too far. Temporary calculations with strings are something that the computer is supposed to just do. I think it is better to just write it the normal way and worry about optimizing later. If we find that this is a bottleneck, we might prefer instead to use some flavor of SmallString such as the one here: https://llvm.org/doxygen/classllvm_1_1SmallString.html

vinniefalco avatar Dec 07 '24 02:12 vinniefalco

I'm uncertain about the interface, but something that eliminates the need for a temporarily allocated string would be ideal. Just to clarify what I mean:

auto host_and_port = url.authority().encoded_host_and_port();
request.set(
    field::host,
    host_and_port.decoded_size(),
    [&](std::span<char> buffer)
    {
        // copy_to is a string_token
        host_and_port.decode({}, copy_to(buffer));
    });

But I agree this is not something we need to be concerned about at the moment, especially considering the allocated temporary will be deallocated immediately afterward.

ashtum avatar Dec 07 '24 06:12 ashtum

too much try-hard

vinniefalco avatar Dec 07 '24 15:12 vinniefalco