Skip to content

perf(Core/SourceLength): Use Regex.Count - #881

Merged
knocte merged 1 commit into
fsprojects:masterfrom
Numpsy:regex_count
Aug 8, 2026
Merged

perf(Core/SourceLength): Use Regex.Count#881
knocte merged 1 commit into
fsprojects:masterfrom
Numpsy:regex_count

Conversation

@Numpsy

@Numpsy Numpsy commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

It allocates less than Regex.Matches.Count when the count is all that is needed from the results.

Before

Method Mean Error StdDev Gen0 Gen1 Gen2 Allocated
LintParsedFile 1.278 s 0.0045 s 0.0042 s 15000.0000 6000.0000 2000.0000 263.95 MB

After

Method Mean Error StdDev Gen0 Gen1 Gen2 Allocated
LintParsedFile 1.278 s 0.0077 s 0.0072 s 15000.0000 6000.0000 2000.0000 262.64 MB

It allocates less than Regex.Matches.Count when the
count is all that is needed from the results.

Before

| Method         | Mean    | Error    | StdDev   | Gen0       | Gen1      | Gen2      | Allocated |
|--------------- |--------:|---------:|---------:|-----------:|----------:|----------:|----------:|
| LintParsedFile | 1.278 s | 0.0045 s | 0.0042 s | 15000.0000 | 6000.0000 | 2000.0000 | 263.95 MB |

After

| Method         | Mean    | Error    | StdDev   | Gen0       | Gen1      | Gen2      | Allocated |
|--------------- |--------:|---------:|---------:|-----------:|----------:|----------:|----------:|
| LintParsedFile | 1.278 s | 0.0077 s | 0.0072 s | 15000.0000 | 6000.0000 | 2000.0000 | 262.64 MB |
@Numpsy

Numpsy commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

There is actually a Roslyn analyzer that makes this suggestion - https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1875

Could that be something that could be checked by FSharpLint, similar to #707 ?

@knocte

knocte commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Could that be something that could be checked by FSharpLint

Yes, sure, you're welcome to create such rule.

@knocte
knocte merged commit 2c7b381 into fsprojects:master Aug 8, 2026
8 checks passed
@Numpsy
Numpsy deleted the regex_count branch August 8, 2026 12:25
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