Skip to content

Commit

Permalink
Avoid continiously instantiating HTMLEntities
Browse files Browse the repository at this point in the history
Co-authored-by: Jean Boussier <[email protected]>
  • Loading branch information
k0kubun and byroot committed Sep 30, 2024
1 parent d2652b7 commit fca941b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion benchmarks/lobsters/app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def description_or_story_text(chars = 0)
s = s.to_s[0, chars].gsub(/ [^ ]*\z/, "")
end

HTMLEntities.new.decode(s.to_s)
HtmlEncoder.encode(s.to_s)
end

def domain_search_url
Expand Down
6 changes: 2 additions & 4 deletions benchmarks/lobsters/app/views/comments/index.rss.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<% coder = HTMLEntities.new %>
<rss version="2.0">
<channel>
<title><%= Rails.application.name %><%= @title.present? ?
Expand All @@ -9,14 +8,13 @@

<% @comments.each do |comment| %>
<item>
<title>on <%= raw coder.encode(comment.story.title, :decimal) %></title>
<title>on <%= raw HtmlEncoder.encode(comment.story.title) %></title>
<link><%= comment.url %></link>
<guid><%= comment.short_id_url %></guid>
<author><%= comment.user.username %>@users.<%= Rails.application.domain %> (<%= comment.user.username %>)</author>
<pubDate><%= comment.created_at.rfc2822 %></pubDate>
<comments><%= comment.url %></comments>
<description><%= raw coder.encode(comment.markeddown_comment,
:decimal) %></description>
<description><%= raw HtmlEncoder.encode(comment.markeddown_comment) %></description>
</item>
<% end %>
</channel>
Expand Down
9 changes: 4 additions & 5 deletions benchmarks/lobsters/app/views/home/rss.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<% coder = HTMLEntities.new %>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><%= Rails.application.name %><%= @title.present? ?
Expand All @@ -10,17 +9,17 @@

<% @stories.each do |story| %>
<item>
<title><%= raw coder.encode(story.title, :decimal) %></title>
<title><%= raw HtmlEncoder.encode(story.title) %></title>
<link><%= story.url_or_comments_url %></link>
<guid isPermaLink="false"><%= story.short_id_url %></guid>
<author><%= story.user.username %>@users.<%= Rails.application.domain %> (<%= story.user.username %>)</author>
<pubDate><%= story.created_at.rfc2822 %></pubDate>
<comments><%= story.comments_url %></comments>
<description>
<%= raw coder.encode(story.markeddown_description, :decimal) %>
<%= raw HtmlEncoder.encode(story.markeddown_description) %>
<% if story.url.present? %>
<%= raw coder.encode("<p>" +
link_to("Comments", story.comments_url) + "</p>", :decimal) %>
<%= raw HtmlEncoder.encode("<p>" +
link_to("Comments", story.comments_url) + "</p>") %>
<% end %>
</description>
<% story.taggings.each do |tagging| %>
Expand Down
17 changes: 17 additions & 0 deletions benchmarks/lobsters/extras/html_encoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# typed: false

require "cgi"

module HtmlEncoder
HTML_ENTITIES = HTMLEntities.new

class << self
def encode(string, type = :decimal)
HTML_ENTITIES.encode(string, type)
end

def decode(encoded_string)
CGI.unescape_html(encoded_string)
end
end
end

0 comments on commit fca941b

Please sign in to comment.