Make ActiveSupport frozen-string-literal friendly.#29506
Make ActiveSupport frozen-string-literal friendly.#29506matthewd merged 5 commits intorails:masterfrom
Conversation
|
I think it's good if we make Rails internals "frozen string" friendly. Though I'd prefer if we used |
|
Fair enough :) I meant to comment on the different approaches ( |
The ActiveSupport test suite only passes currently if it uses the latest unreleased commits for dalli, and a patch for Builder: rails/builder#6 Beyond that, all external dependencies (at least, to the extent they’re used by ActiveSupport) are happy, including Nokogiri as of 1.8.0.
8859757 to
036bdee
Compare
Includes two external changes because they're referenced within the ActiveModel test suite.
|
Incidentally, the encoding of the empty string generated by String.new.encoding # => #<Encoding:ASCII-8BIT>
String.new("").encoding # => #<Encoding:UTF-8>
"".dup.encoding # => #<Encoding:UTF-8>
"".encoding # => #<Encoding:UTF-8>I also think that it is preferable to use the |
Plus a couple of related ActionPack patches.
|
Thanks for that note @koic, good to know :) I've gone ahead and made changes to ActiveModel, ActiveRecord, ActionMailer and ActionView. ActionJob didn't need any changes. For ActionCable, ActionPack and Railties, there are underlying dependencies that need patching (redis, rack, and thor, respectively), so I'll look into those tomorrow. If anyone with particular focus/expertise on the libraries I have patched would like to review, please go ahead! |
|
Also, @tenderlove: I updated my Builder patch to use |
|
Can we add the magic comment to the files that were fixed? |
|
@rafaelfranca we could, but that's not going to help stop bad uses of frozen string literals appear in all the other files. We could add the comment to all current files, but still, any new files that get added to Rails would need to have it as well - which may be acceptable? Using either pragmater or |
|
@pat thanks for this! I think the comment is important for these files in particular because otherwise @rafaelfranca perhaps we should consider just adding the comment everywhere, and enabling the relevant rubocop rule. Once we (read: @pat 😉) have done the work to find all the strings that need a |
|
Bonus thought: even with the magic comment in every file, that wouldn't (AFAIK) cover eval calls, of which we have a few -- though most won't contain strings. Probably narrow enough that we can ignore enforcement for now, until we can go full RUBYOPT on our test suite. |
+1. On that note, are there actual benchmarks that show the frozen string default helping (especially for a real app)? Last thing I saw was a modest speed increase for micro benchmarks. But the last tests I saw were probably 2 years ago when we were discussing the feature on ruby-core. I still think we should move forward with frozen string literals (because I like them), I'm just genuinely curious about real performance. |
That is the idea. I did that already in arel. |
Make ActiveSupport frozen-string-literal friendly.
As I'm sure many folks are aware, MRI 2.3 added the ability to have all string literals frozen by default, which has performance/memory usage benefits. It will become the default behaviour in MRI 3.0, though that is quite some time away!
This PR covers what's needed to make ActiveSupport play nicely when this feature is enabled. The test suite as it currently stands will continue to pass, there are no behaviour changes. Yes, it's for ActiveSupport only, but I figure it's a good place to start.
If this change is appreciated, then I can work further on the rest of the Rails gems to ensure they're working with frozen string literals as well. And if you want to have Travis run the tests with checks for this behaviour, then using pragmater and a test script can help with that (such as what I've done in my own gems and suggested for others).
Running the tests with frozen string literals enabled
Of course, if you want to run the test suite with frozen string literals enabled, you'll need the following locally:
Beyond that, all external dependencies (at least, to the extent they’re used by ActiveSupport) play happily with frozen string literals enabled by default in MRI 2.4 (which can be done so by the
RUBYOPTenvironment variable:RUBYOPT="--enable-frozen-string-literal").