<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/129808>129808</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Increased memory consumption in ParentMapContext after clang-19
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          michael-jabbour-sonarsource
      </td>
    </tr>
</table>

<pre>
    The memory increase can be observed when enabling any check that uses `ASTContext::getParents` in `clang-tidy`. By plotting the memory consumption when analyzing a sample file, I got the following chart which shows around 10x increase in memory consumption in clang-tidy-19 compared to clang-tidy-18 when the number of elements in the array is large enough:

![Image](https://github.com/user-attachments/assets/ef531e57-289a-4b27-a04a-f63d7bbd7ae1)

Here is a script that generates the above plot:

```python
import matplotlib.pyplot as plt
import subprocess

def generate_cpp_file(file_name, num_elements):
  elements = [100] * num_elements
  elements_str = ', '.join(map(hex, elements))
  with open(file_name, 'w') as f:
    f.write(f"""
            const char large_array[] = {{
              {elements_str}
            }};
 """)


def measure_memory_consumption_with(clang_tidy_bin, num_elements):
  file_name = f'file_{num_elements}.cpp'
 generate_cpp_file(file_name, num_elements)
  process = subprocess.run(['/usr/bin/time', '-f', '%M', clang_tidy_bin, '-checks=readability-magic-numbers', file_name, '--', '-std=c++17'], check=True, capture_output=True)
  memory_kb = int(process.stderr)
  return memory_kb // 1024


def plot_memory_consumption():
  num_elements = [10 ** 1, 10 ** 2, 10 ** 3, 10 ** 4, 10 ** 5, 10 ** 6, 3 * 10 ** 6, 5 * 10 ** 6, 8 * 10 ** 6]

  memory_consumption_17 = [measure_memory_consumption_with('clang-tidy-17', n) for n in num_elements]
 memory_consumption_18 = [measure_memory_consumption_with('clang-tidy-18', n) for n in num_elements]
  memory_consumption_19 = [measure_memory_consumption_with('clang-tidy-19', n) for n in num_elements]
 plt.plot(num_elements, memory_consumption_17, label='clang-tidy-17')
 plt.plot(num_elements, memory_consumption_18, label='clang-tidy-18')
 plt.plot(num_elements, memory_consumption_19, label='clang-tidy-19')
 plt.xlabel('Number of elements')
  plt.ylabel('Memory consumption (MB)')
 plt.legend()
  plt.show()


if __name__ == '__main__':
 plot_memory_consumption()
```
I have installed `clang-tidy` binaries in this test from apt.llvm.org on an Ubuntu machine.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVlFvqzoS_jXOy4gITAjwkIemUbR96GqlPfuMBhiCzwEb2aZp9tdf2ZCStD295zayFGyP55uZb8YeNEacJNGOJXuWHFY42lbpXS-qFqkLfmJZqlEHRknURo26olWp6svuR0vQU6_0BYSsNKEhqFBCSaBKQ_qFaji3JIEklp2QJ0B5gaql6hfYFi2Mhgywbfjw3x-PSlp6tSx-YPHDiex_UJO0hm1DENLJVB3KU2BFfWHbcA37CwydstZptYsdlZJm7AcrlJygUWJ3-b_HBoP90BE0oiPGH-EJTsr6w43qOnV2QlWL2sK5FVULplVnA6jVKGuIwtfFSSE_wxMSFiODKIdK9QNqqsGqu51sMs0hy7EvSYNqgDrqncdOjdtBrfECwkCH-kRAUo2n1oUn9INHLNk_9XgilhwYz1prB-O2-ZHx40nYdizXleoZP46GdIDWYtV6BMaPaAz5D2qSOKIkDXiWY7ApeRpguMGg2cZ1WpZ1ihQxnk-g_yJNziIEU2kx2InEE0nSaMlMZpfqhTw1i63bcBrDxbZKsvBB9IPSFnq0TrAT5Xq4uC9AA0NnFwkzloNWFRkzqaqpecMrqmEoJi4z91dI7D2vcuyLazSd7d4OWALM4gOwZB-FIUsOwPjD_Ylb2cJYPcnz1KlmPF3_VEIynvU4uLDTq1u_hfPRAjgL24IaSL43j_H07NXlzt3mah5Asz5rYb07jPN5TFvXn0s365N0SovCZ8lUtpOd6d6N-1Pglm9dYunhnYhbSQ8s9kcX9Cvzc-h7QjNqKqbkL26Sv3DuMp75NC9cmhelC9Nv2XgLiTe7YTz1Kyzd3x1ID-tqGFy4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