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-100239: specialize bytes binary operations by Wulian233 · Pull Request #149458 · python/cpython

Conversation

@Wulian233

@Wulian233 Wulian233 commented May 6, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

The following was generated by Codex:

  • bytes[3]: 20,000,000 iterations
  • 7 repeats
  • PYTHON_UOPS_OPTIMIZE=0
  • compared normal specialization vs PYTHON_SPECIALIZATION_OFF=1

Results:

Benchmark Specialized median Unspecialized median Speedup
bytes[3] 0.513025s 0.701673s ~26.9% faster
Details
import statistics
import time

N = 20_000_000
REPEAT = 7


def bench_bytes_index():
    b = b"foobar"
    x = 0
    for _ in range(N):
        x = b[3]
    return x


# Warm up for specialization.
for _ in range(5):
    bench_bytes_index()

samples = []
for _ in range(REPEAT):
    t0 = time.perf_counter()
    result = bench_bytes_index()
    samples.append(time.perf_counter() - t0)

print("result", result)
print("samples", " ".join(f"{s:.6f}" for s in samples))
print("median", f"{statistics.median(samples):.6f}")
  • Issue: Specialize long tail of binary operations using a table. #100239

Sorry, something went wrong.

Uh oh!

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

All reactions
@Wulian233
pythongh-100239: specialize bytes binary operations
e0173f1
@bedevere-app bedevere-app Bot mentioned this pull request May 6, 2026
Specialize long tail of binary operations using a table. #100239
Open
@bedevere-app bedevere-app Bot added the awaiting review label May 6, 2026
eendebakpt
eendebakpt reviewed May 6, 2026
View reviewed changes
Comment thread Python/specialize.c Resolved

Uh oh!

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

Comment thread Python/specialize.c Outdated
}

static PyObject *
bytes_slice_subscr(PyObject *lhs, PyObject *rhs)

@eendebakpt eendebakpt May 6, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation here is seems copied from bytes_subscript (in byteobject.c) and still has a large overlap. Can you refactor the bytes_subscript into multiple methods so that the method here only becomes a very thin wrapper?

Sorry, something went wrong.

Uh oh!

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

All reactions
StanFromIreland
StanFromIreland reviewed May 6, 2026
View reviewed changes
Comment thread Python/specialize.c Resolved

Uh oh!

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

corona10
corona10 reviewed May 6, 2026
View reviewed changes

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we actually need this specialization. Is there any metrics that that this operation is executed frequently in performance benchmark.
We can add tons of specialization it we want, but IIUC, we just pick few of them.

Sorry, something went wrong.

Uh oh!

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

All reactions
@eendebakpt

eendebakpt commented May 7, 2026

Copy link
Copy Markdown
Contributor

I am not sure we actually need this specialization. Is there any metrics that that this operation is executed frequently in performance benchmark. We can add tons of specialization it we want, but IIUC, we just pick few of them.

I agree we should limit the number of specializations. The bytes indexing seems worthwhile atm, the bytes slicing not so much.

Another option: reconsider an extension mechanism. It was implemented once and reverted (because of a python release date and some technical worries): #133395. If we can get similar performance improvements with the extension mechanism, when we can leave which ops to specialize to the user and outside cpython.

All reactions

Sorry, something went wrong.

Uh oh!

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

@Wulian233
fix
c7dfa59
@read-the-docs-community

read-the-docs-community Bot commented May 7, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32582538 | 📁 Comparing c7dfa59 against main (b3cfd36)

  🔍 Preview build  

26 files changed · ± 26 modified

± Modified

  • c-api/exceptions.html
  • c-api/index.html
  • c-api/interp-lifecycle.html
  • c-api/stable.html
  • c-api/threads.html
  • deprecations/index.html
  • deprecations/pending-removal-in-3.17.html
  • deprecations/pending-removal-in-3.19.html
  • howto/free-threading-extensions.html
  • howto/perf_profiling.html
  • and 16 more...
All reactions

Sorry, something went wrong.

Uh oh!

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

@Wulian233 Wulian233 closed this May 24, 2026
@Wulian233
Wulian233 deleted the bytes branch May 24, 2026 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eendebakpt eendebakpt eendebakpt left review comments
@corona10 corona10 corona10 left review comments
@StanFromIreland StanFromIreland StanFromIreland left review comments

Uh oh!

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

Assignees

No one assigned

Labels

awaiting review

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.

4 participants

@Wulian233 @eendebakpt @corona10 @StanFromIreland
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.