From 444af6577d913b60f22f1b3d335927e5a98e0a32 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Tue, 25 Nov 2025 16:41:12 +0000 Subject: [PATCH 1/9] WIP optimize append and write --- mypyc/lib-rt/librt_strings.c | 15 +------ mypyc/lib-rt/librt_strings.h | 61 ++++++++++++++++++++++++++- mypyc/lib-rt/mypyc_util.h | 4 ++ mypyc/primitives/librt_strings_ops.py | 4 +- 4 files changed, 67 insertions(+), 17 deletions(-) diff --git a/mypyc/lib-rt/librt_strings.c b/mypyc/lib-rt/librt_strings.c index dcad4137e7f11..d403934b89154 100644 --- a/mypyc/lib-rt/librt_strings.c +++ b/mypyc/lib-rt/librt_strings.c @@ -7,24 +7,11 @@ #include "librt_strings.h" #define CPY_BOOL_ERROR 2 -#define CPY_NONE_ERROR 2 -#define CPY_NONE 1 // // BytesWriter // -// Length of the default buffer embedded directly in a BytesWriter object -#define WRITER_EMBEDDED_BUF_LEN 512 - -typedef struct { - PyObject_HEAD - char *buf; // Beginning of the buffer - Py_ssize_t len; // Current length (number of bytes written) - Py_ssize_t capacity; // Total capacity of the buffer - char data[WRITER_EMBEDDED_BUF_LEN]; // Default buffer -} BytesWriterObject; - #define _WRITE(data, type, v) \ do { \ *(type *)(((BytesWriterObject *)data)->buf + ((BytesWriterObject *)data)->len) = v; \ @@ -426,7 +413,7 @@ librt_strings_module_exec(PyObject *m) (void *)BytesWriter_internal, (void *)BytesWriter_getvalue_internal, (void *)BytesWriter_append_internal, - (void *)BytesWriter_write_internal, + (void *)_grow_buffer, (void *)BytesWriter_type_internal, (void *)BytesWriter_len_internal, (void *)BytesWriter_truncate_internal, diff --git a/mypyc/lib-rt/librt_strings.h b/mypyc/lib-rt/librt_strings.h index 75eae8451e553..ef5a43c654b7a 100644 --- a/mypyc/lib-rt/librt_strings.h +++ b/mypyc/lib-rt/librt_strings.h @@ -28,12 +28,23 @@ import_librt_strings(void) static void *LibRTStrings_API[LIBRT_STRINGS_API_LEN]; +// Length of the default buffer embedded directly in a BytesWriter object +#define WRITER_EMBEDDED_BUF_LEN 512 + +typedef struct { + PyObject_HEAD + char *buf; // Beginning of the buffer + Py_ssize_t len; // Current length (number of bytes written) + Py_ssize_t capacity; // Total capacity of the buffer + char data[WRITER_EMBEDDED_BUF_LEN]; // Default buffer +} BytesWriterObject; + #define LibRTStrings_ABIVersion (*(int (*)(void)) LibRTStrings_API[0]) #define LibRTStrings_APIVersion (*(int (*)(void)) LibRTStrings_API[1]) #define LibRTStrings_BytesWriter_internal (*(PyObject* (*)(void)) LibRTStrings_API[2]) #define LibRTStrings_BytesWriter_getvalue_internal (*(PyObject* (*)(PyObject *source)) LibRTStrings_API[3]) #define LibRTStrings_BytesWriter_append_internal (*(char (*)(PyObject *source, uint8_t value)) LibRTStrings_API[4]) -#define LibRTStrings_BytesWriter_write_internal (*(char (*)(PyObject *source, PyObject *value)) LibRTStrings_API[5]) +#define LibRTStrings_ByteWriter_grow_buffer_internal (*(bool (*)(BytesWriterObject *obj, Py_ssize_t size)) LibRTStrings_API[5]) #define LibRTStrings_BytesWriter_type_internal (*(PyTypeObject* (*)(void)) LibRTStrings_API[6]) #define LibRTStrings_BytesWriter_len_internal (*(CPyTagged (*)(PyObject *self)) LibRTStrings_API[7]) #define LibRTStrings_BytesWriter_truncate_internal (*(char (*)(PyObject *self, int64_t size)) LibRTStrings_API[8]) @@ -75,6 +86,54 @@ static inline bool CPyBytesWriter_Check(PyObject *obj) { return Py_TYPE(obj) == LibRTStrings_BytesWriter_type_internal(); } +static inline bool +CPyBytesWriter_EnsureSize(BytesWriterObject *data, Py_ssize_t n) { + if (likely(data->capacity - data->len >= n)) { + return true; + } else { + return LibRTStrings_ByteWriter_grow_buffer_internal(data, n); + } +} + +static inline char +CPyBytesWriter_Append(PyObject *obj, uint8_t value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + // Store length in a local variable to enable additional optimizations + Py_ssize_t len = self->len; + if (!CPyBytesWriter_EnsureSize(self, 1)) + return CPY_NONE_ERROR; + self->buf[len] = value; + self->len = len + 1; + return CPY_NONE; +} + +static char +CPyBytesWriter_Write(PyObject *obj, PyObject *value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + const char *data; + Py_ssize_t size; + if (likely(PyBytes_Check(value))) { + data = PyBytes_AS_STRING(value); + size = PyBytes_GET_SIZE(value); + } else { + data = PyByteArray_AS_STRING(value); + size = PyByteArray_GET_SIZE(value); + } + // Write bytes content. + if (!CPyBytesWriter_EnsureSize(self, size)) + return CPY_NONE_ERROR; + if (size < 8) { + char *p = self->buf + self->len; + for (Py_ssize_t i = 0; i < size; i++) { + p[i] = data[i]; + } + } else { + memcpy(self->buf + self->len, data, size); + } + self->len += size; + return CPY_NONE; +} + #endif // MYPYC_EXPERIMENTAL #endif // LIBRT_STRINGS_H diff --git a/mypyc/lib-rt/mypyc_util.h b/mypyc/lib-rt/mypyc_util.h index 50a806c91a8a6..6715d67d96572 100644 --- a/mypyc/lib-rt/mypyc_util.h +++ b/mypyc/lib-rt/mypyc_util.h @@ -142,6 +142,10 @@ typedef PyObject CPyModule; // Error value for floats #define CPY_FLOAT_ERROR -113.0 +// Value for 'None' primitive type +#define CPY_NONE_ERROR 2 +#define CPY_NONE 1 + typedef void (*CPyVTableItem)(void); static inline CPyTagged CPyTagged_ShortFromInt(int x) { diff --git a/mypyc/primitives/librt_strings_ops.py b/mypyc/primitives/librt_strings_ops.py index 786784d15f60d..3545593f59cfe 100644 --- a/mypyc/primitives/librt_strings_ops.py +++ b/mypyc/primitives/librt_strings_ops.py @@ -38,7 +38,7 @@ name="write", arg_types=[bytes_writer_rprimitive, bytes_rprimitive], return_type=none_rprimitive, - c_function_name="LibRTStrings_BytesWriter_write_internal", + c_function_name="CPyBytesWriter_Write", error_kind=ERR_MAGIC, experimental=True, dependencies=[LIBRT_STRINGS], @@ -48,7 +48,7 @@ name="append", arg_types=[bytes_writer_rprimitive, uint8_rprimitive], return_type=none_rprimitive, - c_function_name="LibRTStrings_BytesWriter_append_internal", + c_function_name="CPyBytesWriter_Append", error_kind=ERR_MAGIC, experimental=True, dependencies=[LIBRT_STRINGS], From e6a6207d1985ff63fbdd17f33888c1f7af11606f Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Tue, 25 Nov 2025 17:07:16 +0000 Subject: [PATCH 2/9] Add inline primitive for len --- mypyc/lib-rt/librt_strings.c | 1 - mypyc/lib-rt/librt_strings.h | 11 ++++++++--- mypyc/primitives/librt_strings_ops.py | 2 +- mypyc/test-data/irbuild-librt-strings.test | 6 +++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/mypyc/lib-rt/librt_strings.c b/mypyc/lib-rt/librt_strings.c index d403934b89154..086e5fd8dde5f 100644 --- a/mypyc/lib-rt/librt_strings.c +++ b/mypyc/lib-rt/librt_strings.c @@ -415,7 +415,6 @@ librt_strings_module_exec(PyObject *m) (void *)BytesWriter_append_internal, (void *)_grow_buffer, (void *)BytesWriter_type_internal, - (void *)BytesWriter_len_internal, (void *)BytesWriter_truncate_internal, }; PyObject *c_api_object = PyCapsule_New((void *)librt_strings_api, "librt.strings._C_API", NULL); diff --git a/mypyc/lib-rt/librt_strings.h b/mypyc/lib-rt/librt_strings.h index ef5a43c654b7a..686453da36ec3 100644 --- a/mypyc/lib-rt/librt_strings.h +++ b/mypyc/lib-rt/librt_strings.h @@ -24,7 +24,7 @@ import_librt_strings(void) // Number of functions in the capsule API. If you add a new function, also increase // LIBRT_STRINGS_API_VERSION. -#define LIBRT_STRINGS_API_LEN 9 +#define LIBRT_STRINGS_API_LEN 8 static void *LibRTStrings_API[LIBRT_STRINGS_API_LEN]; @@ -46,8 +46,7 @@ typedef struct { #define LibRTStrings_BytesWriter_append_internal (*(char (*)(PyObject *source, uint8_t value)) LibRTStrings_API[4]) #define LibRTStrings_ByteWriter_grow_buffer_internal (*(bool (*)(BytesWriterObject *obj, Py_ssize_t size)) LibRTStrings_API[5]) #define LibRTStrings_BytesWriter_type_internal (*(PyTypeObject* (*)(void)) LibRTStrings_API[6]) -#define LibRTStrings_BytesWriter_len_internal (*(CPyTagged (*)(PyObject *self)) LibRTStrings_API[7]) -#define LibRTStrings_BytesWriter_truncate_internal (*(char (*)(PyObject *self, int64_t size)) LibRTStrings_API[8]) +#define LibRTStrings_BytesWriter_truncate_internal (*(char (*)(PyObject *self, int64_t size)) LibRTStrings_API[7]) static int import_librt_strings(void) @@ -134,6 +133,12 @@ CPyBytesWriter_Write(PyObject *obj, PyObject *value) { return CPY_NONE; } +static inline CPyTagged +CPyBytesWriter_Len(PyObject *obj) { + BytesWriterObject *self = (BytesWriterObject *)obj; + return (CPyTagged)self->len << 1; +} + #endif // MYPYC_EXPERIMENTAL #endif // LIBRT_STRINGS_H diff --git a/mypyc/primitives/librt_strings_ops.py b/mypyc/primitives/librt_strings_ops.py index 3545593f59cfe..062091d256814 100644 --- a/mypyc/primitives/librt_strings_ops.py +++ b/mypyc/primitives/librt_strings_ops.py @@ -66,7 +66,7 @@ name="builtins.len", arg_types=[bytes_writer_rprimitive], return_type=short_int_rprimitive, - c_function_name="LibRTStrings_BytesWriter_len_internal", + c_function_name="CPyBytesWriter_Len", error_kind=ERR_NEVER, experimental=True, dependencies=[LIBRT_STRINGS], diff --git a/mypyc/test-data/irbuild-librt-strings.test b/mypyc/test-data/irbuild-librt-strings.test index 21aba382cdd8d..b61649844de22 100644 --- a/mypyc/test-data/irbuild-librt-strings.test +++ b/mypyc/test-data/irbuild-librt-strings.test @@ -29,9 +29,9 @@ def bytes_writer_basics(): L0: r0 = LibRTStrings_BytesWriter_internal() b = r0 - r1 = LibRTStrings_BytesWriter_append_internal(b, 1) + r1 = CPyBytesWriter_Append(b, 1) r2 = b'foo' - r3 = LibRTStrings_BytesWriter_write_internal(b, r2) + r3 = CPyBytesWriter_Write(b, r2) n = 4 r4 = n & 1 r5 = r4 == 0 @@ -55,6 +55,6 @@ def bytes_writer_len(b): r0 :: short_int r1 :: i64 L0: - r0 = LibRTStrings_BytesWriter_len_internal(b) + r0 = CPyBytesWriter_Len(b) r1 = r0 >> 1 return r1 From f9a6691f0f41dbd5dfc848e7e726aabdeba19e40 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Tue, 25 Nov 2025 17:20:32 +0000 Subject: [PATCH 3/9] Simplify len implementation --- mypyc/lib-rt/librt_strings.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mypyc/lib-rt/librt_strings.h b/mypyc/lib-rt/librt_strings.h index 686453da36ec3..0069d0d7e8ad7 100644 --- a/mypyc/lib-rt/librt_strings.h +++ b/mypyc/lib-rt/librt_strings.h @@ -135,8 +135,7 @@ CPyBytesWriter_Write(PyObject *obj, PyObject *value) { static inline CPyTagged CPyBytesWriter_Len(PyObject *obj) { - BytesWriterObject *self = (BytesWriterObject *)obj; - return (CPyTagged)self->len << 1; + return (CPyTagged)((BytesWriterObject *)obj)->len << 1; } #endif // MYPYC_EXPERIMENTAL From 2fe8d43361945c66d18c609003d74c329409c45a Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 8 Dec 2025 16:32:16 +0000 Subject: [PATCH 4/9] Move to a separate file --- mypyc/ir/deps.py | 1 + mypyc/lib-rt/byteswriter_extra_ops.c | 27 ++++++++++++++ mypyc/lib-rt/byteswriter_extra_ops.h | 34 +++++++++++++++++ mypyc/lib-rt/librt_strings.h | 53 --------------------------- mypyc/primitives/librt_strings_ops.py | 10 +++-- 5 files changed, 68 insertions(+), 57 deletions(-) create mode 100644 mypyc/lib-rt/byteswriter_extra_ops.c create mode 100644 mypyc/lib-rt/byteswriter_extra_ops.h diff --git a/mypyc/ir/deps.py b/mypyc/ir/deps.py index b7747646628b1..9aee5d1b33e47 100644 --- a/mypyc/ir/deps.py +++ b/mypyc/ir/deps.py @@ -50,3 +50,4 @@ def get_header(self) -> str: LIBRT_BASE64: Final = Capsule("librt.base64") BYTES_EXTRA_OPS: Final = SourceDep("bytes_extra_ops.c") +BYTES_WRITER_EXTRA_OPS: Final = SourceDep("byteswriter_extra_ops.c") diff --git a/mypyc/lib-rt/byteswriter_extra_ops.c b/mypyc/lib-rt/byteswriter_extra_ops.c new file mode 100644 index 0000000000000..337300c0adbfe --- /dev/null +++ b/mypyc/lib-rt/byteswriter_extra_ops.c @@ -0,0 +1,27 @@ +#include "byteswriter_extra_ops.h" + +char CPyBytesWriter_Write(PyObject *obj, PyObject *value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + const char *data; + Py_ssize_t size; + if (likely(PyBytes_Check(value))) { + data = PyBytes_AS_STRING(value); + size = PyBytes_GET_SIZE(value); + } else { + data = PyByteArray_AS_STRING(value); + size = PyByteArray_GET_SIZE(value); + } + // Write bytes content. + if (!CPyBytesWriter_EnsureSize(self, size)) + return CPY_NONE_ERROR; + if (size < 8) { + char *p = self->buf + self->len; + for (Py_ssize_t i = 0; i < size; i++) { + p[i] = data[i]; + } + } else { + memcpy(self->buf + self->len, data, size); + } + self->len += size; + return CPY_NONE; +} diff --git a/mypyc/lib-rt/byteswriter_extra_ops.h b/mypyc/lib-rt/byteswriter_extra_ops.h new file mode 100644 index 0000000000000..09cd018d4e5f9 --- /dev/null +++ b/mypyc/lib-rt/byteswriter_extra_ops.h @@ -0,0 +1,34 @@ +#ifndef BYTESWRITER_EXTRA_OPS_H +#define BYTESWRITER_EXTRA_OPS_H + +#include "librt_strings.h" + +static inline CPyTagged +CPyBytesWriter_Len(PyObject *obj) { + return (CPyTagged)((BytesWriterObject *)obj)->len << 1; +} + +static inline bool +CPyBytesWriter_EnsureSize(BytesWriterObject *data, Py_ssize_t n) { + if (likely(data->capacity - data->len >= n)) { + return true; + } else { + return LibRTStrings_ByteWriter_grow_buffer_internal(data, n); + } +} + +static inline char +CPyBytesWriter_Append(PyObject *obj, uint8_t value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + // Store length in a local variable to enable additional optimizations + Py_ssize_t len = self->len; + if (!CPyBytesWriter_EnsureSize(self, 1)) + return CPY_NONE_ERROR; + self->buf[len] = value; + self->len = len + 1; + return CPY_NONE; +} + +char CPyBytesWriter_Write(PyObject *obj, PyObject *value); + +#endif diff --git a/mypyc/lib-rt/librt_strings.h b/mypyc/lib-rt/librt_strings.h index 0069d0d7e8ad7..1176bcb3c25bf 100644 --- a/mypyc/lib-rt/librt_strings.h +++ b/mypyc/lib-rt/librt_strings.h @@ -85,59 +85,6 @@ static inline bool CPyBytesWriter_Check(PyObject *obj) { return Py_TYPE(obj) == LibRTStrings_BytesWriter_type_internal(); } -static inline bool -CPyBytesWriter_EnsureSize(BytesWriterObject *data, Py_ssize_t n) { - if (likely(data->capacity - data->len >= n)) { - return true; - } else { - return LibRTStrings_ByteWriter_grow_buffer_internal(data, n); - } -} - -static inline char -CPyBytesWriter_Append(PyObject *obj, uint8_t value) { - BytesWriterObject *self = (BytesWriterObject *)obj; - // Store length in a local variable to enable additional optimizations - Py_ssize_t len = self->len; - if (!CPyBytesWriter_EnsureSize(self, 1)) - return CPY_NONE_ERROR; - self->buf[len] = value; - self->len = len + 1; - return CPY_NONE; -} - -static char -CPyBytesWriter_Write(PyObject *obj, PyObject *value) { - BytesWriterObject *self = (BytesWriterObject *)obj; - const char *data; - Py_ssize_t size; - if (likely(PyBytes_Check(value))) { - data = PyBytes_AS_STRING(value); - size = PyBytes_GET_SIZE(value); - } else { - data = PyByteArray_AS_STRING(value); - size = PyByteArray_GET_SIZE(value); - } - // Write bytes content. - if (!CPyBytesWriter_EnsureSize(self, size)) - return CPY_NONE_ERROR; - if (size < 8) { - char *p = self->buf + self->len; - for (Py_ssize_t i = 0; i < size; i++) { - p[i] = data[i]; - } - } else { - memcpy(self->buf + self->len, data, size); - } - self->len += size; - return CPY_NONE; -} - -static inline CPyTagged -CPyBytesWriter_Len(PyObject *obj) { - return (CPyTagged)((BytesWriterObject *)obj)->len << 1; -} - #endif // MYPYC_EXPERIMENTAL #endif // LIBRT_STRINGS_H diff --git a/mypyc/primitives/librt_strings_ops.py b/mypyc/primitives/librt_strings_ops.py index 062091d256814..3b28388e770c8 100644 --- a/mypyc/primitives/librt_strings_ops.py +++ b/mypyc/primitives/librt_strings_ops.py @@ -1,6 +1,6 @@ from typing import Final -from mypyc.ir.deps import LIBRT_STRINGS +from mypyc.ir.deps import LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER from mypyc.ir.rtypes import ( KNOWN_NATIVE_TYPES, @@ -41,7 +41,7 @@ c_function_name="CPyBytesWriter_Write", error_kind=ERR_MAGIC, experimental=True, - dependencies=[LIBRT_STRINGS], + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], ) method_op( @@ -51,7 +51,7 @@ c_function_name="CPyBytesWriter_Append", error_kind=ERR_MAGIC, experimental=True, - dependencies=[LIBRT_STRINGS], + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], ) method_op( @@ -60,6 +60,8 @@ return_type=none_rprimitive, c_function_name="LibRTStrings_BytesWriter_truncate_internal", error_kind=ERR_MAGIC, + experimental=True, + dependencies=[LIBRT_STRINGS], ) function_op( @@ -69,5 +71,5 @@ c_function_name="CPyBytesWriter_Len", error_kind=ERR_NEVER, experimental=True, - dependencies=[LIBRT_STRINGS], + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], ) From 3119162ae596b3957d1b7c9cf12c002803cad93e Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 8 Dec 2025 16:35:38 +0000 Subject: [PATCH 5/9] Fix build --- mypyc/lib-rt/librt_strings.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mypyc/lib-rt/librt_strings.c b/mypyc/lib-rt/librt_strings.c index 086e5fd8dde5f..d97d0f571f5b3 100644 --- a/mypyc/lib-rt/librt_strings.c +++ b/mypyc/lib-rt/librt_strings.c @@ -18,6 +18,8 @@ ((BytesWriterObject *)data)->len += sizeof(type); \ } while (0) +#ifdef MYPYC_EXPERIMENTAL + static PyTypeObject BytesWriterType; static bool @@ -377,6 +379,8 @@ BytesWriter_len_internal(PyObject *self) { return writer->len << 1; } +#endif + static PyMethodDef librt_strings_module_methods[] = { {NULL, NULL, 0, NULL} }; From 513153b78c6a1b6b6e7da37a33184047b9e8edbc Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 8 Dec 2025 16:36:44 +0000 Subject: [PATCH 6/9] Lint --- mypyc/primitives/librt_strings_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypyc/primitives/librt_strings_ops.py b/mypyc/primitives/librt_strings_ops.py index 3b28388e770c8..1120254e24ae5 100644 --- a/mypyc/primitives/librt_strings_ops.py +++ b/mypyc/primitives/librt_strings_ops.py @@ -1,6 +1,6 @@ from typing import Final -from mypyc.ir.deps import LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS +from mypyc.ir.deps import BYTES_WRITER_EXTRA_OPS, LIBRT_STRINGS from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER from mypyc.ir.rtypes import ( KNOWN_NATIVE_TYPES, From eca46b4980e223f055a80140e3c3432e12f75db2 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 8 Dec 2025 16:42:03 +0000 Subject: [PATCH 7/9] Polish --- mypyc/lib-rt/byteswriter_extra_ops.c | 3 +++ mypyc/lib-rt/librt_internal.c | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/mypyc/lib-rt/byteswriter_extra_ops.c b/mypyc/lib-rt/byteswriter_extra_ops.c index 337300c0adbfe..76bf41045b742 100644 --- a/mypyc/lib-rt/byteswriter_extra_ops.c +++ b/mypyc/lib-rt/byteswriter_extra_ops.c @@ -1,3 +1,6 @@ +// Primitives related to librt.strings.BytesWriter that get linked statically +// with compiled modules, instead of being called via a capsule. + #include "byteswriter_extra_ops.h" char CPyBytesWriter_Write(PyObject *obj, PyObject *value) { diff --git a/mypyc/lib-rt/librt_internal.c b/mypyc/lib-rt/librt_internal.c index fe18c541c11fc..9c57d8bef72c9 100644 --- a/mypyc/lib-rt/librt_internal.c +++ b/mypyc/lib-rt/librt_internal.c @@ -27,8 +27,6 @@ #define LONG_INT_TRAILER 15 #define CPY_BOOL_ERROR 2 -#define CPY_NONE_ERROR 2 -#define CPY_NONE 1 #define _CHECK_READ_BUFFER(data, err) if (unlikely(_check_read_buffer(data) == CPY_NONE_ERROR)) \ return err; From 86037229f4050b4b4cdffbad389802e80e06e7e1 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 8 Dec 2025 17:25:05 +0000 Subject: [PATCH 8/9] Update capsule version --- mypyc/lib-rt/librt_strings.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mypyc/lib-rt/librt_strings.h b/mypyc/lib-rt/librt_strings.h index 1176bcb3c25bf..e619435f52920 100644 --- a/mypyc/lib-rt/librt_strings.h +++ b/mypyc/lib-rt/librt_strings.h @@ -15,12 +15,12 @@ import_librt_strings(void) // ABI version -- only an exact match is compatible. This will only be changed in // very exceptional cases (likely never) due to strict backward compatibility // requirements. -#define LIBRT_STRINGS_ABI_VERSION 0 +#define LIBRT_STRINGS_ABI_VERSION 1 // API version -- more recent versions must maintain backward compatibility, i.e. // we can add new features but not remove or change existing features (unless // ABI version is changed, but see the comment above). - #define LIBRT_STRINGS_API_VERSION 1 + #define LIBRT_STRINGS_API_VERSION 2 // Number of functions in the capsule API. If you add a new function, also increase // LIBRT_STRINGS_API_VERSION. From 51f01c6dae12c1875591131e6a7177d3e6da4293 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 8 Dec 2025 17:25:13 +0000 Subject: [PATCH 9/9] Add comment --- mypyc/lib-rt/byteswriter_extra_ops.c | 1 + 1 file changed, 1 insertion(+) diff --git a/mypyc/lib-rt/byteswriter_extra_ops.c b/mypyc/lib-rt/byteswriter_extra_ops.c index 76bf41045b742..03827bf54fea7 100644 --- a/mypyc/lib-rt/byteswriter_extra_ops.c +++ b/mypyc/lib-rt/byteswriter_extra_ops.c @@ -18,6 +18,7 @@ char CPyBytesWriter_Write(PyObject *obj, PyObject *value) { if (!CPyBytesWriter_EnsureSize(self, size)) return CPY_NONE_ERROR; if (size < 8) { + // Loop tends to be faster for small sizes char *p = self->buf + self->len; for (Py_ssize_t i = 0; i < size; i++) { p[i] = data[i];