ruby-changes:62651
From: Jean <ko1@a...>
Date: Thu, 20 Aug 2020 00:09:15 +0900 (JST)
Subject: [ruby-changes:62651] aaf0e33c0a (master): register_fstring: avoid duping the passed string when possible
https://git.ruby-lang.org/ruby.git/commit/?id=aaf0e33c0a From aaf0e33c0a45e13d27e1009c7298fcb0a57c0d4f Mon Sep 17 00:00:00 2001 From: Jean Boussier <jean.boussier@g...> Date: Wed, 19 Aug 2020 11:29:54 +0200 Subject: register_fstring: avoid duping the passed string when possible If the passed string is frozen, bare and not shared, then there is no need to duplicate it. Ref: 4ab69ebbd7cef8539f687e1f948845d076461dc6 Ref: https://bugs.ruby-lang.org/issues/11386 diff --git a/string.c b/string.c index 1a1b9fd..df7e814 100644 --- a/string.c +++ b/string.c @@ -305,7 +305,8 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t arg, int existi https://github.com/ruby/ruby/blob/trunk/string.c#L305 OBJ_FREEZE_RAW(str); } else { - str = str_new_frozen(rb_cString, str); + if (!OBJ_FROZEN(str)) + str = str_new_frozen(rb_cString, str); if (STR_SHARED_P(str)) { /* str should not be shared */ /* shared substring */ str_make_independent(str); diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 489eee3..529b1be 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -3187,6 +3187,22 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L3187 assert_same(str, -bar, "uminus deduplicates [Feature #13077]") end + def test_uminus_frozen + # embedded + str1 = ("foobar" * 3).freeze + str2 = ("foobar" * 3).freeze + assert_not_same str1, str2 + assert_same str1, -str1 + assert_same str1, -str2 + + # regular + str1 = ("foobar" * 4).freeze + str2 = ("foobar" * 4).freeze + assert_not_same str1, str2 + assert_same str1, -str1 + assert_same str1, -str2 + end + def test_uminus_no_freeze_not_bare str = @cls.new("foo") assert_instance_of(@cls, -str) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/