I have the following regex:
var regex = new Regex(
@"^ActiveMQ[\d\.-]*$",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
It runs over ~1000 strings (IsMatch
call). In .NET Core 2.0 it takes around 10ms
. After migrating to .NET Core 2.1 it takes over 2 seconds
on the same data.
Any idea what's going on? Any behavior changes in 2.1?
======================
Update: BenchmarkDotNet
Reproducible 3x drop (just run, change netcoreapp2.1
to netcoreapp2.0
in csproj
file, run again).
https://github.com/ptupitsyn/netcore2.1-regex-perf/tree/master/src
GetPackageInfos2
reduces the perf drop to only 25%
, but it is still there. Changing this in real-world code is not trivial and I would like to avoid this kind of refactoring.Update 2
Removing RegexOptions.Compiled
solves the problem!
RegexOptions.Compiled
is not implemented in .NET Core 2.0, but is implemented in .NET Core 2.1.
Compilation involves initial overhead, and for certain usage patterns this overhead outweighs the gains of compiled regex.
My case is somewhat complex, and it seems like there might be a bug in .NET, because even with a proper benchmark (with warm-up), Compiled
mode is slower. See details in Corefx issue: https://github.com/dotnet/corefx/issues/30131
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With