Skip to content

The header and write callback now uses a std::string_view as argument for data to avoid copying #1081

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

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

COM8
Copy link
Member

@COM8 COM8 commented Jul 10, 2024

Old

std::function<bool(std::string data, intptr_t userdata)> callback;

New

std::function<bool(const std::string_view& data, intptr_t userdata)> callback;

With the new way we avoid copying the payload.

@COM8 COM8 added this to the CPR 1.11.0 milestone Jul 10, 2024
@COM8 COM8 self-assigned this Jul 10, 2024
@COM8 COM8 merged commit b5a21de into master Jul 10, 2024
32 of 105 checks passed
@COM8 COM8 deleted the feature/callback_user_string_view branch July 10, 2024 14:33
@wonbinbk
Copy link

How does passing a string_view by reference can avoid copying the payload?

@COM8
Copy link
Member Author

COM8 commented Nov 28, 2024

Every time you make a call to the following function data will be copied.

std::function<bool(std::string data, intptr_t userdata)> callback;

This means if e.g. data is multiple MB of size this copying is awfully inefficient.

If we now change it to const std::string_view& data best case we only "copy" 8 byte (on x86_64 systems) for the pointer we are using to access data.

@wonbinbk
Copy link

wonbinbk commented Dec 3, 2024

Should we pass string_view by value here instead of const&?

https://quuxplusone.github.io/blog/2021/11/09/pass-string-view-by-value/

Also it's worth to note that string_view depends on life time of the actual array it points too, since it's used in a constructor here, I think we should be careful about string life time as well.

Thanks for your time and effort on this library @COM8

@COM8
Copy link
Member Author

COM8 commented Dec 4, 2024

I guess this was a classical case where I extremely successfully applied over optimization. Would you like to create a PR to convert const std::string_view& to std::string_view?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WriteCallback should take a string_view as argument instead of a std::string to avoid a copy
2 participants