Skip to content

Pre-size FormData serialization buffer#6855

Open
Brooooooklyn wants to merge 1 commit into
cloudflare:mainfrom
Brooooooklyn:optimize-formdata-serialization
Open

Pre-size FormData serialization buffer#6855
Brooooooklyn wants to merge 1 commit into
cloudflare:mainfrom
Brooooooklyn:optimize-formdata-serialization

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

Summary

  • calculate the exact multipart FormData serialization size with overflow checks
  • pre-size the output vector to avoid repeated reallocations and the final release copy
  • add serializer coverage for escaped names and explicit/default file content types
  • add a focused FormData serialization benchmark

Benchmark

Warm median results from 5 repetitions:

Payload Before After Improvement
1 KiB 0.224 us 0.0865 us 2.59x
1 MiB 0.0387 ms 0.0127 ms 3.06x
16 MiB 0.704 ms 0.214 ms 3.28x

The 16 MiB case improves from 22.2 GiB/s to 72.9 GiB/s.

Tests

  • bazel test //src/workerd/api/tests:form-data-test@
  • python3 tools/cross/format.py --check git

@Brooooooklyn Brooooooklyn requested review from a team as code owners July 3, 2026 15:35
@codspeed-hq

codspeed-hq Bot commented Jul 3, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 9.89%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 71 untouched benchmarks
🆕 3 new benchmarks
⏩ 129 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
bodyWithHeaders[Response] 34.3 µs 31.2 µs +9.89%
🆕 singleFile[FormDataSerialization][1024] N/A 5.9 µs N/A
🆕 singleFile[FormDataSerialization][1048576] N/A 1 ms N/A
🆕 singleFile[FormDataSerialization][16777216] N/A 15.9 ms N/A

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Brooooooklyn:optimize-formdata-serialization (c1d2a0b) with main (4654087)

Open in CodSpeed

Footnotes

  1. 129 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant