Skip to content

Core: Avoid unused regex engine in trimmed WASM apps - #14208

Merged
danielchalmers merged 4 commits into
devfrom
trim-regex-engine
Nov 13, 2026
Merged

danielchalmers merged 4 commits into
devfrom
trim-regex-engine

Conversation

@danielchalmers

@danielchalmers danielchalmers commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

A trimmed Blazor WebAssembly app with a MudTextField, MudSelect or MudDataGrid downloads the whole regex parser and interpreter, even when it never uses a regular expression.
Two places kept it reachable:

  • PatternMask ran Regex.IsMatch on every typed character, and every text field can take a mask.
  • Every component registered its keys through KeyMapBuilder's string methods, which also accept "/pattern/" keys.

Now MaskChar.Letter, Digit and LetterOrDigit test the character directly, and MudBlazor's components register keys through internal methods that never build a Regex.
No public API changes.
RegexMask, BlockMask, MultiMask, MudHighlighter, a custom MaskChar and "/pattern/" keys still use the engine, as before.

Download size

Page net10.0 net11.0
Providers only 0 0
MudTextField -88.0 KB -91.3 KB
MudSelect -88.0 KB -92.1 KB
MudButton, MudTextField, MudSelect -86.7 KB -89.3 KB
MudDataGrid -85.1 KB -87.6 KB

Release publish of the blazorwasm template with default trimming, _framework total, brotli.
System.Text.RegularExpressions.wasm drops from about 240 KB to 22.8 KB raw; the rest serves source-generated regexes.
Pages with DateMask, the date, time and color pickers, MudAutocomplete, MudMenu, MudTabs, MudTable and MudNumericField end at the same 22.8 KB.

Behavior

  • Tests check the new matching against the old patterns for every UTF-16 code unit.
  • Headless Chrome, dev against this branch, 934 real key steps on the final commit and 1,401 on earlier ones, across masks, select search, pickers, dialogs and toggles: no differences.
  • Key registrations are also lighter: a list item's six keys allocate 1,504 bytes, against 1,720 on dev (64-bit).
  • Only keys a browser never sends differ: a null key passed to DispatchAsync by hand no longer throws in a dialog or select, and a key containing a line feed no longer matches.

Built-in mask characters and MudBlazor's own key bindings no longer go through Regex.
A trimmed WebAssembly app with a text field, select or data grid drops about 86-88 KB (brotli) of System.Text.RegularExpressions.

- MaskChar.Letter, Digit and LetterOrDigit test the character with char.IsLetter, char.IsDigit and char.IsLetterOrDigit.
  Custom mask characters from the public constructor still match through their expression.
- Components register keys through new internal literal and predicate methods on KeyMapBuilder.
  The public string methods keep their "/pattern/" support.
- The "/./" and "/^[^ ]$/" bindings in the dialog and select become predicates that match the same keys.
@danielchalmers danielchalmers added the performance Related to time/memory/CPU/allocation performance characteristics label Nov 13, 2026

@mudbot mudbot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Moving the built-in mask characters and MudBlazor's own key registrations off the regex engine keeps behavior the same, and all string-based key registrations in the library have been converted. The new tests cover equivalence, case sensitivity and first-match ordering. I found nothing that needs fixing before merge.

@danielchalmers danielchalmers changed the title Core: Stop shipping regex engine to apps that never use it Core: Cut 88 KB from trimmed WebAssembly apps that don't use regex Nov 13, 2026
@danielchalmers danielchalmers changed the title Core: Cut 88 KB from trimmed WebAssembly apps that don't use regex Core: Stop trimmed WebAssembly apps downloading unused regex engine Nov 13, 2026
The literal registrations went through a predicate command, which cost a closure and two delegates per key.
Every checkbox, switch, radio, chip and list item registers several keys, so that added up per instance.
A dedicated command holds the key or key set and the action directly.
@danielchalmers danielchalmers changed the title Core: Stop trimmed WebAssembly apps downloading unused regex engine Core: Avoid downloading unused regex engine in trimmed WebAssembly apps Nov 13, 2026
@danielchalmers danielchalmers changed the title Core: Avoid downloading unused regex engine in trimmed WebAssembly apps Core: Avoid unused regex engine in trimmed WASM apps Nov 13, 2026
@danielchalmers
danielchalmers merged commit 72fc2de into dev Nov 13, 2026
13 checks passed
@danielchalmers
danielchalmers deleted the trim-regex-engine branch November 13, 2026 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Related to time/memory/CPU/allocation performance characteristics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant