Skip to content

Skip header parsing when set_locale gets a single locale - #147

Merged
grosser merged 1 commit into
grosser:masterfrom
sampokuokkanen:plain-locale-fast-path
Oct 18, 2026
Merged

grosser merged 1 commit into
grosser:masterfrom
sampokuokkanen:plain-locale-fast-path

Conversation

@sampokuokkanen

Copy link
Copy Markdown
Contributor

set_locale parses its argument as an Accept-Language header (gsub, split, a regexp sub per entry, sort) even when it is a single locale like :ja or "zh_TW", which is what it gets almost every time: with gettext_i18n_rails every I18n.locale= and I18n.with_locale (twice: set and restore) calls it, often many times per page (links to each language, hreflang tags).

A single locale already in the de / de_DE form parses to itself, so use it directly. Per locale= call: 24 -> 5 allocations, 2.85 -> 1.44 µs.

set_locale parses its argument as an Accept-Language header (gsub,
split, a regexp sub per entry, sort) even when it is a single locale
like :ja or "zh_TW", which is what it gets almost every time: with
gettext_i18n_rails every I18n.locale= and I18n.with_locale (twice: set
and restore) calls it, often many times per page (links to each
language, hreflang tags).

A single locale already in the de / de_DE form parses to itself, so use
it directly. Per locale= call: 24 -> 5 allocations, 2.85 -> 1.44 µs.

@grosser grosser left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice trick

@grosser
grosser merged commit 854e264 into grosser:master Oct 18, 2026
6 checks passed
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.

2 participants