Kurt StephensNerd Up! | ||
Ruby: Caching #to_s for immutables (and a possible future for constant-folding)Kurt on Mon, 2009-11-23 02:07.
Reference: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/26869 I have a proof-of-concept patch to MRI that caches It reduces the number of It requires a minor semantic change to Ruby core. This minor change could cascade into a huge performance improvement for all Ruby implementations — as will be illustrated later: #to_s may return frozen This appears to not be a problem since any callers of For code that may expect This caching technique could be extended into other immutables (e.g.: the In the patch, If this new semantic for In practice, most Ruby puts :"some string" instead of puts "some string" as an in-line memoized frozen String that creates no garbage when calling This is far more expressive and concise than: SOME_STRING = "some string".freeze ... puts SOME_STRING The alternative to str = '' # Make a mutable empty string. str << "foo" # "foo" is garbage str << "bar" # "bar" is garbage would become: str = ''.dup # Make a mutable empty string. str << "foo" # "foo" is not garbage str << "bar" # "bar" is not garbage The latter is backwards-compatible with the current Interesting analysis..Submitted by Anonymous (not verified) on Wed, 2009-12-16 18:21.
The underlying assumption that I question however is that ‘garbage’ is necessarily bad. At some point garbage gets reclaimed. Interned strings stay around for the duration of the process.. While for some strings (representations of status codes, etc) this is desirable, the default behavior of garbage/recycle, while it burns a few more cycles, so i don’t agree with the implicit interning approach you have (plus, that necesitates ‘’.dup per your example, which is a semantic change I dont think people will like) I do like the caching for immutables, and dup_if/unless_frozen – and clearly some good investigation went into this… These concepts are useful ones to wield… » reply
Interned StringsSubmitted by Kurt on Thu, 2009-12-24 12:43.
All String literals stay around for the duration of the process, they are attached to the syntax Nodes in MRI 1.8 or the bytecodes in MRI 1.9 that represent the executable Ruby code. » reply
|
||
Recent comments
1 week 2 days ago
3 weeks 5 days ago
3 weeks 6 days ago
3 weeks 6 days ago
4 weeks 33 min ago
4 weeks 2 hours ago
6 weeks 2 days ago
6 weeks 2 days ago
8 weeks 6 days ago
8 weeks 6 days ago