refactor: hoist LoaderFunc to package-level var in phpheaders#2053
refactor: hoist LoaderFunc to package-level var in phpheaders#2053
Conversation
43d99e1 to
e8c7b37
Compare
|
I implemented @maxm86545 suggestion, this is now ready to be merged: BenchmarksApple M1 Pro, 6 runs, Latency (sec/op)
Memory (B/op)
Allocations (allocs/op)
Summary
The optimization works by:
The +3 allocs in |
|
Based on just the benchmarks with zero further insight on the topic, I think it's worth it. Runtime performance is almost always worth it over a slightly lower memory footprint and as a php server, it's not like FrankenPHP is expected to consume little memory. |
There was a problem hiding this comment.
Pull request overview
This PR aims to improve request handling performance by batching uncommon HTTP header name conversions/caching, and updates tests accordingly.
Changes:
- Replace per-header uncommon header conversion with a batched
GetUnCommonHeadershelper backed by the existing Otter cache. - Update CGI header-to-
$_SERVERregistration to precompute uncommon header names in bulk. - Adjust header correctness tests to use the new batched helper (and add a duplicate copy in
phpmainthread_test.go).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
internal/phpheaders/phpheaders.go |
Replaces single-key conversion with a batched API that returns a map of converted header keys. |
cgi.go |
Refactors header registration to count/register common headers first, then batch-convert/register uncommon headers. |
internal/phpheaders/phpheaders_test.go |
Updates the common-header correctness test to use the new batched API. |
phpmainthread_test.go |
Adds a second copy of the common-header correctness test in the main package tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Re-running the benchmarks properly, it's not worth it. I just made a tiny cleanup. |
Summary
Hoists the
otter.LoaderFuncclosure inGetUnCommonHeaderto a package-levelloadervar, so it is allocated once at init time instead of being re-created on every call.This is a minor cleanup — the previous code created a new
LoaderFuncclosure each timeGetUnCommonHeaderwas called. While otter's cache-hit path is fast enough that this doesn't show a measurable difference in end-to-end benchmarks, avoiding the repeated allocation is strictly better.What changed
Before (closure created per call):
After (closure allocated once):
Benchmarks
Apple M1 Pro, 8 runs,
benchstatcomparison — no regressions, no extra allocations:All results within noise. Zero change in allocations.