-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Freeze the "." character used in comparisons #181
base: master
Are you sure you want to change the base?
Conversation
… amount (5 MB min)
@@ -11,6 +11,9 @@ module Localization | |||
include DefaultConversions | |||
|
|||
class << self | |||
|
|||
DOT = ".".freeze | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace detected.
wondering should this benefit be extended anywhere else? looks good though. |
I guess @Fred-JulieDesk might be able to chime in on that :) Did the memory allocation report reveal any other strings that allocate considerable amount of memory? Personally I think it's better to leave the other static strings be if there's no evidence that they're harmful/eating memory :) |
Nice find! Though I think this might actually make loading this file in a bit slower (a very very tiny fraction slower) because the strings are already constants so they are only initialized once (in my understanding). So adding |
For the constantes part, it is a good practice to freeze them to forbid any ulterior modification as Ruby constantes are mutable. About the "TRANSLITERATIONS" part, the string is instantiated each time the method to get the conversion type is called (moreover it is true for every other conversion type). Also, each times the method is called, the string is upcased which result in a serious overhead and useless string transformations when it is called a lot. |
const_get conversion_type.upcase | ||
# %w{characters currencies html_entities transliterations vulgar_fractions} | ||
|
||
[['characters'.freeze, 'CHARACTERS'.freeze], ['currencies'.freeze, 'CURRENCIES'.freeze], ['html_entities'.freeze, 'HTML_ENTITIES'.freeze], ['transliterations'.freeze, 'TRANSLITERATIONS'.freeze], ['vulgar_fractions'.freeze, 'VULGAR_FRACTIONS'.freeze]].each do |conversion_type| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [284/80]
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
(Disclaimer: this change is written by @Fred-JulieDesk, I take no credit or responsibility, I'm just the pull request creator :) )
This change freezes the
.
character that is used in comparison of the translation key. This way, we don't allocate a new string object every time the comparison is made and save a bit of memory.See #180 for more.