-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Do not copy string content if possible while base64 encoding/decoding #7997
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
base: master
Are you sure you want to change the base?
Conversation
72c3d12
to
8bb108b
Compare
Looks pretty similar to other string_view changes. Any of the c folks want to weigh in? (@alessandrogario @Smjert @marcosd4h ) |
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 first needs to be rebased on master; the base64::encode
function has already been changed to support std::string_view
on master, so I think this leaves us with decode
only.
std::string decode(std::string&& encoded) { | ||
boost::erase_all(encoded, "\r\n"); | ||
boost::erase_all(encoded, "\n"); | ||
boost::trim_right_if(encoded, boost::is_any_of("=")); |
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.
Something I don't get from the original function is why we need to do this at all.
This seems an unnecessary performance hit to incur into, which might not be necessary to do (and we also have to scan the string again to understand if it's necessary to), when I think the caller should provide a valid base64 decodable string.
Main intent of this PR is to avoid string copying while encoding/decoding.
The
encode
change is quite trivial since it didn't copy the input.But the
decode
case is opposite: it needs to remove few symbols from the input string.So I decided to split code of
decode
function into 2 overloads: first that takes astring_view
and a special function that takes a rvalue reference and can change the string content.I believe that the added linear lookup with
find_first_of("\r\n=")
will not make things worse in comparison with previous code but now the encoded string is not copied every time.PTAL