summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2026-01-19 07:32:09 +0100
committerJean Boussier <jean.boussier@gmail.com>2026-01-20 08:33:42 +0100
commit27bb1623cd048f3cbfc527cc315894803deabba2 (patch)
treea7bcabe824382632378a07ad099ee1fcae1aa0cc /internal
parent826dbcfb2be33ffbfbc99554244881e014513d20 (diff)
file.c: Optimize `rb_file_dirname_n` fixed costs
- `str_null_check` was performed twice, once by `FilePathStringValue` and a second time by `StringValueCStr`. - `StringValueCStr` was checking for the terminator presence, but we don't care about that. - `FilePathStringValue` calls `rb_str_new_frozen` to ensure `fname` isn't mutated, but that's costly for such a check. Instead we can do it in debug mode only. - `rb_enc_get` is slow because it accepts arbitrary objects, even immediates, so it has to do numerous type checks. Add a much faster `rb_str_enc_get` when we know we're dealing with a string. - `rb_enc_copy` is slow for the same reasons, since we already have the encoding, we can use `rb_enc_str_new` instead.
Diffstat (limited to 'internal')
-rw-r--r--internal/string.h7
1 files changed, 7 insertions, 0 deletions
diff --git a/internal/string.h b/internal/string.h
index cd1e8d7929..dd5e20c0c6 100644
--- a/internal/string.h
+++ b/internal/string.h
@@ -50,6 +50,13 @@ rb_str_enc_fastpath(VALUE str)
return rb_str_encindex_fastpath(ENCODING_GET_INLINED(str));
}
+static inline rb_encoding *
+rb_str_enc_get(VALUE str)
+{
+ RUBY_ASSERT(RB_TYPE_P(str, T_STRING));
+ return rb_enc_from_index(ENCODING_GET(str));
+}
+
/* string.c */
VALUE rb_str_dup_m(VALUE str);
VALUE rb_fstring(VALUE);