From 37a16c7812f5b7e6faa762b927e9f04065cc495a Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 13 Nov 2024 13:17:49 +0100 Subject: [PATCH] string.c: preserve coderange when interning a string Since `str_do_hash` will most likely scan the string to compute the coderange, we might as well copy it over in the interned string in case it's useful later. --- string.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/string.c b/string.c index 002d55d6a0..f298d74cfe 100644 --- a/string.c +++ b/string.c @@ -435,6 +435,9 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t data, int exist return ST_STOP; } else { + // Unless the string is empty or binary, its coderange has been precomputed. + int coderange = ENC_CODERANGE(str); + if (FL_TEST_RAW(str, STR_FAKESTR)) { if (arg->copy) { VALUE new_str; @@ -481,6 +484,8 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t data, int exist str = str_new_frozen(rb_cString, str); } } + + ENC_CODERANGE_SET(str, coderange); RBASIC(str)->flags |= RSTRING_FSTR; *key = *value = arg->fstr = str;