[LL FIR] KT-60570 Use emptySet in Kotlin symbol names provider to conserve memory

- As mentioned in KT-60570, memory consumption in cached symbol names
  providers is caused partially by lots of instances of empty mutable
  sets. This commit replaces such sets with the `emptySet()` singleton.
- This improvement also makes sense for callable names because the IDE
  "package names with top-level callables" set currently contains all
  package names in the project.
- In my local tests of the `setUp` Find Usages performance test, the
  retained size of `FirDelegatingCachedSymbolNamesProvider` is cut down
  from 208MB to 170MB by the empty class name set optimization, and from
  170MB to 115MB by the empty callable name set optimization.
This commit is contained in:
Marco Pennekamp
2023-07-25 18:39:44 +02:00
committed by Space Team
parent 336ea28735
commit 017784d24f
2 changed files with 19 additions and 2 deletions
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.utils.collections
/**
* Maps all elements of this non-empty collection with the given [transform] function to a new mutable set, or returns [emptySet] if this
* collection is empty.
*
* [mapToSet] should be preferred over `collection.mapTo(mutableSetOf()) { ... }` when `collection` may be empty and the resulting set may
* be cached, because [mapToSet] saves memory by avoiding the creation of an empty mutable set.
*/
public inline fun <T, R> Collection<T>.mapToSet(transform: (T) -> R): Set<R> =
if (isNotEmpty()) mapTo(mutableSetOf(), transform) else emptySet()