From 72aaf619e124b0107bfd76017503b534a5aa7da8 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Tue, 3 Sep 2024 12:02:34 +0200 Subject: [PATCH] Fix performance issue for Mustermann::AST::Translator#escape (#142) Re-use URI parser object for performance improvement. --- mustermann/lib/mustermann/ast/translator.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mustermann/lib/mustermann/ast/translator.rb b/mustermann/lib/mustermann/ast/translator.rb index 33b8a7d..cb5ad95 100644 --- a/mustermann/lib/mustermann/ast/translator.rb +++ b/mustermann/lib/mustermann/ast/translator.rb @@ -11,6 +11,9 @@ module AST # @abstract # @!visibility private class Translator + + URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::RFC2396_Parser.new + # Encapsulates a single node translation # @!visibility private class NodeTranslator < DelegateClass(Node) @@ -118,7 +121,7 @@ def decorator_for(node) # @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