[LL] Use emptyMap() for empty FileStructureElementDiagnosticLists

- 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.
This commit is contained in:
Marco Pennekamp
2023-11-22 21:41:38 +01:00
committed by Space Team
parent 59c4c83ed5
commit a703f5afb8
@@ -14,7 +14,9 @@ import org.jetbrains.kotlin.diagnostics.*
internal class LLFirDiagnosticReporter : DiagnosticReporter() {
private val pendingDiagnostics = mutableMapOf<PsiElement, MutableList<KtPsiDiagnostic>>()
val committedDiagnostics = mutableMapOf<PsiElement, MutableList<KtPsiDiagnostic>>()
private val _committedDiagnostics = mutableMapOf<PsiElement, MutableList<KtPsiDiagnostic>>()
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()