-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
[WIP] gh-129813, PEP 782: Add PyBytesWriter C API #131681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
459f3d8
to
9097e5f
Compare
Add functions: * PyBytesWriter_Create() * PyBytesWriter_Discard() * PyBytesWriter_Finish() * PyBytesWriter_FinishWithSize() * PyBytesWriter_FinishWithEndPointer() * PyBytesWriter_Data() * PyBytesWriter_Allocated() * PyBytesWriter_SetSize() * PyBytesWriter_Resize()
9097e5f
to
e24d40e
Compare
Convert _PyBytes_FromHex().
Replace PyBytes_FromStringAndSize(NULL, 0) with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).
This change has no impact on performance, even if the new public API allocates memory on the heap, instead of allocating on the stack. It uses a freelist to optimize Microbenchmark on 3 functions, to compare the private
import pyperf
import binascii
runner = pyperf.Runner()
runner.bench_func('from list 100', bytes, list(b'x' * 100))
runner.bench_func('from list 1,000', bytes, list(b'x' * 1_000))
runner.bench_func('from hex 100', bytes.fromhex, bytes(range(100)).hex())
runner.bench_func('from hex 1,000', bytes.fromhex, (b'x' * 1_000).hex())
runner.bench_func('b2a_uu', binascii.b2a_uu, b'x' * 45) Result:
Benchmark hidden because not significant (1): from list 1,000 |
Benchmark comparing Benchmark: import pyperf
SIZES = (10, 100, 500)
runner = pyperf.Runner()
for size in SIZES:
large_int = (2 ** (size * 8) - 1)
runner.bench_func(f'to_bytes({size})', large_int.to_bytes, size)
for size in SIZES:
mem = memoryview(b'x' * size)
runner.bench_func(f'memoryview({size}).tobytes()', mem.tobytes) Result:
It's hard to beat There is an overhead around 10 ns when using |
Add functions: