Skip to content

Avoid foreign grammar calls in Markdown parser - #1818

Open
skatkov wants to merge 2 commits into
ruby:masterfrom
skatkov:optimize-markdown-literals
Open

Avoid foreign grammar calls in Markdown parser#1818
skatkov wants to merge 2 commits into
ruby:masterfrom
skatkov:optimize-markdown-literals

Conversation

@skatkov

@skatkov skatkov commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

The Markdown parser delegates common character rules to a foreign kpeg grammar. Each match resets the foreign parser's input and scanner state, making these frequently invoked rules a significant allocation and CPU hotspot.

This inlines the literal rules in the main grammar to avoid resetting scanner state for every match. Because all these values are inlined, we can also loose literals.kpeg file (and all the mapping associated with it).

On a full Rails documentation build, median runtime decreased from 17.92s to 14.94s (16.6%), peak RSS decreased by 10.4%, and allocations decreased by 24.4%.

Metric Baseline Changed Improvement
Median runtime 17.92s 14.94s 16.6%
Median peak RSS 224,984 KB 201,536 KB 10.4%
Allocations 29,620,947 22,391,183 24.4%
CPU profile samples 17,245 14,477 16.1%
GC profile samples 3,504 2,765 21.1%

Command:
bundle exec rdoc --format=markdown --quiet --force-output \ '--exclude=(?:^|/)(?:app/assets|lib/templates)/|\.(?:json|rbs|xml|xsd)\z' .

Inline the literal rules in the main grammar to avoid resetting scanner
state for every match.
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

Documentation preview

View the preview

Commit: 599f5f0

@skatkov
skatkov marked this pull request as ready for review September 8, 2026 21:51
Copilot AI balanced review requested due to automatic review settings September 8, 2026 21:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The rules are inlined without semantic changes, and all references and supporting configuration are updated consistently.

Pull request overview

Inlines Markdown literal rules to eliminate foreign-parser state resets and improve parsing performance.

Changes:

  • Moves common literal rules into the primary Markdown grammar.
  • Removes the obsolete literals grammar and generated parser.
  • Updates generation, linting, and contributor documentation.
File summaries
File Description
Rakefile Removes the deleted grammar from parser generation.
lib/rdoc/markdown/literals.rb Deletes the generated foreign parser.
lib/rdoc/markdown/literals.kpeg Deletes the foreign grammar source.
lib/rdoc/markdown.rb Uses locally generated literal rules.
lib/rdoc/markdown.kpeg Defines literal rules directly.
CONTRIBUTING.md Updates parser documentation.
AGENTS.md Updates generated-file documentation.
.rubocop.yml Removes the deleted generated file exclusion.
Review details
  • Files reviewed: 8/8 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants