compile.c: use rb_enc_interned_str to reduce allocations

The `rb_fstring(rb_enc_str_new())` pattern is inneficient because:

- It passes a mutable string to `rb_fstring` so if it has to be interned
  it will first be duped.
- It an equivalent interned string already exists, we allocated the string
  for nothing.

With `rb_enc_interned_str` we either directly get the pre-existing string
with 0 allocations, or efficiently directly intern the one we create
without first duping it.
This commit is contained in:
Jean Boussier 2024-04-10 10:50:18 +02:00 committed by Jean Boussier
parent ed303cd56c
commit 1b830740ba
5 changed files with 24 additions and 21 deletions

View file

@ -741,6 +741,14 @@ rb_parser_set_yydebug(VALUE vparser, VALUE flag)
VALUE
rb_str_new_parser_string(rb_parser_string_t *str)
{
VALUE string = rb_enc_interned_str(str->ptr, str->len, str->enc);
rb_enc_str_coderange(string);
return string;
}
VALUE
rb_str_new_mutable_parser_string(rb_parser_string_t *str)
{
return rb_enc_str_new(str->ptr, str->len, str->enc);
}