]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-144157: Optimize bytes.translate() by deferring change detection (GH-144158)
authorGregory P. Smith <68491+gpshead@users.noreply.github.com>
Thu, 22 Jan 2026 17:21:07 +0000 (09:21 -0800)
committerGitHub <noreply@github.com>
Thu, 22 Jan 2026 17:21:07 +0000 (09:21 -0800)
commita966d94e76d91ef60f9912a98a3869f38ecd438b
tree0fc5e0a70388e56879cc3e16dc0c2d1020901676
parent77bf4ba732a8736910bd6b7ec5a58b8ee833c95f
gh-144157: Optimize bytes.translate() by deferring change detection (GH-144158)

Optimize bytes.translate() by deferring change detection

Move the equality check out of the hot loop to allow better compiler
optimization. Instead of checking each byte during translation, perform
a single memcmp at the end to determine if the input can be returned
unchanged.

This allows compilers to unroll and pipeline the loops, resulting in ~2x
throughput improvement for medium-to-large inputs (tested on an AMD zen2).
No change observed on small inputs.

It will also be faster for bytes subclasses as those do not need change
detection.
Misc/NEWS.d/next/Core_and_Builtins/2026-01-22-16-20-16.gh-issue-144157.dxyp7k.rst [new file with mode: 0644]
Objects/bytesobject.c