Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`bytes.translate` now allows the compiler to unroll its loop more
usefully for a 2x speedup in the common no-deletions specified case.
14 changes: 9 additions & 5 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2237,11 +2237,15 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
/* If no deletions are required, use faster code */
for (i = inlen; --i >= 0; ) {
c = Py_CHARMASK(*input++);
if (Py_CHARMASK((*output++ = table_chars[c])) != c)
changed = 1;
}
if (!changed && PyBytes_CheckExact(input_obj)) {
Py_SETREF(result, Py_NewRef(input_obj));
*output++ = table_chars[c];
}
/* Check if anything changed (for returning original object) */
/* We save this check until the end so that the compiler will */
/* unroll the loop above leading to MUCH faster code. */
if (PyBytes_CheckExact(input_obj)) {
if (memcmp(PyBytes_AS_STRING(input_obj), output_start, inlen) == 0) {
Py_SETREF(result, Py_NewRef(input_obj));
}
}
PyBuffer_Release(&del_table_view);
PyBuffer_Release(&table_view);
Expand Down
Loading