https://kotlinlang.org logo
#language-evolution
Title
# language-evolution
e

eygraber

11/23/2023, 2:13 AM
Not sure if this belongs here, but why are the implementations of
toSet
and
toMutableSet
different? Can't they both use
LinkedHashSet(this)
(which seems to be more performant)?
d

Derek Peirce

11/23/2023, 5:27 AM
From what I can tell,
toMutableSet()
uses a custom
mapCapacity
method to make the set's underlying hash space slightly larger than necessary to accommodate new entries, while
toSet()
is content with the JVM default as there will be no more new entries.
c

CLOVIS

11/23/2023, 10:18 AM
Iterable.toSetIterable.toMutableSet
toSet
has specific cases for the empty set and the singleton set (both use less memory than real sets).
1