Repository: dotnet/runtime
File: src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs (C#)

Code before:
                        EmitStackPop();
                        Stloc(startingPos);
                        if (startingCapturePos is not null)
                        {
                            EmitStackPop();
                            Stloc(startingCapturePos);
                        }
                        EmitStackPop();
                    }
                    else
                    {
                        // We're not in a loop, so our locals already store the state we need.
                        // switch (currentBranch)
                        Ldloc(currentBranch);
                    }
                    Switch(labelMap);
                }

                // Successfully completed the alternate.
                MarkLabel(matchLabel);
                Debug.Assert(sliceStaticPos == 0);
            }

            // Tries to emit an alternation as a switch on the first character of each branch.
            // Returns true if the optimization was applied, false otherwise.
            bool TryEmitAlternationAsSwitch(RegexNode node, int childCount, bool isAtomic)
            {
                // Determine whether we can use switched branches.
                // We can't use switched branches if there's any possibility of backtracking into the alternation.
                bool canUseSwitchedBranches = isAtomic;
                if (!canUseSwitchedBranches)
                {
                    canUseSwitchedBranches = true;
                    for (int i = 0; i < childCount; i++)
                    {
                        if (analysis.MayBacktrack(node.Child(i)))
                        {
                            canUseSwitchedBranches = false;
                            break;
                        }
                    }
                }

                if (!canUseSwitchedBranches)
                {
                    return false;
                }

                // Detect whether every branch begins with one or more unique characters.
                const int SetCharsSize = 64; // arbitrary limit; we want it to be large enough to handle ignore-case of common sets, like hex, the latin alphabet, etc.
                Span<char> setChars = stackalloc char[SetCharsSize];

Review comment (refactor):
@copilot, I'd like to keep the source generator and the compiler as synchronized with each other as possible. Can you port back to the source generator any refactorings you added here (like the separate TryEmitAlternationAsSwitch method)?

Code after:
                        EmitStackPop();
                        Stloc(startingPos);
                        if (startingCapturePos is not null)
                        {
                            EmitStackPop();
                            Stloc(startingCapturePos);
                        }
                        EmitStackPop();
                    }
                    else
                    {
                        // We're not in a loop, so our locals already store the state we need.
                        // switch (currentBranch)
                        Ldloc(currentBranch);
                    }
                    Switch(labelMap);
                }

                // Successfully completed the alternate.
                MarkLabel(matchLabel);
                Debug.Assert(sliceStaticPos == 0);
            }

            // Tries to emit an alternation as a switch on the first character of each branch.
            // Returns true if the optimization was applied, false otherwise.
            bool TryEmitAlternationAsSwitch(RegexNode node, int childCount, bool isAtomic)
            {
                // We can't use switched branches if there's any possibility of backtracking into the alternation.
                if (!isAtomic)
                {
                    for (int i = 0; i < childCount; i++)
                    {
                        if (analysis.MayBacktrack(node.Child(i)))
                        {
                            return false;
                        }
                    }
                }

                // Detect whether every branch begins with one or more unique characters.
                if (!node.TryGetAlternationStartingChars(out HashSet<char>? seenChars))
                {
                    return false;
                }

                // Compute min/max to determine density for choosing IL switch vs comparisons
                int count = seenChars.Count;
                int minValue = int.MaxValue;
                int maxValue = int.MinValue;
                foreach (char c in seenChars)
                {