Repository: dotnet/runtime
File: src/coreclr/vm/eepolicy.cpp (C++)

Code before:
                MethodDesc* pMD = m_frames[i];
                if (commonStartIndex != -1)
                {
                    // Some common frames were already found

                    int commonLength = commonStartIndex - largestCommonStartOffset;
                    if (m_frames[i - commonLength] != pMD)
                    {
                        // The frame being added is not part of the repeated sequence
                        int commonRepeat = (i - largestCommonStartOffset) / commonLength;
                        if (commonRepeat >= 2)
                        {
                            // A sequence repeated at least twice was found. It is the largest one that was found so far
                            largestCommonLength = commonLength;
                            largestCommonRepeat = commonRepeat;
                        }

                        commonStartIndex = -1;
                    }
                }

                if (commonStartIndex == -1)
                {
                    if ((i != largestCommonStartOffset) && (pMD == m_frames[largestCommonStartOffset]))
                    {
                        // We have found a frame with the same MethodDesc as the frame at the top of the stack,
                        // possibly a new repeated sequence is starting.
                        commonStartIndex = i;
                    }
                }
            }

            if (largestCommonRepeat != 0)
            {
                // A repeated sequence of frames was identified
                break;
            }
        }

        for (int i = 0; i < largestCommonStartOffset; i++)
        {
            PrintFrame(i, pWordAt);
        }

        if (largestCommonLength != 0)
        {
            SmallStackSString repeatStr;
            repeatStr.AppendPrintf("Repeated %d times:\n", largestCommonRepeat);

            PrintToStdErrW(repeatStr.GetUnicode());


Review comment (suggestion):
The comment "the frame at the top of the stack" is misleading. This should reference "the frame at the start of the repetition search" or "the frame at index largestCommonStartOffset" since the algorithm is now searching for repetitions starting from different offsets, not just from the top of the stack.
```suggestion
                        // We have found a frame with the same MethodDesc as the frame at the start of the repetition search (index largestCommonStartOffset),
```

Code after:
        for (largestCommonStartOffset = 0; largestCommonStartOffset < repetitionSearchLimit; largestCommonStartOffset++)
        {
            // Index of a stack frame where a possible repetition of frames starts
            int commonStartIndex = -1;
            largestCommonLength = 0;
            largestCommonRepeat = 0;

            for (int i = largestCommonStartOffset; i < m_frames.Count(); i++)
            {
                MethodDesc* pMD = m_frames[i];
                if (commonStartIndex != -1)
                {
                    // Some common frames were already found

                    int commonLength = commonStartIndex - largestCommonStartOffset;
                    if (m_frames[i - commonLength] != pMD)
                    {
                        // The frame being added is not part of the repeated sequence
                        int commonRepeat = (i - largestCommonStartOffset) / commonLength;
                        if (commonRepeat >= 2)
                        {
                            // A sequence repeated at least twice was found. It is the largest one that was found so far
                            largestCommonLength = commonLength;
                            largestCommonRepeat = commonRepeat;
                        }

                        commonStartIndex = -1;
                    }
                }

                if (commonStartIndex == -1)
                {
                    if ((i != largestCommonStartOffset) && (pMD == m_frames[largestCommonStartOffset]))
                    {
                        // We have found a frame with the same MethodDesc as the frame at the start of the repetition search (index largestCommonStartOffset),
                        // possibly a new repeated sequence is starting.
                        commonStartIndex = i;
                    }
                }
            }

            if (largestCommonRepeat != 0)
            {
                // A repeated sequence of frames was identified
                break;
            }
        }

        for (int i = 0; i < largestCommonStartOffset; i++)
        {
            PrintFrame(i, pWordAt);