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 strings to prevent re-allocation of newline/empty strings #961
Changes from 1 commit
f70a7e5
6858136
b939015
42f4eaa
de05e61
2130e84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ def on_code(exp) | |
end | ||
|
||
def on_newline | ||
"\n" | ||
"\n".freeze | ||
end | ||
|
||
private | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,9 @@ module TagHelper | |
def content_tag_with_haml(name, *args, &block) | ||
return content_tag_without_haml(name, *args, &block) unless is_haml? | ||
|
||
preserve = haml_buffer.options.fetch(:preserve, %w[textarea pre code]).include?(name.to_s) | ||
@_content_tag_name_cache ||= {} | ||
name_string = @_content_tag_name_cache[name] ||= name.to_s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idea here being that we only needed to run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance is within margin of error but saves a string allocation for each time this method is called. Warming up --------------------------------------
master_version 188.121k i/100ms
fast_version 201.722k i/100ms
Calculating -------------------------------------
master_version 5.176M (±10.1%) i/s - 25.584M in 5.007528s
fast_version 5.595M (± 7.3%) i/s - 27.838M in 5.004395s
Comparison:
fast_version: 5595000.8 i/s
master_version: 5176423.8 i/s - same-ish: difference falls within error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your above benchmark is now expired after you add constant, so you should put benchmark again. As I said,
So at least I wouldn't merge it unless there's SIGNIFICANT performance benefit in benchmark. Basically |
||
preserve = haml_buffer.options.fetch(:preserve, %w[textarea pre code]).include?(name_string) | ||
|
||
if block_given? && block_is_haml?(block) && preserve | ||
return content_tag_without_haml(name, *args) do | ||
|
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.
If anyone has insight into this issue and is willing to help me fix the code/test to be able to freeze this string that would be much appreciated! 😍
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.
Unfortunately I have no time to assist you, but I would write many
binding.pry
s to see how string that shouldn't be freezed is freezed.