From a703f5afb8e694b5fa8ad812855c6e9d32f04cd4 Mon Sep 17 00:00:00 2001 From: Marco Pennekamp Date: Wed, 22 Nov 2023 21:41:38 +0100 Subject: [PATCH] [LL] Use `emptyMap()` for empty `FileStructureElementDiagnosticList`s - This improves the memory usage of `FileStructureElementDiagnosticList` by avoiding lots of duplicate empty mutable maps. We can assume that most diagnostics lists are empty, since code is usually green. - I tested the memory consumption manually by opening all files in `org.jetbrains.kotlin.idea.k2.codeinsight.inspections` and waiting for analysis to finish for each file. With the change, the retained size of `FileStructureElementDiagnosticList` shrank from 63.39kB to 13.77kB. --- .../level/api/fir/diagnostics/LLFirDiagnosticReporter.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostics/LLFirDiagnosticReporter.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostics/LLFirDiagnosticReporter.kt index 6bae9cb9dea..8196fed33e3 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostics/LLFirDiagnosticReporter.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostics/LLFirDiagnosticReporter.kt @@ -14,7 +14,9 @@ import org.jetbrains.kotlin.diagnostics.* internal class LLFirDiagnosticReporter : DiagnosticReporter() { private val pendingDiagnostics = mutableMapOf>() - val committedDiagnostics = mutableMapOf>() + private val _committedDiagnostics = mutableMapOf>() + + val committedDiagnostics get() = _committedDiagnostics.ifEmpty { emptyMap() } override fun report(diagnostic: KtDiagnostic?, context: DiagnosticContext) { if (diagnostic == null) return @@ -35,7 +37,7 @@ internal class LLFirDiagnosticReporter : DiagnosticReporter() { override fun checkAndCommitReportsOn(element: AbstractKtSourceElement, context: DiagnosticContext?) { val commitEverything = context == null for ((diagnosticElement, pendingList) in pendingDiagnostics) { - val committedList = committedDiagnostics.getOrPut(diagnosticElement) { mutableListOf() } + val committedList = _committedDiagnostics.getOrPut(diagnosticElement) { mutableListOf() } val iterator = pendingList.iterator() while (iterator.hasNext()) { val diagnostic = iterator.next()