Skip to content

Commit

Permalink
Less RegEx construction on recursive method (#16351)
Browse files Browse the repository at this point in the history
* Less  RegEx construction on recursive method

* mandatory Fantomas commit

* moved regexes to a static value

* Expected growth in size exactly 2x 256
  • Loading branch information
Thorium authored Dec 5, 2023
1 parent d4e3b26 commit 1c4245c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions src/Compiler/Utilities/sformat.fs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,13 @@ module Display =
&& (ty.GetGenericTypeDefinition() = typedefof<Map<_, _>>
|| ty.GetGenericTypeDefinition() = typedefof<Set<_>>)

let messageRegexLookup =
@"^(?<pre>.*?)(?<!\\){(?<prop>.*?)(?<!\\)}(?<post>.*)$"
|> System.Text.RegularExpressions.Regex

let illFormedBracketPatternLookup =
@"(?<!\\){|(?<!\\)}" |> System.Text.RegularExpressions.Regex

// showMode = ShowTopLevelBinding on the outermost expression when called from fsi.exe,
// This allows certain outputs, e.g. objects that would print as <seq> to be suppressed, etc. See 4343.
// Calls to layout proper sub-objects should pass showMode = ShowAll.
Expand Down Expand Up @@ -1054,8 +1061,6 @@ module Display =
if isNull txt || txt.Length <= 1 then
None
else
let messageRegexPattern = @"^(?<pre>.*?)(?<!\\){(?<prop>.*?)(?<!\\)}(?<post>.*)$"
let illFormedBracketPattern = @"(?<!\\){|(?<!\\)}"

let rec buildObjMessageL (txt: string) (layouts: Layout list) =

Expand All @@ -1066,12 +1071,11 @@ module Display =
// 1) Everything up to the first opening bracket not preceded by a "\", lazily
// 2) Everything between that opening bracket and a closing bracket not preceded by a "\", lazily
// 3) Everything after that closing bracket
let m = System.Text.RegularExpressions.Regex.Match(txt, messageRegexPattern)
let m = messageRegexLookup.Match txt

if not m.Success then
// there isn't a match on the regex looking for a property, so now let's make sure we don't have an ill-formed format string (i.e. mismatched/stray brackets)
let illFormedMatch =
System.Text.RegularExpressions.Regex.IsMatch(txt, illFormedBracketPattern)
let illFormedMatch = illFormedBracketPatternLookup.IsMatch txt

if illFormedMatch then
None // there are mismatched brackets, bail out
Expand Down Expand Up @@ -1108,8 +1112,8 @@ module Display =

countNodes 0 // 0 means we do not count the preText and postText

let postTextMatch =
System.Text.RegularExpressions.Regex.Match(postText, messageRegexPattern)
let postTextMatch = messageRegexLookup.Match postText

// the postText for this node will be everything up to the next occurrence of an opening brace, if one exists
let currentPostText =
match postTextMatch.Success with
Expand All @@ -1129,10 +1133,7 @@ module Display =

// look for stray brackets in the text before the next opening bracket
let strayClosingMatch =
System.Text.RegularExpressions.Regex.IsMatch(
postTextMatch.Groups["pre"].Value,
illFormedBracketPattern
)
illFormedBracketPatternLookup.IsMatch postTextMatch.Groups["pre"].Value

if strayClosingMatch then
None
Expand All @@ -1143,8 +1144,7 @@ module Display =

| remaingPropertyText ->
// make sure we don't have any stray brackets
let strayClosingMatch =
System.Text.RegularExpressions.Regex.IsMatch(remaingPropertyText, illFormedBracketPattern)
let strayClosingMatch = illFormedBracketPatternLookup.IsMatch remaingPropertyText

if strayClosingMatch then
None
Expand Down
2 changes: 1 addition & 1 deletion tests/AheadOfTime/Trimming/check.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ function CheckTrim($root, $tfm, $outputfile, $expected_len) {
CheckTrim -root "SelfContained_Trimming_Test" -tfm "net8.0" -outputfile "FSharp.Core.dll" -expected_len 287232

# Check net7.0 trimmed assemblies
CheckTrim -root "StaticLinkedFSharpCore_Trimming_Test" -tfm "net8.0" -outputfile "StaticLinkedFSharpCore_Trimming_Test.dll" -expected_len 8820736
CheckTrim -root "StaticLinkedFSharpCore_Trimming_Test" -tfm "net8.0" -outputfile "StaticLinkedFSharpCore_Trimming_Test.dll" -expected_len 8821248

0 comments on commit 1c4245c

Please sign in to comment.