Skip to content

gh-100239: specialize bytes binary operations#149458

Open
Wulian233 wants to merge 2 commits intopython:mainfrom
Wulian233:bytes
Open

gh-100239: specialize bytes binary operations#149458
Wulian233 wants to merge 2 commits intopython:mainfrom
Wulian233:bytes

Conversation

@Wulian233
Copy link
Copy Markdown
Contributor

@Wulian233 Wulian233 commented May 6, 2026

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}")

Comment thread Python/specialize.c Outdated
Comment thread Python/specialize.c Outdated
}

static PyObject *
bytes_slice_subscr(PyObject *lhs, PyObject *rhs)
Copy link
Copy Markdown
Contributor

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?

Comment thread Python/specialize.c Outdated
Copy link
Copy Markdown
Member

@corona10 corona10 left a 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.

@eendebakpt
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.

@read-the-docs-community
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants