From cbb8e369fa21aa2e06853899f601447ca0d2f0db Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 17 Sep 2026 22:04:46 +0200 Subject: [PATCH] Cache regexp in `Markup::ToHtml` Especially the first one is not good for performance. In ruby/prism, docs generation goes from 7s to 6.75s for me, which is about a 4% improvement (including bundler startup etc, so for pure rdoc timing it should be even better) --- lib/rdoc/markup/to_html.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb index 474a74ccc3..cbdc3d0188 100644 --- a/lib/rdoc/markup/to_html.rb +++ b/lib/rdoc/markup/to_html.rb @@ -75,6 +75,7 @@ class ToHtml < Markup::Formatter '``' => :open_dquote, "''" => :close_dquote, } + HTML_CHARACTERS_REGEXP = Regexp.union(HTML_CHARACTER_ALIASES.keys) # Transcodes +character+ to +encoding+ with a +fallback+ character. @@ -155,13 +156,13 @@ def initialize(pipe: false, output_decoration: true) def init_regexp_handlings # external links - @markup.add_regexp_handling(/(?:link:|https?:|mailto:|ftp:|irc:|www\.)#{URL_CHARACTERS_REGEXP_STR}+\w/, + @markup.add_regexp_handling(/(?:link:|https?:|mailto:|ftp:|irc:|www\.)#{URL_CHARACTERS_REGEXP_STR}+\w/o, :HYPERLINK) # suppress crossref: \#method \::method \ClassName \method_with_underscores @markup.add_regexp_handling(/\\(?:[#:A-Z]|[a-z]+_[a-z0-9])/, :SUPPRESSED_CROSSREF) - @markup.add_regexp_handling(Regexp.union(HTML_CHARACTER_ALIASES.keys), :HTML_CHARACTERS) + @markup.add_regexp_handling(HTML_CHARACTERS_REGEXP, :HTML_CHARACTERS) @markup.add_regexp_handling(/\b['"`]/, :QUOTE_AFTER_WORD) @markup.add_regexp_handling(/\B['"`]/, :QUOTE_NOT_AFTER_WORD)