diff options
author | schneems <richard.schneeman@gmail.com> | 2015-07-24 23:35:18 -0500 |
---|---|---|
committer | schneems <richard.schneeman@gmail.com> | 2015-07-29 20:41:58 -0500 |
commit | 2a4d4301d405dbc4a6bce85b4632a78099c8d1c6 (patch) | |
tree | 3525a194930f1f3072602705806c0cc29b52b5f0 /actionview | |
parent | 1a14074fb525cde4a439a1fb7515fe417c37ff96 (diff) | |
download | rails-2a4d4301d405dbc4a6bce85b4632a78099c8d1c6.tar.gz rails-2a4d4301d405dbc4a6bce85b4632a78099c8d1c6.tar.bz2 rails-2a4d4301d405dbc4a6bce85b4632a78099c8d1c6.zip |
Decrease string allocation in content_tag_string
When an unknonwn key is passed to the hash in `PRE_CONTENT_STRINGS` it returns nil, when you call "#{nil}" it allocates a new empty string. We can get around this allocation by using a default value `Hash.new { "".freeze }`. We can avoid the `to_sym` call by pre-populating the hash with a symbol key in addition to a string key.
We can freeze some strings when using Array#* to reduce allocations.
Array#join can take frozen strings.
This change buys us 86,600 bytes of memory and 1,857 fewer objects per request.
Diffstat (limited to 'actionview')
-rw-r--r-- | actionview/lib/action_view/helpers/tag_helper.rb | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/actionview/lib/action_view/helpers/tag_helper.rb b/actionview/lib/action_view/helpers/tag_helper.rb index a87c223a71..a821bd9543 100644 --- a/actionview/lib/action_view/helpers/tag_helper.rb +++ b/actionview/lib/action_view/helpers/tag_helper.rb @@ -22,9 +22,10 @@ module ActionView TAG_PREFIXES = ['aria', 'data', :aria, :data].to_set - PRE_CONTENT_STRINGS = { - :textarea => "\n" - } + PRE_CONTENT_STRINGS = Hash.new { "".freeze } + PRE_CONTENT_STRINGS[:textarea] = "\n" + PRE_CONTENT_STRINGS["textarea"] = "\n" + # Returns an empty HTML tag of type +name+ which by default is XHTML # compliant. Set +open+ to true to create an open tag compatible @@ -143,7 +144,7 @@ module ActionView def content_tag_string(name, content, options, escape = true) tag_options = tag_options(options, escape) if options content = ERB::Util.unwrapped_html_escape(content) if escape - "<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name.to_sym]}#{content}</#{name}>".html_safe + "<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name]}#{content}</#{name}>".html_safe end def tag_options(options, escape = true) @@ -160,7 +161,7 @@ module ActionView attrs << tag_option(key, value, escape) end end - " #{attrs * ' '}" unless attrs.empty? + " ".freeze + attrs * ' '.freeze unless attrs.empty? end def prefix_tag_option(prefix, key, value, escape) @@ -177,7 +178,7 @@ module ActionView def tag_option(key, value, escape) if value.is_a?(Array) - value = escape ? safe_join(value, " ") : value.join(" ") + value = escape ? safe_join(value, " ".freeze) : value.join(" ".freeze) else value = escape ? ERB::Util.unwrapped_html_escape(value) : value end |