Issue #15940 has been updated by duerst (Martin D=FCrst).


I think this change makes a lot of sense, not only because of the memory sa=
vings, but also because of the streamlining for encodings in general.

I agree that the chance of backwards incompatibilities in actual programs i=
s very low. I think this is a good change for Ruby 2.7.

----------------------------------------
Feature #15940: Coerce symbols internal fstrings in UTF8 rather than ASCII =
to better share memory with string literals
https://bugs.ruby-lang.org/issues/15940#change-78727

* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
* Assignee: =

* Target version: =

----------------------------------------
Patch: https://github.com/ruby/ruby/pull/2242

It's not uncommon for symbols to have literal string counterparts, e.g.

```ruby
class User
  attr_accessor :name

  def as_json
    { 'name' =3D> name }
  end
end
```

Since the default source encoding is UTF-8, and that symbols coerce their i=
nternal fstring to ASCII when possible, the above snippet will actually kee=
p two instances of `"name"` in the fstring registry. One in ASCII, the othe=
r in UTF-8.

Considering that UTF-8 is a strict superset of ASCII, storing the symbols f=
strings as UTF-8 instead makes no significant difference, but allows in mos=
t cases to reuse the equivalent string literals.

The only notable behavioral change is `Symbol#to_s`.

Previously `:name.to_s.encoding` would be `#<Encoding:US-ASCII>`.
After this patch it's `#<Encoding:UTF-8>`. I can't foresee any significant =
compatibility impact of this change on existing code.

However, there are several ruby specs asserting this behavior, but I don't =
know if they can be changed or not: https://github.com/ruby/spec/commit/a73=
a1c11f13590dccb975ba4348a04423c009453

If this specification is impossible to change, then we could consider chang=
ing the encoding of the String returned by `Symbol#to_s`, e.g in ruby pseud=
o code:

```ruby
def to_s
  str =3D fstr.dup
  str.force_encoding(Encoding::ASCII) if str.ascii_only?
  str
end
```





-- =

https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>