File tree 2 files changed +12
-2
lines changed
stdlib/common/src/generated
tools/kotlin-stdlib-gen/src/templates
2 files changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -9831,7 +9831,7 @@ public fun <T> Array<out T>.toList(): List<T> {
9831
9831
return when (size) {
9832
9832
0 -> emptyList()
9833
9833
1 -> listOf(this[0])
9834
- else -> this.toMutableList ()
9834
+ else -> copyOf().asList ()
9835
9835
}
9836
9836
}
9837
9837
Original file line number Diff line number Diff line change @@ -173,7 +173,7 @@ object Snapshots : TemplateGroupBase() {
173
173
return this.toMutableList().optimizeReadOnlyList()
174
174
"""
175
175
}
176
- body(CharSequences , ArraysOfPrimitives , ArraysOfObjects ) {
176
+ body(CharSequences , ArraysOfPrimitives ) {
177
177
"""
178
178
return when (${f.code.size} ) {
179
179
0 -> emptyList()
@@ -182,6 +182,16 @@ object Snapshots : TemplateGroupBase() {
182
182
}
183
183
"""
184
184
}
185
+ // For object array, ensure a single array copy instead of delegating to `toMutableList`, which can cause two copies (see KT-75801)
186
+ body(ArraysOfObjects ) {
187
+ """
188
+ return when (${f.code.size} ) {
189
+ 0 -> emptyList()
190
+ 1 -> listOf(this[0])
191
+ else -> copyOf().asList()
192
+ }
193
+ """
194
+ }
185
195
body(Sequences ) { optimizedSequenceToCollection(" emptyList" , " listOf" , " ArrayList" ) }
186
196
specialFor(Maps ) {
187
197
doc { " Returns a [List] containing all key-value pairs." }
You can’t perform that action at this time.
0 commit comments