Skip to content

Commit 39d86de

Browse files
authored
Avoid using immutableCopyOf() when mutableTypes = true, for repeated and map types. (#3352)
* This avoids allocations, when mutable types are enabled for performance sensitive code. Test: ./gradlew :wire-golden-files:test
1 parent ce68971 commit 39d86de

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePacket.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import com.squareup.wire.ReverseProtoWriter
1616
import com.squareup.wire.Syntax.PROTO_2
1717
import com.squareup.wire.WireField
1818
import com.squareup.wire.`internal`.JvmField
19-
import com.squareup.wire.`internal`.immutableCopyOf
2019
import kotlin.Any
2120
import kotlin.Boolean
2221
import kotlin.Deprecated
@@ -38,17 +37,15 @@ public class MutablePacket(
3837
schemaIndex = 0,
3938
)
4039
public var header_: MutableHeader? = null,
41-
payload: List<MutablePayload> = emptyList(),
42-
override var unknownFields: ByteString = ByteString.EMPTY,
43-
) : Message<MutablePacket, Nothing>(ADAPTER, unknownFields) {
4440
@field:WireField(
4541
tag = 2,
4642
adapter = "squareup.wire.mutable.MutablePayload#ADAPTER",
4743
label = WireField.Label.REPEATED,
4844
schemaIndex = 1,
4945
)
50-
public var payload: List<MutablePayload> = immutableCopyOf("payload", payload)
51-
46+
public var payload: List<MutablePayload> = emptyList(),
47+
override var unknownFields: ByteString = ByteString.EMPTY,
48+
) : Message<MutablePacket, Nothing>(ADAPTER, unknownFields) {
5249
@Deprecated(
5350
message = "Shouldn't be used in Kotlin",
5451
level = DeprecationLevel.HIDDEN,

wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePayload.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import com.squareup.wire.WireEnum
1919
import com.squareup.wire.WireField
2020
import com.squareup.wire.`internal`.JvmField
2121
import com.squareup.wire.`internal`.JvmStatic
22-
import com.squareup.wire.`internal`.immutableCopyOf
2322
import com.squareup.wire.`internal`.sanitize
2423
import kotlin.Any
2524
import kotlin.Boolean
@@ -53,17 +52,15 @@ public class MutablePayload(
5352
schemaIndex = 2,
5453
)
5554
public var type: Type? = null,
56-
footers: List<String> = emptyList(),
57-
override var unknownFields: ByteString = ByteString.EMPTY,
58-
) : Message<MutablePayload, Nothing>(ADAPTER, unknownFields) {
5955
@field:WireField(
6056
tag = 4,
6157
adapter = "com.squareup.wire.ProtoAdapter#STRING",
6258
label = WireField.Label.REPEATED,
6359
schemaIndex = 3,
6460
)
65-
public var footers: List<String> = immutableCopyOf("footers", footers)
66-
61+
public var footers: List<String> = emptyList(),
62+
override var unknownFields: ByteString = ByteString.EMPTY,
63+
) : Message<MutablePayload, Nothing>(ADAPTER, unknownFields) {
6764
@Deprecated(
6865
message = "Shouldn't be used in Kotlin",
6966
level = DeprecationLevel.HIDDEN,

wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,12 +1251,17 @@ class KotlinGenerator private constructor(
12511251
CodeBlock.of(if (buildersOnly) "builder.$fieldName" else fieldName)
12521252
}
12531253
field.isRepeated || field.isMap -> {
1254-
CodeBlock.of(
1255-
if (buildersOnly) "%M(%S, builder.%N)" else "%M(%S, %N)",
1256-
MemberName("com.squareup.wire.internal", "immutableCopyOf"),
1257-
fieldName,
1258-
fieldName,
1259-
)
1254+
if (mutableTypes) {
1255+
// For mutable types, don't bother using immutableCopyOf(...)
1256+
CodeBlock.of("%N", fieldName)
1257+
} else {
1258+
CodeBlock.of(
1259+
if (buildersOnly) "%M(%S, builder.%N)" else "%M(%S, %N)",
1260+
MemberName("com.squareup.wire.internal", "immutableCopyOf"),
1261+
fieldName,
1262+
fieldName,
1263+
)
1264+
}
12601265
}
12611266
!field.isRepeated && !field.isMap && field.isRequired && buildersOnly -> {
12621267
CodeBlock.of("builder.%N!!", fieldName)

0 commit comments

Comments
 (0)