Skip to content

Commit

Permalink
clif: Remove copy when passing bytes/str to absl::Cord.
Browse files Browse the repository at this point in the history
#perf

PiperOrigin-RevId: 653698975
  • Loading branch information
CLIF Team authored and Ralf W. Grosse-Kunstleve committed Aug 27, 2024
1 parent a71818b commit 012f58e
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions clif/python/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@
// limitations under the License.

#include "clif/python/types.h"

#include <Python.h>

#include <climits>
#include <cstddef>

#include "absl/log/check.h"
#include "absl/numeric/int128.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"

namespace clif {

Expand Down Expand Up @@ -358,7 +366,6 @@ namespace py {
// bytes/unicode
template<typename C>
bool ObjToStr(PyObject* py, C copy) {
#if PY_VERSION_HEX >= 0x03030000
const char* data;
Py_ssize_t length;
if (PyUnicode_Check(py)) {
Expand All @@ -373,20 +380,6 @@ bool ObjToStr(PyObject* py, C copy) {
}
copy(data, length);
return true;
#else
bool decref = false;
if (PyUnicode_Check(py)) {
py = PyUnicode_AsUTF8String(py);
if (!py) return false;
decref = true;
} else if (!PyBytes_Check(py)) {
PyErr_SetString(PyExc_TypeError, "expecting str");
return false;
}
copy(PyBytes_AS_STRING(py), PyBytes_GET_SIZE(py));
if (decref) Py_DECREF(py);
return true;
#endif
}
} // namespace py

Expand Down Expand Up @@ -436,8 +429,14 @@ bool Clif_PyObjAs(PyObject* p, absl::string_view* c) {

bool Clif_PyObjAs(PyObject* p, absl::Cord* c) {
CHECK(c != nullptr);
return py::ObjToStr(p, [c](const char* data, size_t length) {
*c = absl::string_view(data, length);
return py::ObjToStr(p, [p, c](const char* data, size_t length) {
Py_INCREF(p);
*c = absl::MakeCordFromExternal(
absl::string_view(data, length), [p](absl::string_view) {
PyGILState_STATE gstate = PyGILState_Ensure();
Py_DECREF(p);
PyGILState_Release(gstate);
});
});
}

Expand Down

0 comments on commit 012f58e

Please sign in to comment.