1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[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]
This commit is contained in:
Jean Boussier 2019-09-26 17:27:47 +02:00 committed by Nobuyoshi Nakada
parent 2082a26dc7
commit eff15a269f
Notes: git 2019-09-27 13:53:00 +09:00
5 changed files with 51 additions and 3 deletions

8
NEWS
View file

@ -163,6 +163,14 @@ Integer::
0b01001100[2...6] #=> 0b0011 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:: Module::
New method:: New method::

View file

@ -40,6 +40,10 @@ VALUE rb_cNilClass; /*!< NilClass class */
VALUE rb_cTrueClass; /*!< TrueClass class */ VALUE rb_cTrueClass; /*!< TrueClass class */
VALUE rb_cFalseClass; /*!< FalseClass 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 */ /*! \cond INTERNAL_MACRO */
#define id_eq idEq #define id_eq idEq
@ -1442,7 +1446,7 @@ nil_to_f(VALUE obj)
static VALUE static VALUE
nil_to_s(VALUE obj) 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)
static VALUE static VALUE
true_to_s(VALUE obj) 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)
static VALUE static VALUE
false_to_s(VALUE obj) false_to_s(VALUE obj)
{ {
return rb_usascii_str_new2("false"); return rb_cFalseClass_to_s;
} }
/* /*
@ -4609,6 +4613,8 @@ InitVM_Object(void)
rb_define_global_function("Hash", rb_f_hash, 1); rb_define_global_function("Hash", rb_f_hash, 1);
rb_cNilClass = rb_define_class("NilClass", rb_cObject); 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_i", nil_to_i, 0);
rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0); rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0); rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
@ -4703,6 +4709,8 @@ InitVM_Object(void)
rb_deprecate_constant(rb_cObject, "Data"); rb_deprecate_constant(rb_cObject, "Data");
rb_cTrueClass = rb_define_class("TrueClass", rb_cObject); 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_method(rb_cTrueClass, "to_s", true_to_s, 0);
rb_define_alias(rb_cTrueClass, "inspect", "to_s"); rb_define_alias(rb_cTrueClass, "inspect", "to_s");
rb_define_method(rb_cTrueClass, "&", true_and, 1); rb_define_method(rb_cTrueClass, "&", true_and, 1);
@ -4718,6 +4726,8 @@ InitVM_Object(void)
rb_deprecate_constant(rb_cObject, "TRUE"); rb_deprecate_constant(rb_cObject, "TRUE");
rb_cFalseClass = rb_define_class("FalseClass", rb_cObject); 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_method(rb_cFalseClass, "to_s", false_to_s, 0);
rb_define_alias(rb_cFalseClass, "inspect", "to_s"); rb_define_alias(rb_cFalseClass, "inspect", "to_s");
rb_define_method(rb_cFalseClass, "&", false_and, 1); rb_define_method(rb_cFalseClass, "&", false_and, 1);

View file

@ -4,4 +4,14 @@ describe "FalseClass#to_s" do
it "returns the string 'false'" do it "returns the string 'false'" do
false.to_s.should == "false" false.to_s.should == "false"
end 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 end

View file

@ -4,4 +4,14 @@ describe "NilClass#to_s" do
it "returns the string ''" do it "returns the string ''" do
nil.to_s.should == "" nil.to_s.should == ""
end 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 end

View file

@ -4,4 +4,14 @@ describe "TrueClass#to_s" do
it "returns the string 'true'" do it "returns the string 'true'" do
true.to_s.should == "true" true.to_s.should == "true"
end 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 end