Skip to content

Commit fca941b

Browse files
k0kubunbyroot
andcommitted
Avoid continiously instantiating HTMLEntities
Co-authored-by: Jean Boussier <[email protected]>
1 parent d2652b7 commit fca941b

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

benchmarks/lobsters/app/models/story.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def description_or_story_text(chars = 0)
503503
s = s.to_s[0, chars].gsub(/ [^ ]*\z/, "")
504504
end
505505

506-
HTMLEntities.new.decode(s.to_s)
506+
HtmlEncoder.encode(s.to_s)
507507
end
508508

509509
def domain_search_url

benchmarks/lobsters/app/views/comments/index.rss.erb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<% coder = HTMLEntities.new %>
32
<rss version="2.0">
43
<channel>
54
<title><%= Rails.application.name %><%= @title.present? ?
@@ -9,14 +8,13 @@
98

109
<% @comments.each do |comment| %>
1110
<item>
12-
<title>on <%= raw coder.encode(comment.story.title, :decimal) %></title>
11+
<title>on <%= raw HtmlEncoder.encode(comment.story.title) %></title>
1312
<link><%= comment.url %></link>
1413
<guid><%= comment.short_id_url %></guid>
1514
<author><%= comment.user.username %>@users.<%= Rails.application.domain %> (<%= comment.user.username %>)</author>
1615
<pubDate><%= comment.created_at.rfc2822 %></pubDate>
1716
<comments><%= comment.url %></comments>
18-
<description><%= raw coder.encode(comment.markeddown_comment,
19-
:decimal) %></description>
17+
<description><%= raw HtmlEncoder.encode(comment.markeddown_comment) %></description>
2018
</item>
2119
<% end %>
2220
</channel>

benchmarks/lobsters/app/views/home/rss.erb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<% coder = HTMLEntities.new %>
32
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
43
<channel>
54
<title><%= Rails.application.name %><%= @title.present? ?
@@ -10,17 +9,17 @@
109

1110
<% @stories.each do |story| %>
1211
<item>
13-
<title><%= raw coder.encode(story.title, :decimal) %></title>
12+
<title><%= raw HtmlEncoder.encode(story.title) %></title>
1413
<link><%= story.url_or_comments_url %></link>
1514
<guid isPermaLink="false"><%= story.short_id_url %></guid>
1615
<author><%= story.user.username %>@users.<%= Rails.application.domain %> (<%= story.user.username %>)</author>
1716
<pubDate><%= story.created_at.rfc2822 %></pubDate>
1817
<comments><%= story.comments_url %></comments>
1918
<description>
20-
<%= raw coder.encode(story.markeddown_description, :decimal) %>
19+
<%= raw HtmlEncoder.encode(story.markeddown_description) %>
2120
<% if story.url.present? %>
22-
<%= raw coder.encode("<p>" +
23-
link_to("Comments", story.comments_url) + "</p>", :decimal) %>
21+
<%= raw HtmlEncoder.encode("<p>" +
22+
link_to("Comments", story.comments_url) + "</p>") %>
2423
<% end %>
2524
</description>
2625
<% story.taggings.each do |tagging| %>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# typed: false
2+
3+
require "cgi"
4+
5+
module HtmlEncoder
6+
HTML_ENTITIES = HTMLEntities.new
7+
8+
class << self
9+
def encode(string, type = :decimal)
10+
HTML_ENTITIES.encode(string, type)
11+
end
12+
13+
def decode(encoded_string)
14+
CGI.unescape_html(encoded_string)
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)