perf: cache special-scheme flag and use a map for scheme lookups#7
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes URLPattern initialization by reducing repeated special-scheme checks and improving scheme membership lookups, targeting lower allocation/time costs in New().
Changes:
- Convert the special-scheme collection from a slice to a set-like
map[string]struct{}for O(1) membership checks. - Cache
protocolComponentMatchesSpecialScheme()once perURLPatternInit.New()and reuse it for hostname/pathname handling. - Simplify port defaulting by using a direct
DefaultPortslookup.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
bad2ba2 to
a01a5f7
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Call protocolComponentMatchesSpecialScheme() once per New() and reuse
the result for both the hostname and pathname branches instead of
matching the component regex against the five special schemes twice.
- Replace specialSchemeList with a single map[string]struct{} (renamed
to specialSchemeSet since the value is now a set, not an ordered list)
so the per-component lookups in processHostnameForInit and
processPathnameForInit are O(1). protocolComponentMatchesSpecialScheme
just iterates the map keys, since order is irrelevant when the loop
returns on the first hit.
- Collapse the port-defaulting loop into a direct DefaultPorts lookup
gated on specialSchemeSet membership, so user-added DefaultPorts
entries cannot silently trigger special-scheme behaviour. The
processedInit protocol is lowercased before the check because in
"pattern" mode processProtocolForInit does not canonicalize, and the
protocol component is later compiled with canonicalizeProtocol which
lowercases — so a mixed-case literal like "HTTP" should behave as
"http" for port defaulting.
~5-10%% reduction in ns/op on New() across pattern benchmarks.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
a01a5f7 to
1f06067
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 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.
Summary
protocolComponentMatchesSpecialScheme()once perNew()and reuse the result for the hostname and pathname branches instead of matching the component regex against the five special schemes twice.[]stringspecialSchemeListwith amap[string]struct{}soprocessHostnameForInit/processPathnameForInituse O(1) lookups instead of linear scans.protocolComponentMatchesSpecialSchemeranges over the map keys — order is irrelevant since it returns on the first hit.DefaultPortslookup, which already keys the same five schemes.~5–10% reduction in ns/op on
BenchmarkNewacross pattern shapes.