Commit baadf890 by pybind11_abseil authors Committed by Copybara-Service

Avoid extra string-copies in `typecaster<absl::Cord>`.

PiperOrigin-RevId: 663348039
parent 4222c78a
......@@ -42,6 +42,7 @@
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <tuple>
#include <type_traits>
#include <vector>
......@@ -634,7 +635,15 @@ struct type_caster<absl::Cord> {
return str(std::string(src)).release();
}
#endif
return bytes(std::string(src)).release();
bytes data(nullptr, src.size());
// Cord::CopyToArray is not always available so we need to copy
// the cord manually.
char* ptr = PyBytes_AS_STRING(data.ptr());
for (absl::string_view chunk : src.Chunks()) {
std::memcpy(ptr, chunk.data(), chunk.size());
ptr += chunk.size();
}
return data.release();
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment