Project

General

Profile

Feature #16153

Updated by Dan0042 (Daniel DeLorme) about 5 years ago

Freezing objects can give us a nice performance boost, but freezing previously non-frozen objects is a backward-incompatible change which is hard to handle because the place where the object is mutated can be far from where it was frozen, and tests might not cover the cases of frozen input vs non-frozen input. 

 I propose adding a flag which gives us a migration path for freezing objects. For purposes of discussion I will call this flag "eventually_frozen". It would act as a pseudo-frozen flag where mutating the object would result in a warning instead of an error. It would also change the return value of `Object#frozen?` so code like `obj = obj.dup if obj.frozen?` would work as expected to remove the warning. Note that eventually_frozen strings cannot be deduplicated, as they are in reality mutable. 

 This way it would be possible for Symbol#to_s (and many others) to return an eventually_frozen string in 2.7 which gives apps and gems time to migrate, before finally becoming a frozen deduplicated string in 3.0. This might even open up a migration path for eventually using `frozen_string_literal:true` as default. For example if it was possible to add `frozen_string_literal:eventual` to all files in a project (or as a global switch), we could run that in production to discover where to fix things, and then change it to `frozen_string_literal:true` for a bug-free performance boost. 

 Proposed changes: 
 * Object#freeze(immediately:true) Object#freeze(eventual:false) 
    * if `immediately` `eventual` keyword is true, false, set frozen=true and eventually_frozen=false 
    * if `immediately` `eventual` keyword is false, true, set eventually_frozen=true UNLESS frozen flag is already true 
 * String#+@ 
    * if eventually_frozen is true, create a duplicate string with eventually_frozen=false 
 * Object#frozen?(immediately:false) Object#frozen?(eventual:true) 
    * return true if `immediately` `eventual` keyword is false and eventually_frozen flag is are both true 
 * rb_check_frozen 
    * output warning if eventually_frozen flag is true 

Back