fbpx

We keep the cop enabled, but never freeze interpolated strings. It is usually not useful, unless we anticipate that certain variants are instantiated very frequently per-process. There were originally some code fragments in RDoc assuming that a string is mutable. To enable frozen-string-literal, they were changed, which brought performance degradation. After that, enabling frozen-string-literal made RDoc a bit faster, but the net result was worse than the original. Want to use frozen strings; with the toplevel comment overriding this anyway.

I’d love to see some sort of warning happen in 2.7…perhaps a flag you can pass or just a new warning that recommends alternative code or the appropriate pragma. If the performance improvement can be discarded, even the addition is unnecessary. It is said that 90% of the execution time of a computer program is spent executing 10% of the code. Making 100% of the code immutable resembles “premature optimization is the root of all evil”. The magic comment can be now accepted as a migration path. If you’re lucky enough to have a situation where all of your dependencies are frozen-string-literal friendly, then you can just use the RUBYOPT environment variable.

ruby frozen string literal

I need to make sure that the option will be guaranteed to remain exist in future versions. This effect can win the small code bloat with dup (or String.new or something new). We will need to choose “foo” and “foo”.dup instead. Matz said “All String literals are immutable on Ruby 3”.

One way or another, it’s quite probable that you will encounter some bugs. Mainly because you’re relying in 3rd party gems that are not prepared for immutable strings. Ruby ecosystem will need some time to adapt to this new feature, once most popular gems start to adapt frozen string literals the transition will be smoother. I prefer it being immutable by default as it even makes it easier for someone reading a code to understand whether the string is meant to be changed or not… In other words, I also think it improves code reading, so usability, besides the performance improvement. I’d love to see immutable string literals by default.

Let’s take a look at what this all means, and how it will affect every day programming in Ruby. That’s why the location of the string literal creation is added to the error message, helping you to find where the issue is. Immutable string literals also open the door to future performance improvements, particularly ones that reduce GC pressure. That said, it’s unclear if these benchmarks run long enough or allocate enough memory to even measure the performance effects it has on garbage collection. In theory without immutable strings it will create a million objects, one for each ‘a’ and when we switch to immutable strings it should create only one. We can’t expect to have exactly one or one million objects created, as when we measure we’re changing the subject under observation.

Finally, the renamed flag is only required to trace dynamic string literals. Perhaps I’m missing something else that can be gained by using frozen string literals, other than the intent of being ready for Ruby 3? In Ruby 2.3, you can use this magic comment to prepare for frozen string literals being the default in Ruby 3. This only demonstrates that when the interpreter works with repetitive immutable strings it will create less objects thus the GC will have less work to do.

When you freeze a string literal, you’re telling Ruby to not let any of your programs modify the string literal . Same value literals points to the same object, i.e. ‘a’ and ‘a’ points to the same object. In Ruby we can compare two objects in different ways. Let’s run a small test with and without frozen strings to see what’s changed.

Alternative ways to freeze a string in Ruby

Technically, you don’t give the frozen thing a new value, but give the variable referencing the frozen thing a new value. 12 Interesting Environment Friendly Projects on Kickstarter Frozen is a property of the object, not variable. I worry those options will be disappeared in future versions.

Maybe I was seeing the effects of cold cache calls and things would improve overnight. The returned String will be deduplicated as long as it does not have any instance variables set on it. Because clearly the only reason we’d want immutable state by default is FOR SPEED?

ruby frozen string literal

We have had the syntactic hacks (.freeze etc), frozen-string-literal pragma, and the command-line global flag for many years now. A very large corpus of existing code already opts into frozen string literals today, including most of Rails and its dependencies. RuboCop and other tools have been recommending users include the frozen-string-literal pragma as well. MRI could record whether a frozen string was explicitly frozen by the code. This would give some warnings until all cases are fixed and the switch could be then turned off for applications which fixed all cases… If you’re preparing for the upcoming Ruby 2.3 release, you’re in the right place.

Enabled

So, please keep “mutable by default”, and reconsider if the setting means (magic comment and command-line argument) are really suitable for the enterprise development style. It makes that frozen string literal can not have escape sequences such as “\n”. I think that a transition path across multiple releases that slowly increases the severity of the warning will help us identify how big of a problem the change will be. At the same time, it will help us transition to the new semantics a bit at a time.

ruby frozen string literal

But an interpolated string is not actually a string literal but rather it can be called as a dynamic string. When this feature was on the roadmap for Ruby 3, this cop made sense to me by default to help make sure that I would be able to reduce the pain in upgrading. How to Build a Calendar App in 2022-2023 However, it’s now off the roadmap, so I’m not sure that this cop makes sense as something to be enabled by default at this point. By the time Ruby 3.0 is released in late 2020, all we should need to do is enable frozen-string-literal by default.

Gems with Frozen-String-Literal Friendly Commits

In this case, it significantly damages usability and compatibility. Also, I’m afraid if it is not so useful in terms of performance improvement. I Remote AWS Cloud Engineer Jobs think the major factor why matz accept “immutable by default” is not this effect. Finally accepted as a migration path to “immutable by default”.

Even small incompatibilities, it’s a real burden for users if there are many… Without having to manually do an extra .dup to make a string mutable. The magic comment is no problem, at least not to me, if I understood it correctly. After this issue, developers can write clean and fast code with less brain power and the social problem is solved.

  • It is usually not useful, unless we anticipate that certain variants are instantiated very frequently per-process.
  • Finally, the renamed flag is only required to trace dynamic string literals.
  • This effect can win the small code bloat with dup (or String.new or something new).
  • Note that String.new with no argument returns an ASCII-8BIT string.
  • RuboCop and other tools have been recommending users include the frozen-string-literal pragma as well.

I believe Rails 5 and 6 both use it, and, given how large Rails is, I doubt they would have added it without a good reason. Frozen strings may result in time and/or memory optimizations for some applications. Whether the improvements are significant is another matter. Frozen string literal comment must be set to `true`. Note that the cop will accept files where the comment exists but is set to `false` instead of `true`.

Comments

I see no reason why 2.3 cannot also have –error-frozen-strings. As an opt-in I think it will give people more of a chance to make sure their code works. While comments are typically ignored by Ruby, special “magic comments” contain directives that affect how the code is interpreted.

In making this change, the Ruby core developers have done a lot of work to decide how much performance benefit it will really bring. Identical string literals are relatively common, particularly in large frameworks. Some estimates put the saving of object allocation as high as 30 percent of all string literals.

Don’t forget to add that to your CI service as well to avoid any regression issues in your test suite. Put all of this together, and the overall advantage of this feature is reduced memory usage, with the added benefit of avoiding potentially unexpected string modifications. Just add this comment in the first line of your files. Response times drastically improved as you can see from the above graph. Mike includes some benchmarks that show improved memory footprint for Sidekiq. The returned String will be deduplicated if it has no instance variables.

You can also freeze a mutable string with unary -. It sounds like there is a benefit to use this feature but not everywhere, the exact result I saw in performance graphs. Of course, symbols have a bit more restrictive rules to write, so it might be that for clarity or other reasons you cannot/don’t want to use symbols. As you can notice, the key has the same object_id. But values have different object_id, so they are not the same object. Thus we are not only making the string close to modifications but we are also re-using the same object.

Share

Post comment

Your email address will not be published. Required fields are marked *

go top