Earlier quoted context omitted.
> They're just not especially performant What? Mutable strings are more performant generally. Sometimes immutability allows you to use high level algorithms that provide better performance, but most code doesn't take advantage of that.
Not in general. Immutable strings can be deduplicated, leading to a different performance tradeoff that is often quite good. This is mentioned in TFA.
Ruby 3.4 frozen string literals: What Rails developers need to know
41–50 of 131 posts
Re: Ruby 3.4 frozen string literals: What Rails developers need to know
#42How does this work under the hood? Does Ruby keep a giant map of all strings in the application to check new strings against to see if it can dedupe? Does it keep a reference count to each unique string that requires a set lookup to update on each string instance’s deallocation? Set lookups in a giant set can be pretty expensive!
fooLit = "foo"
fooVar = "f".concat("o").concat("o")
This would have fooLit be frozen at parse time. In this situation there would be "foo", "f", and "o" as frozen strings; and fooLit and fooVar would be two different strings since fooVar was created at runtime.Creating a string that happens to be present in the frozen strings wouldn't create a new one.
Re: Ruby 3.4 frozen string literals: What Rails developers need to know
#43How does this work under the hood? Does Ruby keep a giant map of all strings in the application to check new strings against to see if it can dedupe? Does it keep a reference count to each unique string that requires a set lookup to update on each string instance’s deallocation? Set lookups in a giant set can be pretty expensive!
With immutable strings literals, string literals can be reused.
Re: Ruby 3.4 frozen string literals: What Rails developers need to know
#44Earlier quoted context omitted.
I’ll just kill the comment. It said Symbol isn’t garbage collected. It has been since 2.2 and I wasn’t aware. Sorry. Good reminder that anyone can go on the internet, just say stuff, and be wrong.
Symbols have been GCed since CRuby 2.2 https://bugs.ruby-lang.org/issues/9634
Re: Ruby 3.4 frozen string literals: What Rails developers need to know
#45Earlier quoted context omitted.
In Ruby you tend to use :symbol for small immutable strings << is inplace append operator for strings/arrays, while + is used to make copy. So += will make new string & rebind variable
I’ll just kill the comment. It said Symbol isn’t garbage collected. It has been since 2.2 and I wasn’t aware. Sorry. Good reminder that anyone can go on the internet, just say stuff, and be wrong.
Re: Ruby 3.4 frozen string literals: What Rails developers need to know
#46How does this work under the hood? Does Ruby keep a giant map of all strings in the application to check new strings against to see if it can dedupe? Does it keep a reference count to each unique string that requires a set lookup to update on each string instance’s deallocation? Set lookups in a giant set can be pretty expensive!
Though since Ruby already has symbols which act as immutable interned strings, frozen literals might just piggyback on that, with frozen strings being symbols under the hood.
Re: Ruby 3.4 frozen string literals: What Rails developers need to know
#47Re: Ruby 3.4 frozen string literals: What Rails developers need to know
#48Earlier quoted context omitted.
I’ll just kill the comment. It said Symbol isn’t garbage collected. It has been since 2.2 and I wasn’t aware. Sorry. Good reminder that anyone can go on the internet, just say stuff, and be wrong.
dynamically created symbols have been garbage collected for almost 10 years
Re: Ruby 3.4 frozen string literals: What Rails developers need to know
#49The move is so we can avoid allocating a string each we declare and use it since it will be frozen by default. It is a big optimization for GC mainly. Before we had to do such optimization by hand if we intend not to modify it:
# before
def my_method
do_stuff_with("My String") # 1 allocation at each call
end
# before, optim
MY_STRING = "My String".freeze # this does 2 allocations with 1 at init being GC quite early
def my_method
do_stuff_with(MY_STRING)
end
# after
def my_method
do_stuff_with("My String") # 1 allocation first time
end
But this move also complicates strings manipulation in the sense of it will lean users toward immutable ops that tend to allocate a lot of strings. foo.upcase.reverse
# VS
bar = foo.dup
bar.upcase!
bar.reverse!
So now we have to be deliberate about it: my_string = +"My String" # it is not frozen
We have frozen string literals for quite a while now, enabled file by file with the "frozen_string_literal: true" comment and I've seen it as the recommended way by the community and the de-facto standard in most codebase I've seen. It is generally enforced by code quality tools like Rubocop.So the mutable vs immutable is well known, and as it is part of the language, well, people should know the ins and outs.
I'm just a bit surprised that they devised this long path toward real frozen string literals, because it is already ongoing for years with the "frozen_string_literal: true" comment. Maybe to add proper warnings etc. in a way that does not "touch" code ? I prefer the explicit file by file comment. And for deps, well, the version bump of Ruby adding frozen string literals by default is quite a filter already.
Well, Ruby is well alive and it is what matters)
Re: Ruby 3.4 frozen string literals: What Rails developers need to know
#50Earlier quoted context omitted.
Also JavaScript.
Java too. Rust has only 99 string types, some of those are immutable also.