No need to allocate another SafeBuffer object if it's already html_safe?. - #17206
Closed
larrylv wants to merge 1 commit into
Closed
No need to allocate another SafeBuffer object if it's already html_safe?.#17206larrylv wants to merge 1 commit into
larrylv wants to merge 1 commit into
Conversation
When calling `html_escape` on SafeBuffer object, a new SafeBuffer object will always be allocated: https://github.com/rails/rails/blob/3dc9c52/activesupport/lib/active_support/core_ext/string/output_safety.rb#L22 It might be not necessary when `html_escape` a html_safe SafeBuffer object. So for html_safe SafeBuffer object, we should just return itself for `html_safe` method. Benchmark result are attached as below: Calculating ------------------------------------- return-self-if-html-safe 20116 i/100ms ------------------------------------------------- return-self-if-html-safe 1189398.8 (±11.7%) i/s - 5813524 in 5.000982 Calculating ------------------------------------- new-safe-buffer-even-if-html-safe 13813 i/100ms ------------------------------------------------- new-safe-buffer-even-if-html-safe 370526.7 (±12.0%) i/s - 1823316 in 5.007097
Contributor
Author
|
Here is the simple benchmark codes, I switched my git branch and change the reported string to show the benchmark result. $:.unshift "#{File.dirname(__FILE__)}/lib"
require 'active_support/core_ext/string'
require 'benchmark/ips'
require 'erb'
Benchmark.ips do |x|
TEST_STRING = "this is a string".html_safe
x.report("new-safe-buffer-even-if-html-safe") {
ERB::Util.html_escape TEST_STRING
}
end |
Member
|
This will change the behaviour and may break some applications. I believe we can move this check to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When calling
html_escapeon SafeBuffer object, a new SafeBufferobject will always be allocated:
https://github.com/rails/rails/blob/3dc9c52/activesupport/lib/active_support/core_ext/string/output_safety.rb#L22
It might be not necessary when
html_escapea html_safe SafeBuffer object.So for html_safe SafeBuffer object, we should just return itself for
html_safemethod.Benchmark result are attached as below: