y
05/17/2025, 10:49 AMList
vs `MutableList`:
first, I understand that these are interfaces and not concrete classes.
my question is, how cheap is it to go from a List
to a MutableList
, and if the answer is not "pretty cheap", is there anything I can do to make it cheaper?
operations on collections (more specifically, on Iterable
) return List
and that's rather irritating if I want to continue editing the thing.
is it at least cheap to then call toMutableList()
?hfhbd
05/17/2025, 11:00 AMtoMutableList
has a fast path if the runtime instance already is a MutableList.y
05/17/2025, 11:01 AMMutableList
after calling some std method on it?y
05/17/2025, 11:02 AMMutableList
from this list (not cheap)y
05/17/2025, 11:39 AMWout Werkman
05/17/2025, 12:28 PMxxxTo
functions.
.map { ... }.toMutableList()
.filter { ... }.toMutableList()
// And some others are the same as:
.mapTo(mutableListOf()) { ... }
.filterTo(mutableListOf()) { ... }
y
05/17/2025, 12:36 PM.somethingTo(mutableListOf()) { ... }
sure looks like it's allocating a new list...?Wout Werkman
05/17/2025, 12:37 PMy
05/17/2025, 12:37 PMWout Werkman
05/17/2025, 12:37 PMalso, this pattern of chainingCorrect. compared tosure looks like it's allocating a new list...?.somethingTo(mutableListOf()) { ... }
something { ... }.toMutableList()
it only saves a single allocationhfhbd
05/17/2025, 12:37 PMy
05/17/2025, 12:38 PMephemient
05/17/2025, 1:00 PMephemient
05/17/2025, 1:02 PMy
05/17/2025, 1:05 PMy
05/17/2025, 1:09 PMephemient
05/17/2025, 1:10 PMephemient
05/17/2025, 1:11 PMy
05/17/2025, 1:13 PMy
05/17/2025, 1:13 PMloke
05/19/2025, 10:50 AMy
05/19/2025, 11:53 AMephemient
05/19/2025, 9:05 PMloke
05/20/2025, 3:53 AMephemient
05/20/2025, 4:23 AMephemient
05/20/2025, 11:06 PMy
05/22/2025, 5:20 AM