From 4fcb9ea9a5cd6b08ed431d92997deb1fd72c28da Mon Sep 17 00:00:00 2001 From: Alexander Podkhalyuzin Date: Tue, 13 Oct 2020 18:13:11 +0300 Subject: [PATCH] Possible optimization for the refresh in indexes: Plain cycle could be faster than stream on performance critical path. For #IDEA-245096 GitOrigin-RevId: 79b70fb7185833301fbb5ca6297a1006f8747746 --- .../util/indexing/FileBasedIndexImpl.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java index 3659c64ce623..8ce1dec11913 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java @@ -2,6 +2,7 @@ package com.intellij.util.indexing; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.Iterators; import com.intellij.AppTopics; import com.intellij.ide.AppLifecycleListener; import com.intellij.ide.startup.ServiceNotReadyException; @@ -268,13 +269,25 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx { } boolean processChangedFiles(@NotNull Project project, @NotNull Processor processor) { + // can be performance critical, better to use cycle instead of streams // avoid missing files when events are processed concurrently - return Stream.concat(getChangedFilesCollector().getEventMerger().getChangedFiles(), - getChangedFilesCollector().getFilesToUpdate()) - .filter(filesToBeIndexedForProjectCondition(project)) - .distinct() - .mapToInt(f -> processor.process(f) ? 1 : 0) - .allMatch(success -> success == 1); + Iterator iterator = Iterators.concat( + getChangedFilesCollector().getEventMerger().getChangedFiles().iterator(), + getChangedFilesCollector().getFilesToUpdate().iterator() + ); + + HashSet checkedFiles = new HashSet<>(); + Predicate filterPredicate = filesToBeIndexedForProjectCondition(project); + + while (iterator.hasNext()) { + VirtualFile virtualFile = iterator.next(); + if (filterPredicate.test(virtualFile) && !checkedFiles.contains(virtualFile)) { + checkedFiles.add(virtualFile); + if (!processor.process(virtualFile)) return false; + } + } + + return true; } RegisteredIndexes getRegisteredIndexes() {