Skip to content

Commit 2fe8d43

Browse files
committed
Move to a separate file
1 parent f9a6691 commit 2fe8d43

5 files changed

Lines changed: 68 additions & 57 deletions

File tree

mypyc/ir/deps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ def get_header(self) -> str:
5050
LIBRT_BASE64: Final = Capsule("librt.base64")
5151

5252
BYTES_EXTRA_OPS: Final = SourceDep("bytes_extra_ops.c")
53+
BYTES_WRITER_EXTRA_OPS: Final = SourceDep("byteswriter_extra_ops.c")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "byteswriter_extra_ops.h"
2+
3+
char CPyBytesWriter_Write(PyObject *obj, PyObject *value) {
4+
BytesWriterObject *self = (BytesWriterObject *)obj;
5+
const char *data;
6+
Py_ssize_t size;
7+
if (likely(PyBytes_Check(value))) {
8+
data = PyBytes_AS_STRING(value);
9+
size = PyBytes_GET_SIZE(value);
10+
} else {
11+
data = PyByteArray_AS_STRING(value);
12+
size = PyByteArray_GET_SIZE(value);
13+
}
14+
// Write bytes content.
15+
if (!CPyBytesWriter_EnsureSize(self, size))
16+
return CPY_NONE_ERROR;
17+
if (size < 8) {
18+
char *p = self->buf + self->len;
19+
for (Py_ssize_t i = 0; i < size; i++) {
20+
p[i] = data[i];
21+
}
22+
} else {
23+
memcpy(self->buf + self->len, data, size);
24+
}
25+
self->len += size;
26+
return CPY_NONE;
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef BYTESWRITER_EXTRA_OPS_H
2+
#define BYTESWRITER_EXTRA_OPS_H
3+
4+
#include "librt_strings.h"
5+
6+
static inline CPyTagged
7+
CPyBytesWriter_Len(PyObject *obj) {
8+
return (CPyTagged)((BytesWriterObject *)obj)->len << 1;
9+
}
10+
11+
static inline bool
12+
CPyBytesWriter_EnsureSize(BytesWriterObject *data, Py_ssize_t n) {
13+
if (likely(data->capacity - data->len >= n)) {
14+
return true;
15+
} else {
16+
return LibRTStrings_ByteWriter_grow_buffer_internal(data, n);
17+
}
18+
}
19+
20+
static inline char
21+
CPyBytesWriter_Append(PyObject *obj, uint8_t value) {
22+
BytesWriterObject *self = (BytesWriterObject *)obj;
23+
// Store length in a local variable to enable additional optimizations
24+
Py_ssize_t len = self->len;
25+
if (!CPyBytesWriter_EnsureSize(self, 1))
26+
return CPY_NONE_ERROR;
27+
self->buf[len] = value;
28+
self->len = len + 1;
29+
return CPY_NONE;
30+
}
31+
32+
char CPyBytesWriter_Write(PyObject *obj, PyObject *value);
33+
34+
#endif

mypyc/lib-rt/librt_strings.h

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -85,59 +85,6 @@ static inline bool CPyBytesWriter_Check(PyObject *obj) {
8585
return Py_TYPE(obj) == LibRTStrings_BytesWriter_type_internal();
8686
}
8787

88-
static inline bool
89-
CPyBytesWriter_EnsureSize(BytesWriterObject *data, Py_ssize_t n) {
90-
if (likely(data->capacity - data->len >= n)) {
91-
return true;
92-
} else {
93-
return LibRTStrings_ByteWriter_grow_buffer_internal(data, n);
94-
}
95-
}
96-
97-
static inline char
98-
CPyBytesWriter_Append(PyObject *obj, uint8_t value) {
99-
BytesWriterObject *self = (BytesWriterObject *)obj;
100-
// Store length in a local variable to enable additional optimizations
101-
Py_ssize_t len = self->len;
102-
if (!CPyBytesWriter_EnsureSize(self, 1))
103-
return CPY_NONE_ERROR;
104-
self->buf[len] = value;
105-
self->len = len + 1;
106-
return CPY_NONE;
107-
}
108-
109-
static char
110-
CPyBytesWriter_Write(PyObject *obj, PyObject *value) {
111-
BytesWriterObject *self = (BytesWriterObject *)obj;
112-
const char *data;
113-
Py_ssize_t size;
114-
if (likely(PyBytes_Check(value))) {
115-
data = PyBytes_AS_STRING(value);
116-
size = PyBytes_GET_SIZE(value);
117-
} else {
118-
data = PyByteArray_AS_STRING(value);
119-
size = PyByteArray_GET_SIZE(value);
120-
}
121-
// Write bytes content.
122-
if (!CPyBytesWriter_EnsureSize(self, size))
123-
return CPY_NONE_ERROR;
124-
if (size < 8) {
125-
char *p = self->buf + self->len;
126-
for (Py_ssize_t i = 0; i < size; i++) {
127-
p[i] = data[i];
128-
}
129-
} else {
130-
memcpy(self->buf + self->len, data, size);
131-
}
132-
self->len += size;
133-
return CPY_NONE;
134-
}
135-
136-
static inline CPyTagged
137-
CPyBytesWriter_Len(PyObject *obj) {
138-
return (CPyTagged)((BytesWriterObject *)obj)->len << 1;
139-
}
140-
14188
#endif // MYPYC_EXPERIMENTAL
14289

14390
#endif // LIBRT_STRINGS_H

mypyc/primitives/librt_strings_ops.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Final
22

3-
from mypyc.ir.deps import LIBRT_STRINGS
3+
from mypyc.ir.deps import LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS
44
from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER
55
from mypyc.ir.rtypes import (
66
KNOWN_NATIVE_TYPES,
@@ -41,7 +41,7 @@
4141
c_function_name="CPyBytesWriter_Write",
4242
error_kind=ERR_MAGIC,
4343
experimental=True,
44-
dependencies=[LIBRT_STRINGS],
44+
dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS],
4545
)
4646

4747
method_op(
@@ -51,7 +51,7 @@
5151
c_function_name="CPyBytesWriter_Append",
5252
error_kind=ERR_MAGIC,
5353
experimental=True,
54-
dependencies=[LIBRT_STRINGS],
54+
dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS],
5555
)
5656

5757
method_op(
@@ -60,6 +60,8 @@
6060
return_type=none_rprimitive,
6161
c_function_name="LibRTStrings_BytesWriter_truncate_internal",
6262
error_kind=ERR_MAGIC,
63+
experimental=True,
64+
dependencies=[LIBRT_STRINGS],
6365
)
6466

6567
function_op(
@@ -69,5 +71,5 @@
6971
c_function_name="CPyBytesWriter_Len",
7072
error_kind=ERR_NEVER,
7173
experimental=True,
72-
dependencies=[LIBRT_STRINGS],
74+
dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS],
7375
)

0 commit comments

Comments
 (0)