optimization: do not (re)compute the whole file text on each PSI change - will lead to quadratic nightmare in case of many small changes (part of IJPL-199461 Recursive elements processing causes SOE)

GitOrigin-RevId: 183d62bf30680c6ce02f13abda335729f8d5741f
This commit is contained in:
Alexey Kudravtsev
2025-08-01 17:39:45 +00:00
committed by intellij-monorepo-bot
parent c4531287fe
commit 98942f9977
@@ -0,0 +1,24 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.psi.impl;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElementFactory;
import com.intellij.psi.PsiField;
import com.intellij.testFramework.LightJavaCodeInsightTestCase;
public class PsiModificationPerformanceTest extends LightJavaCodeInsightTestCase {
public void testManySmallPSIChangesDoNotCauseQuadraticRecomputationsOfWholeFileText() {
int N = 100_000;
String text = " int field;\n".repeat(N);
PsiClass aClass = PsiElementFactory.getInstance(getProject()).createClassFromText(text, null);
assertFalse(aClass.isPhysical());
PsiField firstField = aClass.getFields()[0];
// would pass only if each PSI change does not cause recomputation of the whole file text in com.intellij.psi.impl.source.PsiJavaFileBaseImpl.getLanguageLevelInner
for (int i = 0; i < N; i++) {
firstField.setName("f" + i);
}
assertFalse(aClass.isPhysical());
assertEquals(N, aClass.getFields().length);
}
}