Skip to content

Remove per-write closures from MultipartParser._internal_write #368

Closed as not planned

Description

@g4lb

Opening a PR is not possible from my account, so the change is on a branch: main...g4lb:python-multipart:speed-up-multipart-write

_internal_write made four closures on each write() call. The change removes them. The callbacks, their order and their arguments do not change.

Input (tests/test_benchmarks.py, CPython 3.14) Before After Change
test_parse_multipart_simple_form 7.98 µs 6.58 µs -17.5%
test_parse_multipart_large_form 296.9 µs 260.5 µs -12.3%
test_parse_multipart_file_upload 456.4 µs 408.4 µs -10.5%
test_parse_multipart_worstcase_boundary_chars 48.6 µs 32.8 µs -32.6%

Ruff, mypy and pytest pass with 100% coverage. Old and new parsers gave the same callback events on 1,000,000 random inputs.

Activity

  1. Kludex commented on Nov 16, 2026

    @Kludex
    Owner

    Thanks for the proposal. We merged the small mark-helper cleanup in #370. CodSpeed did not confirm improvements for the callback extraction or header-size helper evaluated separately, and we are not pursuing the remaining changes. This does not establish that the full proposed patch has no benefit.

  2. g4lb commented on Nov 16, 2026

    @g4lb
    Author

    @Kludex Thank you for testing each part. I am glad that the mark change from this issue is now in #370.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions