Skip to content

Reuse a per-thread UriParser scratch to avoid per-parse allocation#2205

Merged
hyperxpro merged 1 commit into
mainfrom
uriparser-threadlocal-scratch
Jun 20, 2026
Merged

Reuse a per-thread UriParser scratch to avoid per-parse allocation#2205
hyperxpro merged 1 commit into
mainfrom
uriparser-threadlocal-scratch

Conversation

@hyperxpro

Copy link
Copy Markdown
Member

Motivation:

UriParser.parse allocated a fresh parser on every call and returned it - the escape via the return value defeats scalar replacement, so it couldn't be optimized away, costing an allocation on every URL parse (i.e. every request).

Modification:

Reuse a per-thread UriParser scratch instance (held in a FastThreadLocal) instead of allocating per parse; the sole caller copies the parsed fields out synchronously before the scratch can be reused.

Result:

No per-parse UriParser allocation on the request path; parsed Uri output is unchanged, with a reentrancy test guarding the reuse.

@hyperxpro hyperxpro merged commit 5e405f0 into main Jun 20, 2026
17 checks passed
@hyperxpro hyperxpro deleted the uriparser-threadlocal-scratch branch June 20, 2026 21:35
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.

1 participant