From 237f5b0c593587ea5d3f9886cb8e2d332c01372e Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 3 Jun 2022 16:44:15 +0200 Subject: [PATCH] [indexing] Reduce allocations when result is empty IntLinkedOpenHashSet allocates quite much (int[] array+long[] array) but in most of the cases they are never filled GitOrigin-RevId: e3079189f5ac61d511deaaab08b8610ffc42763a --- .../com/intellij/psi/stubs/StubIndexEx.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/platform/indexing-impl/src/com/intellij/psi/stubs/StubIndexEx.java b/platform/indexing-impl/src/com/intellij/psi/stubs/StubIndexEx.java index 32bb7b1d9a69..de5609c3ab2e 100644 --- a/platform/indexing-impl/src/com/intellij/psi/stubs/StubIndexEx.java +++ b/platform/indexing-impl/src/com/intellij/psi/stubs/StubIndexEx.java @@ -27,6 +27,7 @@ import com.intellij.util.io.VoidDataExternalizer; import it.unimi.dsi.fastutil.ints.IntIterator; import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet; import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.ints.IntSets; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -356,23 +357,27 @@ public abstract class StubIndexEx extends StubIndex { UpdatableIndex stubUpdatingIndex = fileBasedIndex.getIndex(stubUpdatingIndexId); try { - IntSet result = new IntLinkedOpenHashSet(); // workaround duplicates keys + // workaround duplicates keys + var action = new ValueContainer.ContainerAction() { + IntSet result = null; + + @Override + public boolean perform(int id, Void value) { + if (finalIdFilter == null || finalIdFilter.containsFileId(id)) { + if (result == null) { + result = new IntLinkedOpenHashSet(); + } + result.add(id); + } + return true; + } + }; myAccessValidator.validate(stubUpdatingIndexId, ()-> { // disable up-to-date check to avoid locks on attempt to acquire index write lock while holding at the same time the readLock for this index - //noinspection Convert2Lambda (workaround for JBR crash, JBR-2349),Convert2Diamond - return FileBasedIndexEx.disableUpToDateCheckIn(() -> ConcurrencyUtil.withLock(stubUpdatingIndex.getLock().readLock(), () -> - index.getData(dataKey).forEach(new ValueContainer.ContainerAction<>() { - @Override - public boolean perform(int id, Void value) { - if (finalIdFilter == null || finalIdFilter.containsFileId(id)) { - result.add(id); - } - return true; - } - }) - )); + return FileBasedIndexEx.disableUpToDateCheckIn(() -> ConcurrencyUtil.withLock( + stubUpdatingIndex.getLock().readLock(), () -> index.getData(dataKey).forEach(action))); }); - return result; + return action.result == null ? IntSets.EMPTY_SET : action.result; } catch (StorageException e) { forceRebuild(e);