GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-160931: Speed up bytes construction from exact bytes and int by cmaloney · Pull Request #160934 · python/cpython

Conversation

@cmaloney

@cmaloney cmaloney commented Oct 29, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Since 3.11 added bytes.__bytes__, bytes(b) for an exact bytes binds and calls that method only to get b back. Return an exact bytes argument directly, like PyObject_Bytes() and PyBytes_FromObject() do, and skip the lookup, which always misses, for an exact int.

How bytes(x) resolves is unchanged: both types are immutable, so the skipped lookup and call could only ever produce the same result, and subclasses, including bool, still look up __bytes__ first. These cases just get there faster.

Could inline the PyLong as another if but that makes a bigger diff and the runtime is very close to just adding the !PyLong_CheckExact case.

pyperf, PGO+LTO, interleaved A/B on 19270d3 (A/A spread <= 1.05x):

Benchmark main patch
bytes(b) 28.5 ns 12.8 ns: 2.22x faster
Sub(b) 51.7 ns 36.0 ns: 1.44x faster
bytes(16) 23.5 ns 21.0 ns: 1.12x faster
bytes(hb) 38.7 ns 38.0 ns: 1.02x faster
bytes(s, 'ascii') 26.5 ns 27.1 ns: 1.02x slower
bytes() 10.7 ns 11.1 ns: 1.03x slower

Not significant: bytes(ba), bytes(l), bytes(mv). The small slowdowns (1.02-1.03x) are within the noise on my machine.

import pyperf

runner = pyperf.Runner()
for stmt in ["bytes(b)", "bytes(16)", "bytes(ba)", "bytes(mv)", "bytes(l)",
             "bytes(s, 'ascii')", "bytes(hb)", "Sub(b)", "bytes()"]:
    runner.timeit(stmt, stmt,
                  "b = b'abcdefghijklmnop'; ba = bytearray(b); mv = memoryview(b); "
                  "l = list(range(16)); s = 'abcdefghijklmnop'\n"
                  "class Sub(bytes): pass\n"
                  "class HB:\n    def __bytes__(self): return b\n"
                  "hb = HB()")
  • Issue: Skip the __bytes__ lookup in bytes() for exact bytes and int #160931

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@cmaloney @claude
pythongh-160931: Speed up bytes construction from exact bytes and int
7c2bb06
Since 3.11 added `bytes.__bytes__`, bytes(b) for an exact bytes binds
and calls that method only to get b back.  Return an exact bytes
argument directly, like PyObject_Bytes() and PyBytes_FromObject() do,
and skip the lookup, which always misses, for an exact int.

How bytes(x) resolves is unchanged: both types are immutable, so the
skipped lookup and call could only ever produce the same result, and
subclasses, including bool, still look up `__bytes__` first.  These
cases just get there faster.

pyperf, PGO+LTO, interleaved A/B on 19270d3 (A/A spread <= 1.05x):

| Benchmark         | main    | patch                 |
|-------------------|:-------:|:---------------------:|
| bytes(b)          | 28.5 ns | 12.8 ns: 2.22x faster |
| Sub(b)            | 51.7 ns | 36.0 ns: 1.44x faster |
| bytes(16)         | 23.5 ns | 21.0 ns: 1.12x faster |
| bytes(hb)         | 38.7 ns | 38.0 ns: 1.02x faster |
| bytes(s, 'ascii') | 26.5 ns | 27.1 ns: 1.02x slower |
| bytes()           | 10.7 ns | 11.1 ns: 1.03x slower |

Not significant: bytes(ba), bytes(l), bytes(mv).  The small slowdowns
(1.02-1.03x) are within the noise on my machine.

    import pyperf

    runner = pyperf.Runner()
    for stmt in ["bytes(b)", "bytes(16)", "bytes(ba)", "bytes(mv)", "bytes(l)",
                 "bytes(s, 'ascii')", "bytes(hb)", "Sub(b)", "bytes()"]:
        runner.timeit(stmt, stmt,
                      "b = b'abcdefghijklmnop'; ba = bytearray(b); mv = memoryview(b); "
                      "l = list(range(16)); s = 'abcdefghijklmnop'\n"
                      "class Sub(bytes): pass\n"
                      "class HB:\n    def __bytes__(self): return b\n"
                      "hb = HB()")

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bedevere-app bedevere-app Bot added awaiting review type-feature A feature request or enhancement labels Oct 29, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Oct 29, 2026
Skip the __bytes__ lookup in bytes() for exact bytes and int #160931
Open
@cmaloney cmaloney added the skip news label Oct 29, 2026
@cmaloney
Remove NEWS
6805424
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review skip news type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@cmaloney
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.