Avoid generating an unused Date header fallback by marcus-campbell · Pull Request #13299 · aio-libs/aiohttp · GitHub
Skip to content

Avoid generating an unused Date header fallback - #13299

Merged
bdraco merged 6 commits into
aio-libs:masterfrom
marcus-campbell:perf-avoid-unused-date-fallback
Aug 24, 2026
Merged

bdraco merged 6 commits into
aio-libs:masterfrom
marcus-campbell:perf-avoid-unused-date-fallback

Conversation

@marcus-campbell

@marcus-campbell marcus-campbell commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

What do these changes do?

headers.setdefault(hdrs.DATE, rfc822_formatted_time()) evaluates the formatter even when the response already has a Date header, so the clock read (and possible cache refresh) happens and the result is discarded. This switches to a presence check so the formatter only runs when its value is used, and adds a regression test plus a small _prepare_headers() benchmark with and without an explicit Date.

Locally, a paired A/B run showed ~0.45 µs (~2.4%) per response on the explicit-Date path and no measurable change on the generated-Date path.

Follow-up to #9898: that benchmark only constructed Response objects and never reached _prepare_headers(), which is why CodSpeed showed no change.

Are there changes in behavior for the user?

No. A user-supplied Date is still retained.

Is it a substantial burden for the maintainers to support this?

No.

Related issue number

N/A (see #9898 above)

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes (no user-facing documentation change is
    required for this internal performance cleanup)
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder

AI assistance

Drafted with help from GPT-5.6 Sol (benchmarking, parts of this description); code by me. I've reviewed all of the code and remain responsible for the submission.

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Aug 1, 2026
@codecov

codecov Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

@codspeed

codspeed Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 1%

⚠️ 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
❌ 1 (👁 1) regressed benchmark
✅ 90 untouched benchmarks
🆕 2 new benchmarks
⏩ 83 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_list_element_pattern_redos_payload[whitespace_run_after_content] 33.9 ms 30.9 ms +9.91%
🆕 test_prepare_web_response_headers[explicit-date] N/A 7.6 ms N/A
🆕 test_prepare_web_response_headers[generated-date] N/A 8.1 ms N/A
👁 test_read_base64_part 14.3 ms 16 ms -10.83%

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 marcus-campbell:perf-avoid-unused-date-fallback (0993f17) with master (394959f)

Open in CodSpeed

Footnotes

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

@marcus-campbell
marcus-campbell marked this pull request as ready for review August 1, 2026 04:08
@marcus-campbell

Copy link
Copy Markdown
Contributor Author

A note on the CodSpeed report above: "will not alter performance" is expected here, because the two benchmarks that exercise this change (test_prepare_web_response_headers[explicit-date] and [generated-date]) are new in this PR. There's no BASE measurement to compare against, so they show N/A rather than an improvement. The 84 untouched benchmarks don't cover _prepare_headers(), which is the gap that also affected #9898's experiment.

The evidence for the change is the local paired A/B run described in the PR body.

@bdraco bdraco added backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot labels Aug 24, 2026
Comment thread tests/test_web_response.py Outdated
@bdraco
bdraco enabled auto-merge (squash) August 24, 2026 01:19
@greptile-apps

greptile-apps Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Reviews (3): Last reviewed commit: "Merge branch 'master' into perf-avoid-un..." | Re-trigger Greptile

Comment thread tests/test_web_response.py Outdated
@bdraco
bdraco merged commit 5fa8584 into aio-libs:master Aug 24, 2026
52 checks passed
@patchback

patchback Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Backport to 3.15: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.15/5fa85842951ad68b2cba0bbc511dad1eca90cd3a/pr-13299

Backported as #13543

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@patchback

patchback Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Dreamsorcerer pushed a commit that referenced this pull request Aug 25, 2026
…eader fallback (#13543)

**This is a backport of PR #13299 as merged into master
(5fa8584).**

---------

Co-authored-by: Marcus Campbell <21266489+marcus-campbell@users.noreply.github.com>
Dreamsorcerer pushed a commit that referenced this pull request Aug 25, 2026
…eader fallback (#13544)

**This is a backport of PR #13299 as merged into master
(5fa8584).**

---------

Co-authored-by: Marcus Campbell <21266489+marcus-campbell@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants