Live data from Hacker News

Ruby 3.4 frozen string literals: What Rails developers need to know

prateekcodes.dev

41–50 of 131 posts

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#41
post #37

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.

Mutable strings can be duplicated too. You can use reference counting, or borrow checking in Rust.

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#42
post #40

How 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!

The literals would be identified at parse time.

    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

#43
post #40

How 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!

Even if it didn't dedupe strings, mutable string literals means that it has to create a new string every time it encounters a literal in run time. If you have a literal string in a method, every time you call the method a new string is created. If you have one inside a loop, every iteration a new string is created. You get the idea.

With immutable strings literals, string literals can be reused.

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#44

Earlier 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

Well thats great, guess I have carried that baggage with me as misinformation for years now.

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#45
post #30

Earlier 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.

dynamically created symbols have been garbage collected for almost 10 years

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#46
post #40

How 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!

The way it works in Python is that string literals are stored in a constant slot of their parent object, so at runtime the VM just returns the value at that index.

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

#48
post #45

Earlier 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

Yeah I am a moron. Updated.

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#49
Well it is not quite a mutable vs immutable strings war, nor Ruby being late to the party or something like that.

The 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

#50
post #27

Earlier quoted context omitted.

Also JavaScript.

Java too. Rust has only 99 string types, some of those are immutable also.

It does not really matter in rust anyway: literals are &’static str so naturally immutable and essentially free, and mutable strings require jumping hoops to share, so it’s hard to unwittingly have one mutate from under you.
Post reply on HN