From b3190fcfe5cbdc3cc064da3c19af4b2b6057b268 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Wed, 11 Nov 2026 19:11:23 +0200 Subject: [PATCH] perf(markdown): fast-path plain Hebrew text and avoid per-character callbacks Otzaria/tools#37 --- pkgs/markdown/CHANGELOG.md | 2 + pkgs/markdown/lib/src/inline_parser.dart | 52 ++++++++++++++++++------ pkgs/markdown/test/regression_test.dart | 48 ++++++++++++++++++++++ 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md index eed8ebab2a..b8f20cacc9 100644 --- a/pkgs/markdown/CHANGELOG.md +++ b/pkgs/markdown/CHANGELOG.md @@ -24,6 +24,8 @@ * Sort footnote definitions by their original case-insensitive labels rather than URI-encoded HTML IDs, preserving first-reference order for Unicode and escaped labels. * Preserve existing percent-encoded sequences in extended autolink destinations instead of double-encoding their percent signs; still escape invalid percent sequences. * Flatten nested AST element text iteratively into one buffer instead of repeatedly concatenating intermediate strings; preserve specialized text getters on subclasses. +* Accelerate parsing of ordinary Hebrew text (including nikud and cantillation) without custom inline syntaxes, and avoid a predicate callback on each inline parsing step. +* Accelerate ordinary Hebrew parsing (including nikud and cantillation) for built-in CommonMark and GitHub extension sets while preserving custom inline syntax behavior; avoid a predicate callback on each inline parsing step. * Skip the regular expression in `EmojiSyntax`, `AutolinkSyntax`, `CodeSyntax`, and `ColorSwatchSyntax` when the character at the current position cannot start a match. This makes inline parsing faster, mostly on diff --git a/pkgs/markdown/lib/src/inline_parser.dart b/pkgs/markdown/lib/src/inline_parser.dart index e67339b195..49609e80d3 100644 --- a/pkgs/markdown/lib/src/inline_parser.dart +++ b/pkgs/markdown/lib/src/inline_parser.dart @@ -7,6 +7,7 @@ import 'dart:collection'; import 'ast.dart'; import 'charcode.dart'; import 'document.dart'; +import 'extension_set.dart'; import 'inline_syntaxes/autolink_syntax.dart'; import 'inline_syntaxes/code_syntax.dart'; import 'inline_syntaxes/decode_html_syntax.dart'; @@ -87,7 +88,12 @@ class InlineParser { // retries from the next position, giving quadratic behavior on long // unbroken runs. Allowing the run to end at `$` makes it match in one step. syntaxes.add( - _PlainTextSyntax(allowSpaces: !document.hasCustomInlineSyntaxes), + _PlainTextSyntax( + allowSpaces: !document.hasCustomInlineSyntaxes, + // The built-in syntaxes never start at a Hebrew character, but a + // custom one might, so it must still be tried at each of them. + includeHebrew: document.inlineSyntaxes.every(_isBuiltInExtension), + ), ); if (document.withDefaultInlineSyntaxes) { @@ -112,6 +118,11 @@ class InlineParser { } } + static bool _isBuiltInExtension(InlineSyntax syntax) => + ExtensionSet.gitHubWeb.inlineSyntaxes.contains(syntax) || + ExtensionSet.gitHubFlavored.inlineSyntaxes.contains(syntax) || + ExtensionSet.commonMark.inlineSyntaxes.contains(syntax); + List parse() { while (!isDone) { // A right bracket (']') is special. Hitting this character triggers the @@ -123,11 +134,16 @@ class InlineParser { continue; } - // See if the current text matches any defined Markdown syntax. - if (syntaxes.any((syntax) => syntax.tryMatch(this))) continue; - - // If we got here, it's just text. - advanceBy(1); + // See if the current text matches any defined Markdown syntax. Avoid + // allocating a predicate closure for every ordinary text character. + var matched = false; + for (final syntax in syntaxes) { + if (syntax.tryMatch(this)) { + matched = true; + break; + } + } + if (!matched) advanceBy(1); } // Write any trailing text content to a Text node. @@ -452,15 +468,26 @@ class InlineParser { } class _PlainTextSyntax extends TextSyntax { + static const _hebrewWithSpaces = + r'[ \tA-Za-z0-9\u0590-\u05FF]*[A-Za-z0-9\u0590-\u05FF](?=\s|$)'; + static const _hebrewWithoutSpaces = r'[A-Za-z0-9\u0590-\u05FF]+(?=\s|$)'; + static const _asciiWithSpaces = r'[ \tA-Za-z0-9]*[A-Za-z0-9](?=\s|$)'; + static const _asciiWithoutSpaces = r'[A-Za-z0-9]+(?=\s|$)'; + + static String _plainTextPattern(bool allowSpaces, bool includeHebrew) { + if (includeHebrew) { + return allowSpaces ? _hebrewWithSpaces : _hebrewWithoutSpaces; + } + return allowSpaces ? _asciiWithSpaces : _asciiWithoutSpaces; + } + final bool _allowSpaces; + final bool _includeHebrew; - _PlainTextSyntax({required bool allowSpaces}) + _PlainTextSyntax({required bool allowSpaces, required bool includeHebrew}) : _allowSpaces = allowSpaces, - super( - allowSpaces - ? r'[ \tA-Za-z0-9]*[A-Za-z0-9](?=\s|$)' - : r'[A-Za-z0-9]+(?=\s|$)', - ); + _includeHebrew = includeHebrew, + super(_plainTextPattern(allowSpaces, includeHebrew)); @override bool tryMatch(InlineParser parser, [int? startMatchPos]) { @@ -472,6 +499,7 @@ class _PlainTextSyntax extends TextSyntax { if (pos > parser.start) { final prev = parser.charAt(pos - 1); if (_isAlphaNum(prev) || + (_includeHebrew && prev >= 0x0590 && prev <= 0x05FF) || (_allowSpaces && (prev == $space || prev == $tab))) { return false; } diff --git a/pkgs/markdown/test/regression_test.dart b/pkgs/markdown/test/regression_test.dart index 6c56486a9b..ca4483ad21 100644 --- a/pkgs/markdown/test/regression_test.dart +++ b/pkgs/markdown/test/regression_test.dart @@ -399,6 +399,44 @@ a