Skip to content

Commit cea168a

Browse files
committed
Refactor to store len and capacity instead of just pointers
This will help with random access and makes things a bit cleaner.
1 parent 043b0d3 commit cea168a

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

mypyc/lib-rt/librt_strings.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@
2020
typedef struct {
2121
PyObject_HEAD
2222
char *buf; // Beginning of the buffer
23-
char *ptr; // Current write location in the buffer
24-
char *end; // End of the buffer
23+
Py_ssize_t len; // Current length (number of bytes written)
24+
Py_ssize_t capacity; // Total capacity of the buffer
2525
char data[WRITER_EMBEDDED_BUF_LEN]; // Default buffer
2626
} BytesWriterObject;
2727

2828
#define _WRITE(data, type, v) \
2929
do { \
30-
*(type *)(((BytesWriterObject *)data)->ptr) = v; \
31-
((BytesWriterObject *)data)->ptr += sizeof(type); \
30+
*(type *)(((BytesWriterObject *)data)->buf + ((BytesWriterObject *)data)->len) = v; \
31+
((BytesWriterObject *)data)->len += sizeof(type); \
3232
} while (0)
3333

3434
static PyTypeObject BytesWriterType;
3535

3636
static bool
3737
_grow_buffer(BytesWriterObject *data, Py_ssize_t n) {
38-
Py_ssize_t index = data->ptr - data->buf;
39-
Py_ssize_t target = index + n;
40-
Py_ssize_t size = data->end - data->buf;
38+
Py_ssize_t target = data->len + n;
39+
Py_ssize_t size = data->capacity;
4140
Py_ssize_t old_size = size;
4241
do {
4342
size *= 2;
@@ -55,14 +54,13 @@ _grow_buffer(BytesWriterObject *data, Py_ssize_t n) {
5554
PyErr_NoMemory();
5655
return false;
5756
}
58-
data->ptr = data->buf + index;
59-
data->end = data->buf + size;
57+
data->capacity = size;
6058
return true;
6159
}
6260

6361
static inline bool
6462
ensure_bytes_writer_size(BytesWriterObject *data, Py_ssize_t n) {
65-
if (likely(data->end - data->ptr >= n)) {
63+
if (likely(data->capacity - data->len >= n)) {
6664
return true;
6765
} else {
6866
return _grow_buffer(data, n);
@@ -72,8 +70,8 @@ ensure_bytes_writer_size(BytesWriterObject *data, Py_ssize_t n) {
7270
static inline void
7371
BytesWriter_init_internal(BytesWriterObject *self) {
7472
self->buf = self->data;
75-
self->ptr = self->data;
76-
self->end = self->data + WRITER_EMBEDDED_BUF_LEN;
73+
self->len = 0;
74+
self->capacity = WRITER_EMBEDDED_BUF_LEN;
7775
}
7876

7977
static PyObject*
@@ -131,7 +129,7 @@ static PyObject*
131129
BytesWriter_getvalue_internal(PyObject *self)
132130
{
133131
BytesWriterObject *obj = (BytesWriterObject *)self;
134-
return PyBytes_FromStringAndSize(obj->buf, obj->ptr - obj->buf);
132+
return PyBytes_FromStringAndSize(obj->buf, obj->len);
135133
}
136134

137135
static PyObject*
@@ -154,7 +152,7 @@ BytesWriter_repr(BytesWriterObject *self)
154152
static PyObject*
155153
BytesWriter_getvalue(BytesWriterObject *self, PyObject *Py_UNUSED(ignored))
156154
{
157-
return PyBytes_FromStringAndSize(self->buf, self->ptr - self->buf);
155+
return PyBytes_FromStringAndSize(self->buf, self->len);
158156
}
159157

160158
static PyObject* BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames);
@@ -212,9 +210,8 @@ BytesWriter_write_internal(BytesWriterObject *self, PyObject *value) {
212210
// Write bytes content.
213211
if (!ensure_bytes_writer_size(self, size))
214212
return CPY_NONE_ERROR;
215-
char *ptr = ((BytesWriterObject *)self)->ptr;
216-
memcpy(ptr, data, size);
217-
((BytesWriterObject *)self)->ptr += size;
213+
memcpy(self->buf + self->len, data, size);
214+
self->len += size;
218215
return CPY_NONE;
219216
}
220217

0 commit comments

Comments
 (0)