Skip to content

Commit 284e368

Browse files
committed
optimize c funcs
1 parent 1b34c8f commit 284e368

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

mypyc/lib-rt/bytes_ops.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,14 @@ PyObject *CPyBytes_RjustDefaultFill(PyObject *self, CPyTagged width) {
173173
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width);
174174
Py_ssize_t len = PyBytes_Size(self);
175175
if (width_size_t <= len) {
176-
return PyBytes_FromStringAndSize(PyBytes_AsString(self), len);
176+
Py_INCREF(self);
177+
return self;
177178
}
178-
// should this be a constant?
179-
PyObject *fillbyte = PyBytes_FromStringAndSize(" ", 1);
180-
// can we optimize out the above line and the below line?
181-
char fill = PyBytes_AsString(fillbyte)[0];
182179
Py_ssize_t pad = width_size_t - len;
183180
PyObject *result = PyBytes_FromStringAndSize(NULL, width_size_t);
184181
if (!result) return NULL;
185182
char *res_buf = PyBytes_AsString(result);
186-
memset(res_buf, fill, pad);
183+
memset(res_buf, ' ', pad);
187184
memcpy(res_buf + pad, PyBytes_AsString(self), len);
188185
return result;
189186
}
@@ -201,7 +198,8 @@ PyObject *CPyBytes_RjustCustomFill(PyObject *self, CPyTagged width, PyObject *fi
201198
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width);
202199
Py_ssize_t len = PyBytes_Size(self);
203200
if (width_size_t <= len) {
204-
return PyBytes_FromStringAndSize(PyBytes_AsString(self), len);
201+
Py_INCREF(self);
202+
return self;
205203
}
206204
char fill = PyBytes_AsString(fillbyte)[0];
207205
Py_ssize_t pad = width_size_t - len;
@@ -222,18 +220,15 @@ PyObject *CPyBytes_LjustDefaultFill(PyObject *self, CPyTagged width) {
222220
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width);
223221
Py_ssize_t len = PyBytes_Size(self);
224222
if (width_size_t <= len) {
225-
return PyBytes_FromStringAndSize(PyBytes_AsString(self), len);
223+
Py_INCREF(self);
224+
return self;
226225
}
227-
// should this be a constant?
228-
PyObject *fillbyte = PyBytes_FromStringAndSize(" ", 1);
229-
// can we optimize out the above line and the below line?
230-
char fill = PyBytes_AsString(fillbyte)[0];
231226
Py_ssize_t pad = width_size_t - len;
232227
PyObject *result = PyBytes_FromStringAndSize(NULL, width_size_t);
233228
if (!result) return NULL;
234229
char *res_buf = PyBytes_AsString(result);
235230
memcpy(res_buf, PyBytes_AsString(self), len);
236-
memset(res_buf + len, fill, pad);
231+
memset(res_buf + len, ' ', pad);
237232
return result;
238233
}
239234

@@ -250,7 +245,8 @@ PyObject *CPyBytes_LjustCustomFill(PyObject *self, CPyTagged width, PyObject *fi
250245
Py_ssize_t width_size_t = CPyTagged_AsSsize_t(width);
251246
Py_ssize_t len = PyBytes_Size(self);
252247
if (width_size_t <= len) {
253-
return PyBytes_FromStringAndSize(PyBytes_AsString(self), len);
248+
Py_INCREF(self);
249+
return self;
254250
}
255251
char fill = PyBytes_AsString(fillbyte)[0];
256252
Py_ssize_t pad = width_size_t - len;

0 commit comments

Comments
 (0)