From 5da0a28355d6a3cbd4e5f3cc73366f21699d4813 Mon Sep 17 00:00:00 2001 From: Tomasz Pasternak Date: Tue, 10 Dec 2024 22:48:58 +0100 Subject: [PATCH 1/2] fix: Reduce freeze in ProjectUpdater closes #7149 --- .../idea/blaze/base/qsync/ProjectUpdater.java | 8 +++---- .../idea/blaze/base/qsync/ThreadingUtils.kt | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 base/src/com/google/idea/blaze/base/qsync/ThreadingUtils.kt diff --git a/base/src/com/google/idea/blaze/base/qsync/ProjectUpdater.java b/base/src/com/google/idea/blaze/base/qsync/ProjectUpdater.java index a7026e015bc..a7783750345 100644 --- a/base/src/com/google/idea/blaze/base/qsync/ProjectUpdater.java +++ b/base/src/com/google/idea/blaze/base/qsync/ProjectUpdater.java @@ -37,7 +37,6 @@ import com.google.idea.blaze.qsync.project.ProjectPath; import com.google.idea.blaze.qsync.project.ProjectProto; import com.google.idea.blaze.qsync.project.ProjectProto.LibrarySource; -import com.google.idea.common.util.Transactions; import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider; import com.intellij.openapi.externalSystem.service.project.ProjectDataManager; import com.intellij.openapi.module.Module; @@ -116,7 +115,7 @@ public void onNewProjectSnapshot(Context context, QuerySyncProjectSnapshot gr private void updateProjectModel(ProjectProto.Project spec, Context context) { File imlDirectory = new File(BlazeDataStorage.getProjectDataDir(importSettings), "modules"); - Transactions.submitWriteActionTransactionAndWait( + ThreadingUtils.Companion.readWriteAction( () -> { IdeModifiableModelsProvider models = ProjectDataManager.getInstance().createModifiableModelsProvider(project); @@ -217,9 +216,10 @@ private void updateProjectModel(ProjectProto.Project spec, Context context) { .build(), workspaceLanguageSettings); } - models.commit(); + } - }); + return models; + }, models -> models.commit()); } private Library getOrCreateLibrary( diff --git a/base/src/com/google/idea/blaze/base/qsync/ThreadingUtils.kt b/base/src/com/google/idea/blaze/base/qsync/ThreadingUtils.kt new file mode 100644 index 00000000000..1453f8467c6 --- /dev/null +++ b/base/src/com/google/idea/blaze/base/qsync/ThreadingUtils.kt @@ -0,0 +1,22 @@ +package com.google.idea.blaze.base.qsync + +import com.intellij.openapi.application.readAndWriteAction +import kotlinx.coroutines.runBlocking +import java.util.concurrent.Callable +import java.util.function.Consumer + + +class ThreadingUtils { + companion object { + fun readWriteAction(readPart: Callable, commit: Consumer) { + runBlocking { + readAndWriteAction { + var ret = readPart.call() + writeAction { + commit.accept(ret); + } + } + } + } + } +} \ No newline at end of file From 8be88d3e5d8f463d51f6f94cff4d54a879c1eb2a Mon Sep 17 00:00:00 2001 From: Tomasz Pasternak Date: Wed, 11 Dec 2024 11:24:33 +0100 Subject: [PATCH 2/2] Allow to switch back to the old behavior (just in case) --- base/src/META-INF/blaze-base.xml | 3 ++ .../idea/blaze/base/qsync/ProjectUpdater.java | 2 +- .../qsync/ProjectUpdaterThreadingUtils.kt | 33 +++++++++++++++++++ .../idea/blaze/base/qsync/ThreadingUtils.kt | 22 ------------- 4 files changed, 37 insertions(+), 23 deletions(-) create mode 100644 base/src/com/google/idea/blaze/base/qsync/ProjectUpdaterThreadingUtils.kt delete mode 100644 base/src/com/google/idea/blaze/base/qsync/ThreadingUtils.kt diff --git a/base/src/META-INF/blaze-base.xml b/base/src/META-INF/blaze-base.xml index f186817d8f7..18cddb8e893 100644 --- a/base/src/META-INF/blaze-base.xml +++ b/base/src/META-INF/blaze-base.xml @@ -424,6 +424,9 @@ + diff --git a/base/src/com/google/idea/blaze/base/qsync/ProjectUpdater.java b/base/src/com/google/idea/blaze/base/qsync/ProjectUpdater.java index a7783750345..ad8fd3426e8 100644 --- a/base/src/com/google/idea/blaze/base/qsync/ProjectUpdater.java +++ b/base/src/com/google/idea/blaze/base/qsync/ProjectUpdater.java @@ -115,7 +115,7 @@ public void onNewProjectSnapshot(Context context, QuerySyncProjectSnapshot gr private void updateProjectModel(ProjectProto.Project spec, Context context) { File imlDirectory = new File(BlazeDataStorage.getProjectDataDir(importSettings), "modules"); - ThreadingUtils.Companion.readWriteAction( + ProjectUpdaterThreadingUtils.Companion.readWriteAction( () -> { IdeModifiableModelsProvider models = ProjectDataManager.getInstance().createModifiableModelsProvider(project); diff --git a/base/src/com/google/idea/blaze/base/qsync/ProjectUpdaterThreadingUtils.kt b/base/src/com/google/idea/blaze/base/qsync/ProjectUpdaterThreadingUtils.kt new file mode 100644 index 00000000000..354d2750d08 --- /dev/null +++ b/base/src/com/google/idea/blaze/base/qsync/ProjectUpdaterThreadingUtils.kt @@ -0,0 +1,33 @@ +package com.google.idea.blaze.base.qsync + +import com.google.idea.common.util.Transactions +import com.intellij.openapi.application.readAndWriteAction +import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.util.registry.Registry +import kotlinx.coroutines.runBlocking +import java.util.concurrent.Callable +import java.util.function.Consumer + +class ProjectUpdaterThreadingUtils { + companion object { + val logger = Logger.getInstance(ProjectUpdaterThreadingUtils::class.java) + fun readWriteAction(readPart: Callable, commit: Consumer) { + if (Registry.`is`("bazel.qsync.enable.coroutine.project.updater")) { + runBlocking { + readAndWriteAction { + logger.info("Starting read operation") + val ret = readPart.call() + writeAction { + commit.accept(ret) + } + } + } + } else { + Transactions.submitWriteActionTransactionAndWait { + val ret = readPart.call() + commit.accept(ret) + } + } + } + } +} \ No newline at end of file diff --git a/base/src/com/google/idea/blaze/base/qsync/ThreadingUtils.kt b/base/src/com/google/idea/blaze/base/qsync/ThreadingUtils.kt deleted file mode 100644 index 1453f8467c6..00000000000 --- a/base/src/com/google/idea/blaze/base/qsync/ThreadingUtils.kt +++ /dev/null @@ -1,22 +0,0 @@ -package com.google.idea.blaze.base.qsync - -import com.intellij.openapi.application.readAndWriteAction -import kotlinx.coroutines.runBlocking -import java.util.concurrent.Callable -import java.util.function.Consumer - - -class ThreadingUtils { - companion object { - fun readWriteAction(readPart: Callable, commit: Consumer) { - runBlocking { - readAndWriteAction { - var ret = readPart.call() - writeAction { - commit.accept(ret); - } - } - } - } - } -} \ No newline at end of file