Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-97016: Convert PyBytes_AS_STRING() to function
Convert the following static inline functions to regular functions:

* PyByteArray_AS_STRING()
* PyByteArray_GET_SIZE()
* PyBytes_AS_STRING()
* PyBytes_GET_SIZE()

Remove the _PyByteArray_empty_string variable. It was excluded from
the limited C API.

In bytesobject.c and bytearrayobject.c, add static inline functions
and use them for best performance:

* _PyByteArray_AS_STRING()
* _PyByteArray_GET_SIZE()
* _PyBytes_AS_STRING()
* _PyBytes_GET_SIZE()
  • Loading branch information
vstinner committed Sep 22, 2022
commit 72346a4b1e1ab4ca58f184b1371dab32e625b018
16 changes: 2 additions & 14 deletions Include/cpython/bytearrayobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,12 @@ typedef struct {
Py_ssize_t ob_exports; /* How many buffer exports */
} PyByteArrayObject;

PyAPI_DATA(char) _PyByteArray_empty_string[];

/* Macros and static inline functions, trading safety for speed */
#define _PyByteArray_CAST(op) \
(assert(PyByteArray_Check(op)), _Py_CAST(PyByteArrayObject*, op))

static inline char* PyByteArray_AS_STRING(PyObject *op)
{
PyByteArrayObject *self = _PyByteArray_CAST(op);
if (Py_SIZE(self)) {
return self->ob_start;
}
return _PyByteArray_empty_string;
}
PyAPI_DATA(char*) PyByteArray_AS_STRING(PyObject *op);
#define PyByteArray_AS_STRING(self) PyByteArray_AS_STRING(_PyObject_CAST(self))

static inline Py_ssize_t PyByteArray_GET_SIZE(PyObject *op) {
PyByteArrayObject *self = _PyByteArray_CAST(op);
return Py_SIZE(self);
}
PyAPI_DATA(Py_ssize_t) PyByteArray_GET_SIZE(PyObject *op);
#define PyByteArray_GET_SIZE(self) PyByteArray_GET_SIZE(_PyObject_CAST(self))
14 changes: 2 additions & 12 deletions Include/cpython/bytesobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,10 @@ PyAPI_FUNC(PyObject*) _PyBytes_FromHex(
PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
const char *, const char **);

/* Macros and static inline functions, trading safety for speed */
#define _PyBytes_CAST(op) \
(assert(PyBytes_Check(op)), _Py_CAST(PyBytesObject*, op))

static inline char* PyBytes_AS_STRING(PyObject *op)
{
return _PyBytes_CAST(op)->ob_sval;
}
PyAPI_FUNC(char*) PyBytes_AS_STRING(PyObject *op);
#define PyBytes_AS_STRING(op) PyBytes_AS_STRING(_PyObject_CAST(op))

static inline Py_ssize_t PyBytes_GET_SIZE(PyObject *op) {
PyBytesObject *self = _PyBytes_CAST(op);
return Py_SIZE(self);
}
PyAPI_FUNC(Py_ssize_t) PyBytes_GET_SIZE(PyObject *op);
#define PyBytes_GET_SIZE(self) PyBytes_GET_SIZE(_PyObject_CAST(self))

/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Convert the following static inline functions to regular functions:

* :c:func:`PyByteArray_AS_STRING()`
* :c:func:`PyByteArray_GET_SIZE()`
* :c:func:`PyBytes_AS_STRING()`
* :c:func:`PyBytes_GET_SIZE()`

Remove the ``_PyByteArray_empty_string`` variable. It was excluded from the
limited C API. Patch by Victor Stinner.
Loading