Skip to content

No need to allocate another SafeBuffer object if it's already html_safe?. - #17206

Closed
larrylv wants to merge 1 commit into
rails:masterfrom
larrylv:add-html-safe-to-safe-buffer
Closed

No need to allocate another SafeBuffer object if it's already html_safe?.#17206
larrylv wants to merge 1 commit into
rails:masterfrom
larrylv:add-html-safe-to-safe-buffer

Conversation

@larrylv

@larrylv larrylv commented Oct 8, 2014

Copy link
Copy Markdown
Contributor

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

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
@larrylv

larrylv commented Oct 8, 2014

Copy link
Copy Markdown
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

@rafaelfranca

Copy link
Copy Markdown
Member

This will change the behaviour and may break some applications. I believe we can move this check to html_escape. WDYT?

cc @jeremy @matthewd @tenderlove

@larrylv larrylv closed this Nov 11, 2014
@larrylv
larrylv deleted the add-html-safe-to-safe-buffer branch November 11, 2014 03:38
@larrylv
larrylv restored the add-html-safe-to-safe-buffer branch November 11, 2014 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants