You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Store the compiled TRegExpProgram on the RegExp object instance so that exec, test, match, matchAll, search, replace, and split reuse it instead of recompiling on every call.
Why
ExecuteRegExp calls CompileRegExp on every match attempt. Additionally, CreateRegExpObject calls ValidateRegExpPatternNew which also compiles and discards the result. This means every RegExp construction compiles twice (once to validate, once on first exec), and every subsequent match recompiles from scratch.
For String.prototype.replace(/pattern/g, ...) on a large string or matchAll iterators, the pattern is recompiled for every match position. Compilation involves parsing the entire pattern, building the instruction array, and constructing character class tables — unnecessary work when the pattern hasn't changed.
Summary
Store the compiled
TRegExpProgramon the RegExp object instance so thatexec,test,match,matchAll,search,replace, andsplitreuse it instead of recompiling on every call.Why
ExecuteRegExpcallsCompileRegExpon every match attempt. Additionally,CreateRegExpObjectcallsValidateRegExpPatternNewwhich also compiles and discards the result. This means every RegExp construction compiles twice (once to validate, once on first exec), and every subsequent match recompiles from scratch.For
String.prototype.replace(/pattern/g, ...)on a large string ormatchAlliterators, the pattern is recompiled for every match position. Compilation involves parsing the entire pattern, building the instruction array, and constructing character class tables — unnecessary work when the pattern hasn't changed.Current behavior
Expected behavior
Scope notes
source/units/Goccia.RegExp.Engine.pas:146—CompileRegExpcall inExecuteRegExpsource/units/Goccia.RegExp.Compiler.pas:1528-1534—ValidateRegExpPatternNewcompiles and discardssource/units/Goccia.RegExp.Runtime.pas:134-135—CreateRegExpObjectcalls validateTGocciaObjectValue, or as a pointer field if a lighter mechanism is preferredValidateRegExpPatternNewcan be replaced by the construction-time compile — if compilation succeeds, the pattern is valid