Live data from Hacker News

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

prateekcodes.dev

81–90 of 131 posts

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

#81

We learned nothing from Python 2->3 An obviously good change, actually massive performance improvements not hard to implement but its still gonna be such a headache and dependency hell

Even if you have an incompatible codebase that you don't wish to convert, you'll be able to set `RUBYOPT="--disable-frozen-string-literal"` so it keeps running.

And since that flag really doesn't require lots of work in the VM, it's likely to be kept around pretty much forever.

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

#83

The phrase “Frozen String Literals” is kind of weird to me. When I assign a string literal to a variable, I do not think of the variable itself as a “string literal.” That phrase is for the literal characters in between quotes in the code, which by definition are already “frozen.” They’re a static part of the code itself. This change makes it so that you cannot mutate a variable which was initialized using a string l…

That's not quite how Ruby and similar languages like Python or JS work.

Variables don't "contain" a string, they just point to objects on the heap.

So:

   my_string = same_string = "Hello World"
Here both variables are essentially pointers to a pre-existing object on the heap, and that object is immutable.

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

#84
post #27

Earlier quoted context omitted.

Also JavaScript.

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

Rust the language has one string type: &str

The standard library also has String, CString, CStr, OsString, and OsStr.

The latter four are for niche situations. 99.9% of the time, it's similar to Java: &str is Java's String, String is Java's StringBuffer/StringBuilder.

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

#85

The phrase “Frozen String Literals” is kind of weird to me. When I assign a string literal to a variable, I do not think of the variable itself as a “string literal.” That phrase is for the literal characters in between quotes in the code, which by definition are already “frozen.” They’re a static part of the code itself. This change makes it so that you cannot mutate a variable which was initialized using a string l…

In ruby, "frozen" is a property of some values that makes them immutable. I mean, other than the part where you can mutably unfreeze objects of many classes. (At least you can't unfreeze strings.) This change makes string values that come from literals initially frozen. It has nothing to do with variable bindings.

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

#86
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!

> 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?

1. Strings have a flag (FL_FREEZE) that are set when the string is frozen. This is checked whenever a string would be mutated, to prevent it.

2. There is an interned string table for frozen strings.

> Does it keep a reference count to each unique string that requires a set lookup to update on each string instance’s deallocation?

This I am less sure about, I poked around in the implementation for a bit, but I am not sure of this answer. It appears to me that it just deletes it, but that cannot be right, I suspect I'm missing something, I only dig around in Ruby internals once or twice a year :)

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

#87

Earlier quoted context omitted.

> for a web-focused scripting language it makes sense to default to mutable strings Why do you say that? I would say the opposite.

Because there’ll likely be a lot of string concatenation in a language whose job is to output HTML. Adding a single char to a 1k string will require allocating a modified copy, in mutable strings it’s just appending in a preallocated buffer.

If you are using string concatenation to build HTML in your application layer, you are fundamentally doing it wrong.

Ruby isn’t making all strings immutable here. Just string literals. You are free to allocate mutable strings that can be appended to, to your heart’s content. It is extremely rare that modifying a literal is intended behavior, since their contents are permanently persisted throughout the lifetime of your program. With your example, this would be like having one shared global buffer for your final document.

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

#89
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!

> 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? 1. Strings have a flag (FL_FREEZE) that are set when the string is frozen. This is checked whenever a string would be mutated, to prevent it. 2. There is an interned string table for frozen strings. > Does it keep a reference count to each unique string that requires…

There's no need for ref counting, since Ruby has a mark & sweep GC.

The interned string table uses weak references. Any string added to the interned string tables has the `FL_FSTR` flag set to it, and when a string a freed, if it has that flag the GC knowns to remove it from the interned string table.

The keyword to know to search for this in the VM is `fstring`, that's what interned strings are called internally:

- https://github.com/ruby/ruby/blob/b146eae3b5e9154d3fb692e8fe...

- https://github.com/ruby/ruby/blob/b146eae3b5e9154d3fb692e8fe...

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

#90
post #5
post #4

Is it the future path of any successful JIT / dynamic typed / scripting language to realize they needed all optimizations from compiled / statically typed / lower level languages ? Would Ruby be as successful if they had all those complicated features right from the start ? Or do all languages start from a nice simple clean slate tabula rasa to get developers hooked, until the language is enough famous to get well de…

I would actually say it’s the opposite in this case: it’s extremely common in scripting languages for strings to be immutable, mutable strings are usually only available in lower level languages. I’m very surprised Ruby had this feature at all.

Mutable strings were part of the perl heritage, which was one of the direct ancestors for ruby.
Post reply on HN