Issue #8909 has been updated by enebo (Thomas Enebo).


My take on this proposal is that deep and shallow freezing is not as important as knowing that the 'f' (which I think should mean fixed and not frozen) is that once the array literal evaluates it will not be possible to change its size.  What is inside can change but that is ok.  At a grammar level we know we can alloc an array of n elements once and never need to worry about allocating it again.

On top of that if the literal only contains immediate values or frozen/fixed literals then we can do a bunch of other optimizations.  Like never populate the array more than once.  Or even constant propagate elements to where they are used if we know [] is not overriden.  In my mind, the 'f' gives us a whole dimensions of things which we can do internally to improve performance. 

For a Ruby programmer it means they can never change the size of the data structure (or in case of a hash never change the keys anymore -- values can change).  I don't want to hijack an enhancement but let's do this :)

----------------------------------------
Feature #8909: Expand "f" frozen suffix to literal arrays and hashes
https://bugs.ruby-lang.org/issues/8909#change-42234

Author: headius (Charles Nutter)
Status: Feedback
Priority: Normal
Assignee: 
Category: 
Target version: next minor


The "f" suffix to declare a frozen string was recently accepted into 2.1, and I think it's a spectacular addition. I would very much like to see it work for literal arrays and hashes too:

[1, 2, 3, 4, 5]f

{foo: 1, bar: 2, baz: 3}f

There are many, many cases where this could reduce allocation (frozen array with literal elements would only need to be allocated once) and improve thread-safety (explicitly create frozen arrays and hashes when creating structures that might be used across threads).

Is there any reason why we could not do this? I believe both of the above syntaxes would be invalid today, as was the case with the String "f" suffix, and hopefully that means the work to add this syntax would be similar.


-- 
http://bugs.ruby-lang.org/