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

Code before:
                            return true;
                        }

                        pos = textSpan.Length;
                        return false;
                    }

                // Nothing special to look for.  Just return true indicating this is a valid position to try to match.

                default:
                    Debug.Assert(FindMode == FindNextStartingPositionMode.NoSearch, $"Unexpected FindMode {FindMode}");
                    return true;
            }
        }
#endif

        /// <summary>
        /// Determines whether the characters in the best <see cref="FixedDistanceSet"/> are frequent enough in typical text
        /// that IndexOfAny with those characters would be a poor filter. When the best set's characters are common,
        /// multi-string search via SearchValues is preferred because IndexOfAny would match too many
        /// positions. When the characters are rare, IndexOfAny is an excellent filter and is preferred.
        /// </summary>
        private static bool HasHighFrequencyChars(FixedDistanceSet set)
        {
            // Negated sets typically match most characters (e.g. [^a]) making them poor quality indicators,
            // and sets without extracted chars can't be frequency-analyzed.
            // Single-char sets use IndexOf which has much higher throughput than multi-string search,
            // so we never consider them "high frequency" regardless of which character it is.
            if (set.Negated || set.Chars is not { Length: > 1 } chars)
            {
                return false;
            }

            ReadOnlySpan<float> frequency = RegexPrefixAnalyzer.Frequency;
            float totalFrequency = 0;

            foreach (char c in chars)
            {
                if (c >= frequency.Length)
                {
                    return false;
                }

                totalFrequency += frequency[c];
            }

            // If the average frequency of the set's chars exceeds this threshold, the characters
            // are common enough that IndexOfAny would match many positions, making SearchValues<string>
            // the better choice. Common characters like lowercase letters have frequencies above this
            // threshold, while less frequent characters like uppercase letters fall below it, so
            // IndexOfAny with rare characters is already an effective filter and is preferred.

Review comment (suggestion):
I'm confused, then. Based on this set, we're deciding whether it's better to search for this set or whether it's better to search for multiple strings. If the set would match most characters, doesn't that mean we it has high frequency chars / we want to switch to multi-string search? Yet we're returning false.

Code after:
                            return true;
                        }

                        pos = textSpan.Length;
                        return false;
                    }

                // Nothing special to look for.  Just return true indicating this is a valid position to try to match.

                default:
                    Debug.Assert(FindMode == FindNextStartingPositionMode.NoSearch, $"Unexpected FindMode {FindMode}");
                    return true;
            }
        }
#endif

        /// <summary>
        /// Determines whether the characters in the best <see cref="FixedDistanceSet"/> are frequent enough in typical text
        /// that IndexOfAny with those characters would be a poor filter. When the best set's characters are common,
        /// multi-string search via SearchValues is preferred because IndexOfAny would match too many
        /// positions. When the characters are rare, IndexOfAny is an excellent filter and is preferred.
        /// </summary>
        private static bool HasHighFrequencyChars(FixedDistanceSet set)
        {
            // Negated sets match most characters (e.g. [^a]), so they are inherently high frequency.
            if (set.Negated)
            {
                return true;
            }

            // Sets without extracted chars can't be frequency-analyzed.
            // Single-char sets use IndexOf which has much higher throughput than multi-string search,
            // so we never consider them "high frequency" regardless of which character it is.
            if (set.Chars is not { Length: > 1 } chars)
            {
                return false;
            }

            ReadOnlySpan<float> frequency = RegexPrefixAnalyzer.Frequency;
            float totalFrequency = 0;

            foreach (char c in chars)
            {
                if (c >= frequency.Length)
                {
                    return false;
                }

                totalFrequency += frequency[c];
            }