From d90103513619b38ca5ddcd15bedafae5955aa5b2 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Fri, 29 May 2026 10:18:53 -0700 Subject: [PATCH] Atomic fast path for "locale" encoding registration The "locale" encoding is only registered once, so we can use an atomic to avoid the full lock and hash lookup. --- encoding.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/encoding.c b/encoding.c index 73fad8f1a6..c17f118eef 100644 --- a/encoding.c +++ b/encoding.c @@ -98,6 +98,7 @@ static rb_encoding *global_enc_ascii, *global_enc_us_ascii; static int filesystem_encindex = ENCINDEX_ASCII_8BIT; +static rb_atomic_t locale_alias_registered; #define GLOBAL_ENC_TABLE_LOCKING(tbl) \ for (struct enc_table *tbl = &global_enc_table, **locking = &tbl; \ @@ -1568,15 +1569,16 @@ rb_locale_encindex(void) if (idx < 0) idx = ENCINDEX_UTF_8; - GLOBAL_ENC_TABLE_LOCKING(enc_table) { - if (enc_registered(enc_table, "locale") < 0) { + if (!RUBY_ATOMIC_LOAD(locale_alias_registered)) { + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + if (enc_registered(enc_table, "locale") < 0) { # if defined _WIN32 - void Init_w32_codepage(void); - Init_w32_codepage(); + void Init_w32_codepage(void); + Init_w32_codepage(); # endif - GLOBAL_ENC_TABLE_LOCKING(enc_table) { enc_alias_internal(enc_table, "locale", idx); } + RUBY_ATOMIC_SET(locale_alias_registered, 1); } }