ruby-changes:57981
From: Jean <ko1@a...>
Date: Fri, 27 Sep 2019 13:52:55 +0900 (JST)
Subject: [ruby-changes:57981] eff15a269f (master): [EXPERIMENTAL] Make NilClass#to_s, TrueClass#to_s and FalseClass#to_s return a frozen String
https://git.ruby-lang.org/ruby.git/commit/?id=eff15a269f From eff15a269fdc37d2b09cf1dfe8c1b1bf6e377a32 Mon Sep 17 00:00:00 2001 From: Jean Boussier <jean.boussier@g...> Date: Thu, 26 Sep 2019 17:27:47 +0200 Subject: [EXPERIMENTAL] Make NilClass#to_s, TrueClass#to_s and FalseClass#to_s return a frozen String * Always the same frozen String for each of these values. * Avoids extra allocations whenever calling these 3 methods. * See [Feature #16150] diff --git a/NEWS b/NEWS index 1ef70fc..2f8b954 100644 --- a/NEWS +++ b/NEWS @@ -163,6 +163,14 @@ Integer:: https://github.com/ruby/ruby/blob/trunk/NEWS#L163 0b01001100[2...6] #=> 0b0011 ^^^^ +NilClass / TrueClass / FalseClass:: + + Modified method:: + + * NilClass#to_s, TrueClass#to_s and FalseClass#to_s now always returns a + frozen String. The returned String is always the same for each of these + values. This change is experimental. [Feature #16150] + Module:: New method:: diff --git a/object.c b/object.c index 9c603a8..ab9ff80 100644 --- a/object.c +++ b/object.c @@ -40,6 +40,10 @@ VALUE rb_cNilClass; /*!< NilClass class */ https://github.com/ruby/ruby/blob/trunk/object.c#L40 VALUE rb_cTrueClass; /*!< TrueClass class */ VALUE rb_cFalseClass; /*!< FalseClass class */ +static VALUE rb_cNilClass_to_s; +static VALUE rb_cTrueClass_to_s; +static VALUE rb_cFalseClass_to_s; + /*! \cond INTERNAL_MACRO */ #define id_eq idEq @@ -1442,7 +1446,7 @@ nil_to_f(VALUE obj) https://github.com/ruby/ruby/blob/trunk/object.c#L1446 static VALUE nil_to_s(VALUE obj) { - return rb_usascii_str_new(0, 0); + return rb_cNilClass_to_s; } /* @@ -1525,7 +1529,7 @@ nil_match(VALUE obj1, VALUE obj2) https://github.com/ruby/ruby/blob/trunk/object.c#L1529 static VALUE true_to_s(VALUE obj) { - return rb_usascii_str_new2("true"); + return rb_cTrueClass_to_s; } @@ -1602,7 +1606,7 @@ true_xor(VALUE obj, VALUE obj2) https://github.com/ruby/ruby/blob/trunk/object.c#L1606 static VALUE false_to_s(VALUE obj) { - return rb_usascii_str_new2("false"); + return rb_cFalseClass_to_s; } /* @@ -4609,6 +4613,8 @@ InitVM_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L4613 rb_define_global_function("Hash", rb_f_hash, 1); rb_cNilClass = rb_define_class("NilClass", rb_cObject); + rb_cNilClass_to_s = rb_fstring_enc_lit("", rb_usascii_encoding()); + rb_gc_register_mark_object(rb_cNilClass_to_s); rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0); rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0); rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0); @@ -4703,6 +4709,8 @@ InitVM_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L4709 rb_deprecate_constant(rb_cObject, "Data"); rb_cTrueClass = rb_define_class("TrueClass", rb_cObject); + rb_cTrueClass_to_s = rb_fstring_enc_lit("true", rb_usascii_encoding()); + rb_gc_register_mark_object(rb_cTrueClass_to_s); rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0); rb_define_alias(rb_cTrueClass, "inspect", "to_s"); rb_define_method(rb_cTrueClass, "&", true_and, 1); @@ -4718,6 +4726,8 @@ InitVM_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L4726 rb_deprecate_constant(rb_cObject, "TRUE"); rb_cFalseClass = rb_define_class("FalseClass", rb_cObject); + rb_cFalseClass_to_s = rb_fstring_enc_lit("false", rb_usascii_encoding()); + rb_gc_register_mark_object(rb_cFalseClass_to_s); rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0); rb_define_alias(rb_cFalseClass, "inspect", "to_s"); rb_define_method(rb_cFalseClass, "&", false_and, 1); diff --git a/spec/ruby/core/false/to_s_spec.rb b/spec/ruby/core/false/to_s_spec.rb index 40853da..c996d87 100644 --- a/spec/ruby/core/false/to_s_spec.rb +++ b/spec/ruby/core/false/to_s_spec.rb @@ -4,4 +4,14 @@ describe "FalseClass#to_s" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/false/to_s_spec.rb#L4 it "returns the string 'false'" do false.to_s.should == "false" end + + ruby_version_is "2.7" do + it "returns a frozen string" do + false.to_s.frozen?.should == true + end + + it "always returns the same string" do + false.to_s.should equal(false.to_s) + end + end end diff --git a/spec/ruby/core/nil/to_s_spec.rb b/spec/ruby/core/nil/to_s_spec.rb index c7a18e2..4b61f58 100644 --- a/spec/ruby/core/nil/to_s_spec.rb +++ b/spec/ruby/core/nil/to_s_spec.rb @@ -4,4 +4,14 @@ describe "NilClass#to_s" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/nil/to_s_spec.rb#L4 it "returns the string ''" do nil.to_s.should == "" end + + ruby_version_is "2.7" do + it "returns a frozen string" do + nil.to_s.frozen?.should == true + end + + it "always returns the same string" do + nil.to_s.should equal(nil.to_s) + end + end end diff --git a/spec/ruby/core/true/to_s_spec.rb b/spec/ruby/core/true/to_s_spec.rb index 30ca354..68b0052 100644 --- a/spec/ruby/core/true/to_s_spec.rb +++ b/spec/ruby/core/true/to_s_spec.rb @@ -4,4 +4,14 @@ describe "TrueClass#to_s" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/true/to_s_spec.rb#L4 it "returns the string 'true'" do true.to_s.should == "true" end + + ruby_version_is "2.7" do + it "returns a frozen string" do + true.to_s.frozen?.should == true + end + + it "always returns the same string" do + true.to_s.should equal(true.to_s) + end + end end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/