Skip to content

Commit

Permalink
Bring back memory optimization of caching lowercased node name
Browse files Browse the repository at this point in the history
In 4.6.2, rgrove#175 significantly cut down on memory usage by caching the lowercase
version of the node name. However, as reported in rgrove#177, this broke certain
transformers that modified the node name since the name was cached. We can
bring back this optimization by updating the node name only if it has been
changed.

Closes rgrove#184
  • Loading branch information
stanhu committed Jul 24, 2018
1 parent cee5bfa commit d721754
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/sanitize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ def to_html(node)
end

def transform_node!(node, node_whitelist)
# To minimize string duplication, cache the node name here.
node_name = node.name.downcase

@transformers.each do |transformer|
# Since transform_node! may be called in a tight loop to process thousands
# of items, we can optimize both memory and CPU performance by:
Expand All @@ -229,14 +232,18 @@ def transform_node!(node, node_whitelist)
config = @transformer_config
config[:is_whitelisted] = node_whitelist.include?(node)
config[:node] = node
config[:node_name] = node.name.downcase
config[:node_name] = node_name
config[:node_whitelist] = node_whitelist

result = transformer.call(config)

if result.is_a?(Hash) && result[:node_whitelist].respond_to?(:each)
node_whitelist.merge(result[:node_whitelist])
end

# It's possible that a transformer modifies the node name, so only
# copy the string again if it has changed.
node_name = node.name.downcase if node.name != node_name
end

node
Expand Down

0 comments on commit d721754

Please sign in to comment.