Project

General

Profile

Actions

Feature #11066

closed

symbol.to_s performance - caching and returning a frozen string

Added by rdsubhas (Subhas Dandapani) almost 9 years ago. Updated over 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:<unknown>]

Description

Hi, this is my first report so please bear with me if its bad.

Symbol.to_s creates a new string upon every call. This seems to be a huge waste of time. In typical Rails applications, "to_json" is called hundreds of times for ActiveRecord models on each request. Many symbols are being converted to string and GCd again and again. Is it possible to cache and return a Frozen string (string.freeze) when symbol.to_s is called? Now that symbols are also garbage collected, the string can also be garbage collected.

I tried getting some performance benchmarks, but with Ruby 2.1, I'm not able to modify Symbol class. I believe Symbols are now frozen internally so any modifications are not possible. So I was not able to do any monkey patching or benchmarking. But I can confirm that new strings are getting created every time, please see attached file. I believe caching this would improve performance. It would also increase memory usage, but since symbols are also garbage collected, the memory can be freed up over time, and anyways we can cache only when "symbol.to_s" is invoked first time.


Files

symbol_to_s_mem_usage.rb (283 Bytes) symbol_to_s_mem_usage.rb rdsubhas (Subhas Dandapani), 04/13/2015 08:32 PM

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #11346: New #intern_str or similarly-named method of Symbol to return a cached, frozen string (like "string".freeze)Rejectedmatz (Yukihiro Matsumoto)Actions
Actions #1

Updated by nobu (Nobuyoshi Nakada) almost 9 years ago

I tried it and found some libraries (ostruct.rb at least) depend on the current behavior.

Actions #2

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

  • Related to Feature #11346: New #intern_str or similarly-named method of Symbol to return a cached, frozen string (like "string".freeze) added
Actions #3

Updated by naruse (Yui NARUSE) almost 8 years ago

  • Assignee deleted (core)
Actions #4

Updated by jeremyevans0 (Jeremy Evans) over 2 years ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0