Skip to content

Commit

Permalink
Avoid extra string-copies in typecaster<absl::Cord>.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 662940213
  • Loading branch information
pybind11_abseil authors authored and copybara-github committed Aug 14, 2024
1 parent 4222c78 commit f94fabf
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pybind11_abseil/absl_casters.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <tuple>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -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();
}
};

Expand Down

0 comments on commit f94fabf

Please sign in to comment.