RDoc::Markdown#code takes superlinear time on long space/tab runs in inline code spans
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 62/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- ruby
- Bereich
- documentation, performance
Rechercherichtung
Read lib/rdoc/markdown.rb around RDoc::Markdown#code (line 908 on master) and inspect the existing Markdown tests in test/rdoc/rdoc_markdown_test.rb. Reproduce the slowdown with the supplied script, then compare behavior on long space and tab runs, including text with newlines. Done means trimming remains equivalent to the current method and parsing long spans no longer grows superlinearly.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Summary
RDoc::Markdown#code trims "even spaces" by repeatedly running an anchored
regex until it stops matching:
def code text
# trim even spaces
text = $2 while /\A( +|\t+)(.*)\1\z/ =~ text
# escape unescaped backslash at the end
backslash_at_end = "\\" if /(?<!\\)(?:\\\\)*\\\z/.match?(text)
"<code>#{text}#{backslash_at_end}</code>"
end
(on master today: lib/rdoc/markdown.rb:908; in rdoc 7.0.4, which ships with
Ruby 4.0.10: line 890)
The regex is anchored and requires the same leading and trailing run, so for
text that is mostly spaces/tabs it backtracks heavily, and the while loop
repeats O(n) times. A single inline code span with a long run of spaces
therefore costs superlinear (roughly quadratic-to-cubic) time: ~20 KB of
Markdown keeps a core busy for ~6 minutes. Memory stays constant, so this is a
CPU-only denial of service, but it is reachable anywhere rdoc is pointed at
Markdown that comes from a third party (docs generation over a fetched
README/CHANGELOG/comments).
Reproduction
require "rdoc"
require "rdoc/markdown"
n = (ARGV[0] || 1000).to_i
src = "`" + " " * n + "!`"
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
RDoc::Markdown.new.parse(src)
dt = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
puts "n=%d parse=%.1f ms" % [n, dt * 1000]
Ruby 4.0.10 (rdoc 7.0.4), x86_64-linux:
| n (spaces in span) | RDoc::Markdown#parse |
|---|---|
| 500 | 34 ms |
| 1 000 | 132 ms |
| 2 000 | 981 ms |
| 4 000 | 7 123 ms |
| 8 000 | 31 501 ms |
| 20 000 | ~358 000 ms |
Each doubling multiplies the time by ~6–8×, so the growth is between quadratic
and cubic. The same happens on master (8.1.0, 07859df): the same input takes
24.8 s at n=8 000, i.e. the issue is still present and the hot spot is the
same code method.
code in isolation (same inputs, no parser overhead), rdoc 7.0.4:
| n | one call of the trim loop |
|---|---|
| 1 000 | 65 ms |
| 2 000 | 413 ms |
| 4 000 | 2 973 ms |
Cause
For text = " " * n + "!" the regex must try every possible length of
( +), then for each of them let (.*) backtrack looking for a position where
\1 is followed by \z; the trailing ! guarantees failure, so the engine
walks a quadratic (in practice worse) search space. On each outer iteration the
captured run is trimmed by min(leading_run, trailing_run), and the loop keeps
going while leading and trailing runs differ, giving O(n) iterations.
Suggested fix
Trim at the byte level instead of with the regex, removing the same "even"
number of bytes per iteration (capped at half of the remaining size). Short-
circuit when the text contains "\n", because . never matches a newline, so
the original loop can never trim such text at all.
def code text
# trim even spaces
- text = $2 while /\A( +|\t+)(.*)\1\z/ =~ text
+ unless text.include?("\n")
+ s = 0
+ e = text.bytesize
+ while s < e
+ ch = text.getbyte(s)
+ break unless ch == 0x20 || ch == 0x09
+ lead = s
+ lead += 1 while lead < e && text.getbyte(lead) == ch
+ trail = e
+ trail -= 1 while trail > s && text.getbyte(trail - 1) == ch
+ run = [lead - s, e - trail, (e - s) / 2].min
+ break if run.zero?
+ s += run
+ e -= run
+ end
+ text = text.byteslice(s, e - s)
+ end
# escape unescaped backslash at the end
backslash_at_end = "\\" if /(?<!\\)(?:\\\\)*\\\z/.match?(text)
"<code>#{text}#{backslash_at_end}</code>"
Validation of the patch (against master, 07859df)
- Equivalence with the original
code: 500 000 random strings (length 0–16,
alphabet" ","\t","\n","a","b","!","é","😀"," ",
"\t\t","`"," \t") plus exhaustively all strings over
{" ", "\t", "\n", "x"}up to length 6 (5 461 strings) — 0 mismatches. - Existing tests pass:
test/rdoc/rdoc_markdown_test.rb(127 tests) and
test/rdoc/rdoc_markdown_test_test.rb(MarkdownTest 1.0.3 suite, 22 tests),
run asruby -Ilib -Itest ...— 100% passed. - End-to-end with the patch, same PoC: n=4 000 → 25 ms, 8 000 → 38 ms,
20 000 → 88 ms, 100 000 → 596 ms, 400 000 → 2 154 ms (linear).
Happy to send this as a PR if the approach looks good.
- Vorherrschende Sprache
- Ruby
- Sterne
- 931
- Forks
- 475
- Ø Merge
- 1 T. 15 Std.
- Gemergte PRs (30 T.)
- 10
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus ruby/rdoc
-
Page has duplicate id valuesEvtl. vergeben @sourya2704 hat das vor 3 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
Maintainer antworten meist innerhalb von 1 Tag
-
Heading element has empty id attribute valueEvtl. vergeben @sourya2704 hat das vor 3 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
Maintainer antworten meist innerhalb von 1 Tag
-
Spaces in image alt texts are not supportedEvtl. vergeben @singye3 hat das vor 8 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
RDoc 8.0.0 gem omits `doc/rdoc/example.rb`, which is referenced by the packaged markup documentationEvtl. vergeben @OskarEichler hat das vor 90 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
ruby/rdoc#1759 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
open-feature/ruby-sdk-contrib#340 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
zold-io/wts.zold.io#651 ·
Maintainer antworten meist innerhalb von 1 Tag
-
good first issue help wanted
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 87/100
erkamyaman/rust-anything#788 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 74/100
TheOdinProject/curriculum#31579 ·
Maintainer antworten meist innerhalb von 1 Tag