From: jean.boussier@... Date: 2019-09-26T13:35:16+00:00 Subject: [ruby-core:95108] [Ruby master Feature#16150] Add a way to request a frozen string from to_s Issue #16150 has been updated by byroot (Jean Boussier). Ok, so I just finished updating our application to be compatible with both `Symbol#to_s` and `Module#name` returning frozen strings. Over the 502 gems used in the repo, I found 5 impacted gems, all because of `Symbol#to_s`, and all in very minor ways: - A [`method_missing` in Pry](https://github.com/pry/pry/blob/fa97d5c2997ff1cf03cf925df482a7a1d9ca3ea3/lib/pry/slop.rb#L457) - Some [string manipulation in the grpc gem](https://github.com/grpc/grpc/blob/9810d217709e536a49625c32d45ff6cbbe535e18/src/ruby/lib/grpc/generic/service.rb#L33-L39) - Also some encoding coercion in [activerecord-databasevalidations](https://github.com/wvanbergen/activerecord-databasevalidations/pull/23) - The [memoist gem](https://github.com/matthewrudy/memoist/blob/510cb571cdea0fcb72fa8ec7560bc876f933f442/lib/memoist.rb#L40-L51) - [Another `method_missing` in google-cloud-core](https://github.com/googleapis/google-cloud-ruby/blob/bbaf9f27e8ea8251a99049cf99fed9c755a29dd1/google-cloud-core/lib/google/cloud/config.rb#L458-L466) - And of course the Rails one mentioned above but it got fixed this morning. Inside our repo of over 1 million lines of code, I found 4 impacted methods, all were trivially fixed. Again they were all caused by `Symbol#to_s`, and very similar in nature to the public ones I listed above. Overall I was able to trivially update a gigantic app to be ready for this change, all that in under an hour. ---------------------------------------- Feature #16150: Add a way to request a frozen string from to_s https://bugs.ruby-lang.org/issues/16150#change-81742 * Author: headius (Charles Nutter) * Status: Assigned * Priority: Normal * Assignee: Eregon (Benoit Daloze) * Target version: ---------------------------------------- Much of the time when a user calls to_s, they are just looking for a simple string representation to display or to interpolate into another string. In my brief exploration, the result of to_s is rarely mutated directly. It seems that we could save a lot of objects by providing a way to explicitly request a *frozen* string. For purposes of discussion I will call this to_frozen_string, which is a terrible name. This would reduce string allocations dramatically when applied to many common to_s calls: * Symbol#to_frozen_string could always return the same cached String representation. This method is *heavily* used by almost all Ruby code that intermingles Symbols and Strings. * nil, true, false, and any other singleton values in the system could similarly cache and return the same String object. * The strings coming from core types could also be in the fstring cache and deduplicated as a result. * User-provided to_s implementations could opt-in to caching and returning the same frozen String object when the author knows that the result will always be the same. A few ideas for what to call this: * `to_fstring` or `fstring` reflects internal the "fstring" cache but is perhaps not obvious for most users. * `to_s(frozen: true)` is clean but there will be many cases when the kwargs hash doesn't get eliminated, making matters worse. * `def to_s(frozen = false)` would be mostly free but may not be compatible with existing to_s params (like `Integer#to_s(radix)` This idea was inspired by @schneems's talk at RubyConf Thailand, where he showed significant overhead in ActiveRecord from Symbol#to_s allocation. -- https://bugs.ruby-lang.org/ Unsubscribe: