Skip to content

RegexOptions.Compiled impacts Android startup #71007

@jonathanpeppers

Description

@jonathanpeppers

Description

Reviewing a customer issue, we noticed usage of RegexOptions.Compiled impacts startup quite a bit:

image

This was coming from the AppCenter SDK in the customer's app:

https://github.com/microsoft/appcenter-sdk-dotnet/blob/b19ec99e16e554eb1382342b2852b16f7d8f0084/SDK/AppCenter/Microsoft.AppCenter.Shared/AppCenter.cs#L26

This shows up as a regression from Xamarin.Android -> .NET 6, because RegexOptions.Compiled was not implemented at all in mono/mono.

The question here, is how do we get the previous behavior from mono/mono? Is there a way to toggle RuntimeFeature.IsDynamicCodeCompiled?

if (RuntimeFeature.IsDynamicCodeCompiled && (options & RegexOptions.Compiled) != 0)

If there is a way to toggle RuntimeFeature.IsDynamicCodeCompiled, should we consider turning on this flag by default on Android? I've found System.Reflection.Emit to be very impactful to startup.

Reproduction Steps

  1. dotnet new android
  2. Open MainActivity.cs and put _ = new Regex(@"([^;=]+)=([^;]+)(?:;\s*)?", RegexOptions.Compiled); in the OnCreate() method.
  3. Build/run with dotnet build -c Release -t:Run

Expected behavior

RegexOptions.Compiled doesn't have a significant impact to Android startup. (Or we have a feature flag to turn it off)

Actual behavior

RegexOptions.Compiled has a significant impact to Android startup.

Regression?

From a customer's perspective they might say this is startup performance regression from Xamarin.Android to .NET 6.

Known Workarounds

Don't use RegexOptions.Compiled, use Normal.

Configuration

I tested this with the .NET 6.0.400-preview.22301.10 SDK, and the SR 1 version of .NET MAUI.

Other information

dotnet trace output: regexoptions.compiled.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done & Blogged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions