Skip to content

Commit e6a6207

Browse files
committed
Add inline primitive for len
1 parent 444af65 commit e6a6207

4 files changed

Lines changed: 12 additions & 8 deletions

File tree

mypyc/lib-rt/librt_strings.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ librt_strings_module_exec(PyObject *m)
415415
(void *)BytesWriter_append_internal,
416416
(void *)_grow_buffer,
417417
(void *)BytesWriter_type_internal,
418-
(void *)BytesWriter_len_internal,
419418
(void *)BytesWriter_truncate_internal,
420419
};
421420
PyObject *c_api_object = PyCapsule_New((void *)librt_strings_api, "librt.strings._C_API", NULL);

mypyc/lib-rt/librt_strings.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import_librt_strings(void)
2424

2525
// Number of functions in the capsule API. If you add a new function, also increase
2626
// LIBRT_STRINGS_API_VERSION.
27-
#define LIBRT_STRINGS_API_LEN 9
27+
#define LIBRT_STRINGS_API_LEN 8
2828

2929
static void *LibRTStrings_API[LIBRT_STRINGS_API_LEN];
3030

@@ -46,8 +46,7 @@ typedef struct {
4646
#define LibRTStrings_BytesWriter_append_internal (*(char (*)(PyObject *source, uint8_t value)) LibRTStrings_API[4])
4747
#define LibRTStrings_ByteWriter_grow_buffer_internal (*(bool (*)(BytesWriterObject *obj, Py_ssize_t size)) LibRTStrings_API[5])
4848
#define LibRTStrings_BytesWriter_type_internal (*(PyTypeObject* (*)(void)) LibRTStrings_API[6])
49-
#define LibRTStrings_BytesWriter_len_internal (*(CPyTagged (*)(PyObject *self)) LibRTStrings_API[7])
50-
#define LibRTStrings_BytesWriter_truncate_internal (*(char (*)(PyObject *self, int64_t size)) LibRTStrings_API[8])
49+
#define LibRTStrings_BytesWriter_truncate_internal (*(char (*)(PyObject *self, int64_t size)) LibRTStrings_API[7])
5150

5251
static int
5352
import_librt_strings(void)
@@ -134,6 +133,12 @@ CPyBytesWriter_Write(PyObject *obj, PyObject *value) {
134133
return CPY_NONE;
135134
}
136135

136+
static inline CPyTagged
137+
CPyBytesWriter_Len(PyObject *obj) {
138+
BytesWriterObject *self = (BytesWriterObject *)obj;
139+
return (CPyTagged)self->len << 1;
140+
}
141+
137142
#endif // MYPYC_EXPERIMENTAL
138143

139144
#endif // LIBRT_STRINGS_H

mypyc/primitives/librt_strings_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
name="builtins.len",
6767
arg_types=[bytes_writer_rprimitive],
6868
return_type=short_int_rprimitive,
69-
c_function_name="LibRTStrings_BytesWriter_len_internal",
69+
c_function_name="CPyBytesWriter_Len",
7070
error_kind=ERR_NEVER,
7171
experimental=True,
7272
dependencies=[LIBRT_STRINGS],

mypyc/test-data/irbuild-librt-strings.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def bytes_writer_basics():
2929
L0:
3030
r0 = LibRTStrings_BytesWriter_internal()
3131
b = r0
32-
r1 = LibRTStrings_BytesWriter_append_internal(b, 1)
32+
r1 = CPyBytesWriter_Append(b, 1)
3333
r2 = b'foo'
34-
r3 = LibRTStrings_BytesWriter_write_internal(b, r2)
34+
r3 = CPyBytesWriter_Write(b, r2)
3535
n = 4
3636
r4 = n & 1
3737
r5 = r4 == 0
@@ -55,6 +55,6 @@ def bytes_writer_len(b):
5555
r0 :: short_int
5656
r1 :: i64
5757
L0:
58-
r0 = LibRTStrings_BytesWriter_len_internal(b)
58+
r0 = CPyBytesWriter_Len(b)
5959
r1 = r0 >> 1
6060
return r1

0 commit comments

Comments
 (0)