You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Skip the __bytes__ lookup in bytes() for exact bytes and int #160931
Since 3.11 added bytes.__bytes__ (gh-68422), bytes(b) for an exact bytes looks up __bytes__, creates a bound method and calls it, only to get b back. For an exact int the lookup always misses, because int has no __bytes__.
Both types are immutable, so bytes_new_impl() can check for them first:
return an exact bytes argument directly, as PyObject_Bytes() and PyBytes_FromObject() already do;
skip the lookup for an exact int.
Subclasses, including bool, still look up __bytes__, so there's no behaviour change.
That makes bytes(b) about 2.2x faster, a bytes subclass Sub(b) about 1.4x, and bytes(16) about 1.1x. Other bytes() calls and the controls are unchanged (pyperf, PGO+LTO).
Activity