[java-dfa] Optimize getPossibleTargetIndices and isLinear

GitOrigin-RevId: 96d89e1c669490daffd5b3627689bb22f0e5e0ca
This commit is contained in:
Tagir Valeev
2023-12-11 12:02:30 +01:00
committed by intellij-monorepo-bot
parent 8b12dee884
commit 542593d7dd
6 changed files with 37 additions and 15 deletions

View File

@@ -12,7 +12,6 @@ import com.intellij.util.containers.FList;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class EnterFinallyTrap implements DfaControlTransferValue.Trap {
@@ -44,8 +43,8 @@ public class EnterFinallyTrap implements DfaControlTransferValue.Trap {
}
@Override
public @NotNull Collection<@NotNull Integer> getPossibleTargets() {
return List.of(myJumpOffset.getInstructionOffset());
public int @NotNull [] getPossibleTargets() {
return new int[] {myJumpOffset.getInstructionOffset()};
}
@NotNull

View File

@@ -10,7 +10,6 @@ import com.intellij.psi.PsiElement;
import com.intellij.util.containers.FList;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.List;
public class TryCatchAllTrap implements DfaControlTransferValue.Trap {
@@ -23,7 +22,7 @@ public class TryCatchAllTrap implements DfaControlTransferValue.Trap {
}
@Override
public @NotNull Collection<@NotNull Integer> getPossibleTargets() {
public int @NotNull [] getPossibleTargets() {
return DfaControlTransferValue.Trap.super.getPossibleTargets();
}

View File

@@ -110,8 +110,14 @@ public class TryCatchTrap implements Trap {
}
@Override
public @NotNull Collection<@NotNull Integer> getPossibleTargets() {
return ContainerUtil.map(myClauses.values(), ControlFlow.ControlFlowOffset::getInstructionOffset);
public int @NotNull [] getPossibleTargets() {
Collection<? extends ControlFlow.ControlFlowOffset> values = myClauses.values();
int[] result = new int[values.size()];
int i = 0;
for (ControlFlow.ControlFlowOffset value : values) {
result[i++] = value.getInstructionOffset();
}
return result;
}
@Override

View File

@@ -49,6 +49,11 @@ public class ControlTransferInstruction extends Instruction {
return myTransfer.getPossibleTargetIndices();
}
@Override
public boolean isLinear() {
return false;
}
@Override
public String toString() {
int[] indexes = getSuccessorIndexes();

View File

@@ -109,6 +109,11 @@ public class EnsureInstruction extends Instruction {
return result.toArray(DfaInstructionState.EMPTY_ARRAY);
}
@Override
public boolean isLinear() {
return myTransferValue == null;
}
@Override
public int @NotNull [] getSuccessorIndexes() {
if (myTransferValue == null) {

View File

@@ -7,10 +7,10 @@ import com.intellij.codeInspection.dataFlow.memory.DfaMemoryState;
import com.intellij.psi.PsiElement;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.FList;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -18,8 +18,8 @@ import java.util.List;
* A value that could be pushed to the stack and used for control transfer
*/
public final class DfaControlTransferValue extends DfaValue {
final @NotNull TransferTarget target;
final @NotNull FList<Trap> traps;
private final @NotNull TransferTarget target;
private final @NotNull FList<Trap> traps;
DfaControlTransferValue(@NotNull DfaValueFactory factory,
@NotNull TransferTarget target,
@@ -47,8 +47,16 @@ public final class DfaControlTransferValue extends DfaValue {
}
public int @NotNull [] getPossibleTargetIndices() {
return StreamEx.of(traps).flatCollection(Trap::getPossibleTargets).mapToInt(x -> x).append(target.getPossibleTargets())
.distinct().toArray();
IntOpenHashSet indices = new IntOpenHashSet();
for (Trap trap : traps) {
for (int possibleTarget : trap.getPossibleTargets()) {
indices.add(possibleTarget);
}
}
for (int possibleTarget : target.getPossibleTargets()) {
indices.add(possibleTarget);
}
return indices.toIntArray();
}
public @NotNull List<DfaInstructionState> dispatch(@NotNull DfaMemoryState state, @NotNull DataFlowInterpreter interpreter) {
@@ -111,10 +119,10 @@ public final class DfaControlTransferValue extends DfaValue {
*/
public interface Trap {
/**
* @return list of possible instruction offsets for given trap
* @return array of possible instruction offsets for given trap
*/
default @NotNull Collection<@NotNull Integer> getPossibleTargets() {
return Collections.emptyList();
default int @NotNull [] getPossibleTargets() {
return ArrayUtil.EMPTY_INT_ARRAY;
}
default void link(DfaControlTransferValue value) {