diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.Cache.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.Cache.cs index c8759484567728..35dddc889d85f6 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.Cache.cs +++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.Cache.cs @@ -248,9 +248,12 @@ private static void Add(Key key, Regex regex) } } - // Remove the key found to have the smallest access stamp. + // Remove the key found to have the smallest access stamp. List ordering isn't important, so rather than + // just removing the element at minListIndex, which would result in an O(N) shift down, we copy the last + // element to minListIndex, and then remove the last. (If minListIndex is the last, this is a no-op.) s_cacheDictionary.TryRemove(s_cacheList[minListIndex].Key, out _); - s_cacheList.RemoveAt(minListIndex); + s_cacheList[minListIndex] = s_cacheList[^1]; + s_cacheList.RemoveAt(s_cacheList.Count - 1); } // Finally add the regex.