Repository navigation
Commit 61fdee3
BytesIO is commonly constructed in the test suite and gets significantly
faster to construct. Argument error messages are unchanged.
pyperf microbenchmark (PGO+LTO, run serially):
| Benchmark | base | bytesio |
|------------------------------|:-------:|:---------------------:|
| BytesIO(bytes) | 54.2 ns | 27.0 ns: 2.01x faster |
| BytesIO(bytearray) | 76.7 ns | 38.6 ns: 1.99x faster |
| BytesIO(initial_bytes=bytes) | 56.5 ns | 34.8 ns: 1.63x faster |
| BytesIO() | 40.9 ns | 25.4 ns: 1.61x faster |
| Geometric mean | (ref) | 1.48x faster |
Benchmark hidden because not significant (2): `BytesIO.__new__`,
subclass(bytes)
Benchmark code:
```python
"""BytesIO() construction: AC @vectorcall vs tp_new + tp_init."""
import pyperf
runner = pyperf.Runner()
setup = "from io import BytesIO; b = b'abcdefgh'"
runner.timeit('BytesIO()', 'BytesIO()', setup)
runner.timeit('BytesIO(bytes)', 'BytesIO(b)', setup)
runner.timeit('BytesIO(bytearray)', 'BytesIO(x)', setup + '; x = bytearray(b)')
runner.timeit('BytesIO(initial_bytes=bytes)', 'BytesIO(initial_bytes=b)', setup)
runner.timeit('subclass(bytes)', 'S(b)', setup + '\nclass S(BytesIO): pass')
runner.timeit('BytesIO.__new__', 'new(BytesIO)',
setup + '; new = BytesIO.__new__')
```
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent eba2a21 commit 61fdee3
2 files changed
Lines changed: 70 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1118 | 1118 | | |
1119 | 1119 | | |
1120 | 1120 | | |
| 1121 | + | |
1121 | 1122 | | |
1122 | 1123 | | |
1123 | 1124 | | |
| |||
1127 | 1128 | | |
1128 | 1129 | | |
1129 | 1130 | | |
1130 | | - | |
| 1131 | + | |
1131 | 1132 | | |
1132 | 1133 | | |
1133 | 1134 | | |
| |||
1256 | 1257 | | |
1257 | 1258 | | |
1258 | 1259 | | |
| 1260 | + | |
1259 | 1261 | | |
1260 | 1262 | | |
1261 | 1263 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments