Skip to content

perf: reduce async request overhead - #1183

Open
WH-2099 wants to merge 3 commits into
pydantic:mainfrom
WH-2099:perf/reduce-async-request-overhead
Open

perf: reduce async request overhead#1183
WH-2099 wants to merge 3 commits into
pydantic:mainfrom
WH-2099:perf/reduce-async-request-overhead

Conversation

@WH-2099

@WH-2099 WH-2099 commented Sep 1, 2026

Copy link
Copy Markdown

Closes #1182.

Discussion: #1181.

Summary

This drops three bits of avoidable work from common request paths:

  • Skip the cancellation shield when the connection pool has nothing to close.
  • Reuse client headers when a request does not add any.
  • Pass unencoded async response chunks through without no-op chunking work.

The changes are split into three commits so each one can be reviewed or reverted on its own.

Performance

Scenario Requests/s Client CPU/request
c1 / 1 KiB 4,710 -> 4,994 (+6.0%) 210.0 -> 198.1 us (-5.7%)
c32 / 1 KiB 4,568 -> 4,813 (+5.4%) 217.2 -> 206.2 us (-5.1%)
c32 / 64 KiB 3,941 -> 4,114 (+4.4%) 251.7 -> 241.1 us (-4.2%)

These are medians from five interleaved 2.5-second runs on CPython 3.14.7 with stdlib asyncio and pinned server/client CPUs.

The benchmark used plain HTTP/1.1 on loopback, so the numbers should not be read as TLS, HTTP/2, or WAN results.

Validation

  • scripts/test
  • 1995 passed, 1 skipped
  • 100% coverage
  • Ruff, mypy, lockfile, and unasync checks pass

Checklist

  • I understand that this PR may be closed in case there was no previous discussion.
  • I've added regression tests for the behavior-sensitive changes and kept the commits atomic.
  • No documentation update is needed for these internal changes.

Review in cubic

@codspeed-hq

codspeed-hq Bot commented Sep 1, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing WH-2099:perf/reduce-async-request-overhead (42a9350) with main (bd0b9bc)

Open in CodSpeed

Footnotes

  1. 7 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.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 6 files

Re-trigger cubic

@WH-2099

WH-2099 commented Sep 7, 2026

Copy link
Copy Markdown
Author

@Kludex Could you take a look at this? (⌐■_■)

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.

Trim avoidable work from async requests

1 participant