Freeze string literals when not mutated. #20946
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I wrote a utility that helps find areas where you could optimize your program using a frozen string instead of a string literal, it's called let_it_go. After going through the output and adding
.freeze
I was able to eliminate the creation of 1,114 string objects on EVERY request to codetriage. How does this impact execution?To look at memory:
Creating 1,114 string objects results in
Diff: 0.03125 mb
of RAM allocated on every request. Or 1mb every 32 requests.To look at raw speed:
We get the results
Now we can do some maths:
So we're shaving off 1 second of execution time for every 220 requests.
Is this going to be an insane speed boost to any Rails app: nope. Should we merge it: yep.
p.s. If you know of a method call that doesn't modify a string input such as String#gsub please give me a pull request to the appropriate file, or open an issue in LetItGo so we can track and freeze more strings.
Keep those strings Frozen