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 — [WIP] gh-129813: Add PyBytesWriter C API by vstinner · Pull Request #129814 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Feb 7, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member
  • Replace usage of the old private _PyBytesWriter with the new public PyBytesWriter C API.
  • Remove the old private _PyBytesWriter C API.
  • Add a freelist for PyBytesWriter_Create().
  • TODO: write doc
  • TODO: document new functions in What's New and Changelog
  • Issue: [C API] PEP 782: Add PyBytesWriter API #129813

Sorry, something went wrong.

Uh oh!

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

All reactions
@vstinner
pythongh-129813: Add PyBytesWriter C API
490222f
* Replace usage of the old private _PyBytesWriter with the new public
  PyBytesWriter C API.
* Remove the old private _PyBytesWriter C API.
* Add a freelist for PyBytesWriter_Create().
* TODO: write doc
* TODO: document new functions in What's New and Changelog
@vstinner vstinner added the DO-NOT-MERGE label Feb 7, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Feb 7, 2025
[C API] PEP 782: Add PyBytesWriter API #129813
Closed
@vstinner

vstinner commented Feb 7, 2025

Copy link
Copy Markdown
Member Author

The PR is big because it also replaces usage of the old private API with new public API. If the API is approved, I will split the PR into smaller pieces and measure the performance impact of these changes.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Feb 7, 2025

Copy link
Copy Markdown
Member Author

Some functions should be optimized after the removal of the private min_size member:

  • unicode_encode_ucs1()
  • utf8_encoder()
  • PyBytes_FromFormatV()

These functions allocate a little bit too much memory, extend argument of PyBytesWriter_Extend() should be adjusted.

I just tried to make the code work, not to optimize it.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Feb 7, 2025

Copy link
Copy Markdown
Member Author

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 PyBytesWriter_Create().

Example of microbenchmark on 3 functions:

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 ref public
from list 100 672 ns 647 ns: 1.04x faster
from list 1,000 6.22 us 6.12 us: 1.02x faster
from hex 100 143 ns 145 ns: 1.02x slower
from hex 1,000 1.02 us 1.03 us: 1.00x slower
Geometric mean (ref) 1.01x faster

Benchmark hidden because not significant (1): b2a_uu

All reactions

Sorry, something went wrong.

Uh oh!

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

@cmaloney

cmaloney commented Feb 11, 2025

Copy link
Copy Markdown
Contributor

pseudo-tangential idea: Could this instead just be a C wrapper for io.BytesIO? Working to try to get to one fast implementation to read/write a bytes object.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Feb 13, 2025

Copy link
Copy Markdown
Member Author

pseudo-tangential idea: Could this instead just be a C wrapper for io.BytesIO?

io.BytesIO API is basically the write(bytes) method, whereas proposed PyBytesWriter API gives directly a pointer into a buffer. It's a different API. I don't think that io.BytesIO API can or should be modified to give directly a pointer. io.BytesIO is more complex since it allows changing the position, the seek() method, and also reading, the read() method.

cmaloney reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

vstinner added 7 commits February 13, 2025 20:03
@vstinner
Add documentation
8f8aeb8
@vstinner
Add PyBytesWriter_GetRemaining()
68d2fe7
@vstinner
Optimize bytes_fromformat()
e582385
@vstinner
Remove PyBytesWriter_GetAllocated()
7cb444b
@vstinner
Add 'center' example
486d7cd
@vstinner
Change PyBytesWriter_WriteBytes() argument type to void*
c4f4c07
@vstinner
Documentation
18ec24d
@vstinner

vstinner commented Mar 12, 2025

Copy link
Copy Markdown
Member Author

I created a discussion: https://discuss.python.org/t/add-pybyteswriter-public-c-api/81182

It seems like most developers are confused by the API which requires to pass writer and buf to most functions. I abandon this API.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner closed this Mar 12, 2025
@vstinner
vstinner deleted the bytes_writer branch March 12, 2025 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently will be requested when the pull request is marked ready for review ericsnowcurrently is a code owner
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity will be requested when the pull request is marked ready for review ZeroIntensity is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

DO-NOT-MERGE

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.

2 participants

@vstinner @cmaloney
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.