[java-refactoring] Avoid local ref cache during inline refactoring

Useless anyway, as the files are constantly updated; does many unrelated computations
IDEA-364541 Inline method spends a lot of time inside getVariableReferences


(cherry picked from commit 1814ada8815641cb9159bbe10bc28c719d2bbf44)

IJ-CR-151330

GitOrigin-RevId: 406d056a261e17cf8d34d5b4edfa4a7a9e592d1a
This commit is contained in:
Tagir Valeev
2024-12-10 17:18:42 +01:00
committed by intellij-monorepo-bot
parent 6e515ed9b8
commit 0a7dce9cba
2 changed files with 26 additions and 3 deletions

View File

@@ -285,6 +285,25 @@ public final class VariableAccessUtils {
return LocalRefUseInfo.forFile(file).getVariableReferences(variable, null);
}
/**
* Finds all references to the specified variable in the declaration scope of the variable,
* i.e. everywhere the variable is accessible.
* NOTE: this method will only search in the containing file for the variable. This may lead to incorrect results for fields.
* <p>
* This method behaves exactly like {@link #getVariableReferences(PsiVariable)} but it will not
* try to compute references data for the whole file, so in some specific scenarios
* (e.g., searching in many different files not opened in the editor) it might be faster.
* </p>
*
* @param variable the variable to find references for
* @return a list of references, empty list if no references were found.
*/
public static List<PsiReferenceExpression> getVariableReferencesNoCache(@NotNull PsiVariable variable) {
PsiElement scope = variable instanceof PsiField ? variable.getContainingFile() : PsiUtil.getVariableCodeBlock(variable, null);
if (scope == null) return List.of();
return getVariableReferencesNoCache(variable, scope);
}
/**
* Finds all references to the specified variable in the specified context.
* @param variable the variable to find references for
@@ -298,6 +317,10 @@ public final class VariableAccessUtils {
if (variableFile != null && file == variableFile && file.isPhysical()) {
return LocalRefUseInfo.forFile(file).getVariableReferences(variable, context);
}
return getVariableReferencesNoCache(variable, context);
}
private static @NotNull List<PsiReferenceExpression> getVariableReferencesNoCache(@NotNull PsiVariable variable, @NotNull PsiElement context) {
List<PsiReferenceExpression> result = new ArrayList<>();
PsiTreeUtil.processElements(context, e -> {
if (e instanceof PsiReferenceExpression && ((PsiReferenceExpression)e).isReferenceTo(variable)) {

View File

@@ -501,7 +501,7 @@ public final class InlineUtil implements CommonJavaInlineUtil {
}
public static boolean canInlineParameterOrThisVariable(PsiLocalVariable variable) {
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferences(variable);
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferencesNoCache(variable);
boolean isAccessedForWriting = false;
for (PsiReferenceExpression refElement : refs) {
if (PsiUtil.isAccessedForWriting(refElement)) {
@@ -737,7 +737,7 @@ public final class InlineUtil implements CommonJavaInlineUtil {
* @param strictlyFinal whether the variable is referenced in the places where final variable is required
*/
public static void tryInlineGeneratedLocal(PsiLocalVariable variable, boolean strictlyFinal) throws IncorrectOperationException {
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferences(variable);
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferencesNoCache(variable);
PsiReferenceExpression firstRef = ContainerUtil.getFirstItem(refs);
PsiExpression initializer = variable.getInitializer();
@@ -806,7 +806,7 @@ public final class InlineUtil implements CommonJavaInlineUtil {
throws IncorrectOperationException {
PsiElement context = PsiUtil.getVariableCodeBlock(resultVar, null);
if (context == null) return;
List<PsiReferenceExpression> references = VariableAccessUtils.getVariableReferences(resultVar);
List<PsiReferenceExpression> references = VariableAccessUtils.getVariableReferencesNoCache(resultVar);
if (resultVar.getInitializer() == null) {
PsiAssignmentExpression assignment = null;
for (PsiReferenceExpression ref : references) {