Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvement for Mustermann::AST::Translator#escape #141

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mustermann/bench/uri_parser_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "objspace"
require "uri"
require_relative "../lib/mustermann/ast/translator"

translator = Mustermann::AST::Translator.new
translator.escape("foo")

h1 = ObjectSpace.each_object.inject(Hash.new 0) { |h, o| h[o.class] += 1; h }

100.times do
translator.escape("foo")
end

h2 = ObjectSpace.each_object.inject(Hash.new 0) { |h, o| h[o.class] += 1; h }

raise if (h2[URI::RFC2396_Parser] - h1[URI::RFC2396_Parser] != 0)
12 changes: 11 additions & 1 deletion mustermann/lib/mustermann/ast/translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,19 @@ def decorator_for(node)
result
end

# @return [URI::RFC2396_Parser] URI::RFC2396 parser
# @!visibility private
def uri_parser
if defined?(URI::RFC2396_PARSER)
URI::RFC2396_PARSER
else
@uri_parser ||= URI::RFC2396_Parser.new
end
end
Comment on lines +121 to +127
Copy link
Member

@dentarg dentarg Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI require 100% coverage so we need to cover this branching in the specs

Copy link
Member

@dentarg dentarg Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is possible in some resonable way? I don't know. If not, can we opt-out from the coverage requirement just for this code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dentarg when defined as a constant, you get 100% coverage. You can see my PR.
#142

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, I don't know where URI::RFC2396_PARSER comes from :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh got it. It's in the next version of Ruby.


# @return [String] escaped character
# @!visibility private
def escape(char, parser: URI::RFC2396_Parser.new, escape: URI::RFC2396_Parser.new.regexp[:UNSAFE], also_escape: nil)
def escape(char, parser: uri_parser, escape: uri_parser.regexp[:UNSAFE], also_escape: nil)
escape = Regexp.union(also_escape, escape) if also_escape
char.to_s =~ escape ? parser.escape(char, Regexp.union(*escape)) : char
end
Expand Down
Loading