Skip to content

Commit 03c26ec

Browse files
authored
refactor: hoist LoaderFunc to package-level var in phpheaders (#2053)
## Summary Hoists the `otter.LoaderFunc` closure in `GetUnCommonHeader` to a package-level `loader` var, 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 `LoaderFunc` closure each time `GetUnCommonHeader` was 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): ```go func GetUnCommonHeader(ctx context.Context, key string) string { phpHeaderKey, err := headerKeyCache.Get( ctx, key, otter.LoaderFunc[string, string](func(_ context.Context, key string) (string, error) { return "HTTP_" + headerNameReplacer.Replace(strings.ToUpper(key)) + "\x00", nil }), ) ... } ``` **After** (closure allocated once): ```go var loader = otter.LoaderFunc[string, string](func(_ context.Context, key string) (string, error) { return "HTTP_" + headerNameReplacer.Replace(strings.ToUpper(key)) + "\x00", nil }) func GetUnCommonHeader(ctx context.Context, key string) string { phpHeaderKey, err := headerKeyCache.Get(ctx, key, loader) ... } ``` ## Benchmarks Apple M1 Pro, 8 runs, `benchstat` comparison — no regressions, no extra allocations: | Benchmark | main | PR | vs base | |---|---|---|---| | HelloWorld | 41.81µ ± 2% | 42.75µ ± 5% | ~ (p=0.065) | | ServerSuperGlobal | 73.36µ ± 2% | 74.20µ ± 3% | ~ (p=0.105) | | UncommonHeaders | 69.03µ ± 3% | 68.71µ ± 1% | ~ (p=0.382) | All results within noise. Zero change in allocations.
1 parent 2b27601 commit 03c26ec

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

internal/phpheaders/phpheaders.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,16 @@ var CommonRequestHeaders = map[string]string{
119119

120120
// Cache up to 256 uncommon headers
121121
// This is ~2.5x faster than converting the header each time
122-
var headerKeyCache = otter.Must[string, string](&otter.Options[string, string]{MaximumSize: 256})
123-
124-
var headerNameReplacer = strings.NewReplacer(" ", "_", "-", "_")
122+
var (
123+
headerKeyCache = otter.Must[string, string](&otter.Options[string, string]{MaximumSize: 256})
124+
headerNameReplacer = strings.NewReplacer(" ", "_", "-", "_")
125+
loader = otter.LoaderFunc[string, string](func(_ context.Context, key string) (string, error) {
126+
return "HTTP_" + headerNameReplacer.Replace(strings.ToUpper(key)) + "\x00", nil
127+
})
128+
)
125129

126130
func GetUnCommonHeader(ctx context.Context, key string) string {
127-
phpHeaderKey, err := headerKeyCache.Get(
128-
ctx,
129-
key,
130-
otter.LoaderFunc[string, string](func(_ context.Context, key string) (string, error) {
131-
return "HTTP_" + headerNameReplacer.Replace(strings.ToUpper(key)) + "\x00", nil
132-
}),
133-
)
131+
phpHeaderKey, err := headerKeyCache.Get(ctx, key, loader)
134132
if err != nil {
135133
panic(err)
136134
}

0 commit comments

Comments
 (0)