Skip to content

gh-144569: Avoid creating temporary objects in BINARY_SLICE for bytes - #156318

Open
koxudaxi wants to merge 2 commits into
python:mainfrom
koxudaxi:gh-144569-bytes-binary-slice
Open

gh-144569: Avoid creating temporary objects in BINARY_SLICE for bytes#156318
koxudaxi wants to merge 2 commits into
python:mainfrom
koxudaxi:gh-144569-bytes-binary-slice

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Add an exact bytes fast path to BINARY_SLICE, following the list, tuple, and unicode fast paths added in gh-144590. This avoids creating a temporary slice object. Bytes subclasses continue to use the generic path.

Teach the Tier 2 optimizer that slicing an exact bytes object returns an exact bytes object.

Benchmark

Release builds on macOS arm64. Each result is the median of nine alternating runs after two warmups. Each sample ran in a fresh process.

Benchmark main this PR Change
Partial bytes slice, 5 million operations 347.717 ms 310.204 ms 10.8% faster
Full bytes slice, 5 million operations 274.867 ms 246.176 ms 10.4% faster
base64.b64encode with altchars, 3 million operations 562.155 ms 533.675 ms 5.1% faster

There was no measurable change in peak RSS.

Tests

The full debug test suite passed with 50,671 tests using 14 workers.

Focused tests passed on debug, free-threaded, and JIT builds. test_bytes and test_slice also passed a -R 3:3 refleak run.

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.

1 participant