Skip to content

Commit 09b494c

Browse files
authored
chore: optimize codegen for size round1 (#533)
1 parent 31b7922 commit 09b494c

File tree

27 files changed

+286
-337
lines changed

27 files changed

+286
-337
lines changed

runtime/protocol/http/common/src/aws/smithy/kotlin/runtime/http/operation/SdkHttpOperation.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SdkHttpOperation<I, O>(
5656
}
5757

5858
companion object {
59-
fun <I, O> build(block: SdkHttpOperationBuilder<I, O>.() -> Unit): SdkHttpOperation<I, O> =
59+
inline fun <I, O> build(block: SdkHttpOperationBuilder<I, O>.() -> Unit): SdkHttpOperation<I, O> =
6060
SdkHttpOperationBuilder<I, O>().apply(block).build()
6161
}
6262
}
@@ -109,4 +109,4 @@ class SdkHttpOperationBuilder<I, O> {
109109
/**
110110
* Configure HTTP operation context elements
111111
*/
112-
fun <I, O> SdkHttpOperationBuilder<I, O>.context(block: HttpOperationContext.Builder.() -> Unit) = context.apply(block)
112+
inline fun <I, O> SdkHttpOperationBuilder<I, O>.context(block: HttpOperationContext.Builder.() -> Unit) = context.apply(block)

runtime/serde/common/src/aws/smithy/kotlin/runtime/serde/Deserializer.kt

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ interface Deserializer {
6262
*
6363
* @param descriptor SdkObjectDescriptor the structure descriptor
6464
*/
65-
suspend fun deserializeStruct(descriptor: SdkObjectDescriptor): FieldIterator
65+
fun deserializeStruct(descriptor: SdkObjectDescriptor): FieldIterator
6666

6767
/**
6868
* Begin deserialization of a list type. Use the returned [ElementIterator] to drive
@@ -73,7 +73,7 @@ interface Deserializer {
7373
*
7474
* @param descriptor SdkFieldDescriptor the structure descriptor
7575
*/
76-
suspend fun deserializeList(descriptor: SdkFieldDescriptor): ElementIterator
76+
fun deserializeList(descriptor: SdkFieldDescriptor): ElementIterator
7777

7878
/**
7979
* Begin deserialization of a map type. Use the returned [EntryIterator] to drive
@@ -84,7 +84,7 @@ interface Deserializer {
8484
*
8585
* @param descriptor SdkFieldDescriptor the structure descriptor
8686
*/
87-
suspend fun deserializeMap(descriptor: SdkFieldDescriptor): EntryIterator
87+
fun deserializeMap(descriptor: SdkFieldDescriptor): EntryIterator
8888

8989
/**
9090
* Iterator over raw elements in a collection
@@ -94,12 +94,12 @@ interface Deserializer {
9494
* Advance to the next element. Returns false when no more elements are in the list
9595
* or the document has been read completely.
9696
*/
97-
suspend fun hasNextElement(): Boolean
97+
fun hasNextElement(): Boolean
9898

9999
/**
100100
* Returns true if the next token contains a value, or false otherwise.
101101
*/
102-
suspend fun nextHasValue(): Boolean
102+
fun nextHasValue(): Boolean
103103
}
104104

105105
/**
@@ -110,17 +110,17 @@ interface Deserializer {
110110
* Advance to the next element. Returns false when no more elements are in the map
111111
* or the document has been read completely.
112112
*/
113-
suspend fun hasNextEntry(): Boolean
113+
fun hasNextEntry(): Boolean
114114

115115
/**
116116
* Read the next key
117117
*/
118-
suspend fun key(): String
118+
fun key(): String
119119

120120
/**
121121
* Returns true if the next token contains a value, or false otherwise.
122122
*/
123-
suspend fun nextHasValue(): Boolean
123+
fun nextHasValue(): Boolean
124124
}
125125

126126
/**
@@ -130,12 +130,12 @@ interface Deserializer {
130130
/**
131131
* Returns the index of the next field found, null if fields exhausted, or [UNKNOWN_FIELD].
132132
*/
133-
suspend fun findNextFieldIndex(): Int?
133+
fun findNextFieldIndex(): Int?
134134

135135
/**
136136
* Skip the next field value recursively. Meant for discarding unknown fields
137137
*/
138-
suspend fun skipValue()
138+
fun skipValue()
139139

140140
companion object {
141141
/**
@@ -153,60 +153,60 @@ interface PrimitiveDeserializer {
153153
/**
154154
* Deserialize and return the next token as a [Byte]
155155
*/
156-
suspend fun deserializeByte(): Byte
156+
fun deserializeByte(): Byte
157157

158158
/**
159159
* Deserialize and return the next token as an [Int]
160160
*/
161-
suspend fun deserializeInt(): Int
161+
fun deserializeInt(): Int
162162

163163
/**
164164
* Deserialize and return the next token as a [Short]
165165
*/
166-
suspend fun deserializeShort(): Short
166+
fun deserializeShort(): Short
167167

168168
/**
169169
* Deserialize and return the next token as a [Long]
170170
*/
171-
suspend fun deserializeLong(): Long
171+
fun deserializeLong(): Long
172172

173173
/**
174174
* Deserialize and return the next token as a [Float]
175175
*/
176-
suspend fun deserializeFloat(): Float
176+
fun deserializeFloat(): Float
177177

178178
/**
179179
* Deserialize and return the next token as a [Double]
180180
*/
181-
suspend fun deserializeDouble(): Double
181+
fun deserializeDouble(): Double
182182

183183
/**
184184
* Deserialize and return the next token as a [String]
185185
*/
186-
suspend fun deserializeString(): String
186+
fun deserializeString(): String
187187

188188
/**
189189
* Deserialize and return the next token as a [Boolean]
190190
*/
191-
suspend fun deserializeBoolean(): Boolean
191+
fun deserializeBoolean(): Boolean
192192

193193
/**
194194
* Consume the next token if represents a null value. Always returns null.
195195
*/
196-
suspend fun deserializeNull(): Nothing?
196+
fun deserializeNull(): Nothing?
197197
}
198198

199-
suspend fun Deserializer.deserializeStruct(descriptor: SdkObjectDescriptor, block: suspend Deserializer.FieldIterator.() -> Unit) {
199+
inline fun Deserializer.deserializeStruct(descriptor: SdkObjectDescriptor, block: Deserializer.FieldIterator.() -> Unit) {
200200
val deserializer = deserializeStruct(descriptor)
201201
block(deserializer)
202202
}
203203

204-
suspend fun <T> Deserializer.deserializeList(descriptor: SdkFieldDescriptor, block: suspend Deserializer.ElementIterator.() -> T): T {
204+
inline fun <T> Deserializer.deserializeList(descriptor: SdkFieldDescriptor, block: Deserializer.ElementIterator.() -> T): T {
205205
val deserializer = deserializeList(descriptor)
206206
return block(deserializer)
207207
}
208208

209-
suspend fun <T> Deserializer.deserializeMap(descriptor: SdkFieldDescriptor, block: suspend Deserializer.EntryIterator.() -> T): T {
209+
inline fun <T> Deserializer.deserializeMap(descriptor: SdkFieldDescriptor, block: Deserializer.EntryIterator.() -> T): T {
210210
val deserializer = deserializeMap(descriptor)
211211
return block(deserializer)
212212
}

runtime/serde/common/src/aws/smithy/kotlin/runtime/serde/SdkObjectDescriptor.kt

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,29 @@ package aws.smithy.kotlin.runtime.serde
77
/**
88
* Metadata container for all fields of an object/class
99
*/
10-
class SdkObjectDescriptor private constructor(builder: BuilderImpl) : SdkFieldDescriptor(
10+
class SdkObjectDescriptor private constructor(builder: Builder) : SdkFieldDescriptor(
1111
kind = SerialKind.Struct, traits = builder.traits
1212
) {
1313
val fields: List<SdkFieldDescriptor> = builder.fields
1414

1515
companion object {
16-
fun build(block: DslBuilder.() -> Unit): SdkObjectDescriptor = BuilderImpl().apply(block).build()
16+
inline fun build(block: Builder.() -> Unit): SdkObjectDescriptor = Builder().apply(block).build()
1717
}
1818

19-
interface DslBuilder {
20-
/**
21-
* Declare a field belonging to this object
22-
*/
23-
fun field(field: SdkFieldDescriptor)
24-
/**
25-
* Declare a trait belonging to this object
26-
*/
27-
fun trait(trait: FieldTrait)
28-
fun build(): SdkObjectDescriptor
29-
}
30-
31-
private class BuilderImpl : DslBuilder {
32-
val fields: MutableList<SdkFieldDescriptor> = mutableListOf()
33-
val traits: MutableSet<FieldTrait> = mutableSetOf()
19+
class Builder {
20+
internal val fields: MutableList<SdkFieldDescriptor> = mutableListOf()
21+
internal val traits: MutableSet<FieldTrait> = mutableSetOf()
3422

35-
override fun field(field: SdkFieldDescriptor) {
23+
fun field(field: SdkFieldDescriptor) {
3624
field.index = fields.size
3725
fields.add(field)
3826
}
3927

40-
override fun trait(trait: FieldTrait) {
28+
fun trait(trait: FieldTrait) {
4129
traits.add(trait)
4230
}
4331

44-
override fun build(): SdkObjectDescriptor = SdkObjectDescriptor(this)
32+
@PublishedApi
33+
internal fun build(): SdkObjectDescriptor = SdkObjectDescriptor(this)
4534
}
4635
}

runtime/serde/common/src/aws/smithy/kotlin/runtime/serde/SdkSerializable.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ private data class SdkSerializableLambda<T>(
2626
}
2727
}
2828

29+
// FIXME - this causes backing classes to be generated behind the scenes and contributes to the overall jar size
2930
fun <T> asSdkSerializable(input: T, serializeFn: SerializeFn<T>): SdkSerializable = SdkSerializableLambda(input, serializeFn)

runtime/serde/serde-json/common/src/aws/smithy/kotlin/runtime/serde/json/JsonDeserializer.kt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ class JsonDeserializer(payload: ByteArray) : Deserializer, Deserializer.ElementI
2121
// deserializing a single byte isn't common in JSON - we are going to assume that bytes are represented
2222
// as numbers and user understands any truncation issues. `deserializeByte` is more common in binary
2323
// formats (e.g. protobufs) where the binary encoding stores metadata in a single byte (e.g. flags or headers)
24-
override suspend fun deserializeByte(): Byte = nextNumberValue { it.toByteOrNull() ?: it.toDouble().toInt().toByte() }
24+
override fun deserializeByte(): Byte = nextNumberValue { it.toByteOrNull() ?: it.toDouble().toInt().toByte() }
2525

26-
override suspend fun deserializeInt(): Int = nextNumberValue { it.toIntOrNull() ?: it.toDouble().toInt() }
26+
override fun deserializeInt(): Int = nextNumberValue { it.toIntOrNull() ?: it.toDouble().toInt() }
2727

28-
override suspend fun deserializeShort(): Short = nextNumberValue { it.toShortOrNull() ?: it.toDouble().toInt().toShort() }
28+
override fun deserializeShort(): Short = nextNumberValue { it.toShortOrNull() ?: it.toDouble().toInt().toShort() }
2929

30-
override suspend fun deserializeLong(): Long = nextNumberValue { it.toLongOrNull() ?: it.toDouble().toLong() }
30+
override fun deserializeLong(): Long = nextNumberValue { it.toLongOrNull() ?: it.toDouble().toLong() }
3131

32-
override suspend fun deserializeFloat(): Float = deserializeDouble().toFloat()
32+
override fun deserializeFloat(): Float = deserializeDouble().toFloat()
3333

34-
override suspend fun deserializeDouble(): Double = nextNumberValue { it.toDouble() }
34+
override fun deserializeDouble(): Double = nextNumberValue { it.toDouble() }
3535

3636
// assert the next token is a Number and execute [block] with the raw value as a string. Returns result
3737
// of executing the block. This is mostly so that numeric conversions can keep as much precision as possible
38-
private suspend fun <T> nextNumberValue(block: (value: String) -> T): T {
38+
private fun <T> nextNumberValue(block: (value: String) -> T): T {
3939
val token = reader.nextToken()
4040
return when {
4141
token is JsonToken.Number -> block(token.value)
@@ -44,7 +44,7 @@ class JsonDeserializer(payload: ByteArray) : Deserializer, Deserializer.ElementI
4444
}
4545
}
4646

47-
override suspend fun deserializeString(): String =
47+
override fun deserializeString(): String =
4848
// allow for tokens to be consumed as string even when the next token isn't a quoted string
4949
when (val token = reader.nextToken()) {
5050
is JsonToken.String -> token.value
@@ -53,17 +53,17 @@ class JsonDeserializer(payload: ByteArray) : Deserializer, Deserializer.ElementI
5353
else -> throw DeserializationException("$token cannot be deserialized as type String")
5454
}
5555

56-
override suspend fun deserializeBoolean(): Boolean {
56+
override fun deserializeBoolean(): Boolean {
5757
val token = reader.nextTokenOf<JsonToken.Bool>()
5858
return token.value
5959
}
6060

61-
override suspend fun deserializeNull(): Nothing? {
61+
override fun deserializeNull(): Nothing? {
6262
reader.nextTokenOf<JsonToken.Null>()
6363
return null
6464
}
6565

66-
override suspend fun deserializeStruct(descriptor: SdkObjectDescriptor): Deserializer.FieldIterator =
66+
override fun deserializeStruct(descriptor: SdkObjectDescriptor): Deserializer.FieldIterator =
6767
when (reader.peek()) {
6868
JsonToken.BeginObject -> {
6969
reader.nextTokenOf<JsonToken.BeginObject>()
@@ -73,24 +73,24 @@ class JsonDeserializer(payload: ByteArray) : Deserializer, Deserializer.ElementI
7373
else -> throw DeserializationException("Unexpected token type ${reader.peek()}")
7474
}
7575

76-
override suspend fun deserializeList(descriptor: SdkFieldDescriptor): Deserializer.ElementIterator {
76+
override fun deserializeList(descriptor: SdkFieldDescriptor): Deserializer.ElementIterator {
7777
reader.nextTokenOf<JsonToken.BeginArray>()
7878
return this
7979
}
8080

81-
override suspend fun deserializeMap(descriptor: SdkFieldDescriptor): Deserializer.EntryIterator {
81+
override fun deserializeMap(descriptor: SdkFieldDescriptor): Deserializer.EntryIterator {
8282
reader.nextTokenOf<JsonToken.BeginObject>()
8383
return this
8484
}
8585

86-
override suspend fun key(): String {
86+
override fun key(): String {
8787
val token = reader.nextTokenOf<JsonToken.Name>()
8888
return token.value
8989
}
9090

91-
override suspend fun nextHasValue(): Boolean = reader.peek() != JsonToken.Null
91+
override fun nextHasValue(): Boolean = reader.peek() != JsonToken.Null
9292

93-
override suspend fun hasNextEntry(): Boolean =
93+
override fun hasNextEntry(): Boolean =
9494
when (reader.peek()) {
9595
JsonToken.EndObject -> {
9696
// consume the token
@@ -102,7 +102,7 @@ class JsonDeserializer(payload: ByteArray) : Deserializer, Deserializer.ElementI
102102
else -> true
103103
}
104104

105-
override suspend fun hasNextElement(): Boolean =
105+
override fun hasNextElement(): Boolean =
106106
when (reader.peek()) {
107107
JsonToken.EndArray -> {
108108
// consume the token
@@ -116,9 +116,9 @@ class JsonDeserializer(payload: ByteArray) : Deserializer, Deserializer.ElementI
116116

117117
// Represents the deserialization of a null object.
118118
private class JsonNullFieldIterator(deserializer: JsonDeserializer) : Deserializer.FieldIterator, Deserializer by deserializer, PrimitiveDeserializer by deserializer {
119-
override suspend fun findNextFieldIndex(): Int? = null
119+
override fun findNextFieldIndex(): Int? = null
120120

121-
override suspend fun skipValue() {
121+
override fun skipValue() {
122122
throw DeserializationException("This should not be called during deserialization.")
123123
}
124124
}
@@ -129,7 +129,7 @@ private class JsonFieldIterator(
129129
deserializer: JsonDeserializer
130130
) : Deserializer.FieldIterator, Deserializer by deserializer, PrimitiveDeserializer by deserializer {
131131

132-
override suspend fun findNextFieldIndex(): Int? {
132+
override fun findNextFieldIndex(): Int? {
133133
val candidate = when (reader.peek()) {
134134
JsonToken.EndObject -> {
135135
// consume the token
@@ -161,7 +161,7 @@ private class JsonFieldIterator(
161161
return candidate
162162
}
163163

164-
override suspend fun skipValue() {
164+
override fun skipValue() {
165165
// stream reader skips the *next* token
166166
reader.skipNext()
167167
}

runtime/serde/serde-json/common/src/aws/smithy/kotlin/runtime/serde/json/JsonLexer.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ internal class JsonLexer(
6262
private val state = StateManager()
6363
private var idx = 0
6464

65-
override suspend fun nextToken(): JsonToken {
65+
override fun nextToken(): JsonToken {
6666
val next = peek()
6767
peeked = null
6868
state.update()
6969
return next
7070
}
7171

72-
override suspend fun peek(): JsonToken = peeked ?: doPeek().also { peeked = it }
72+
override fun peek(): JsonToken = peeked ?: doPeek().also { peeked = it }
7373

74-
override suspend fun skipNext() {
74+
override fun skipNext() {
7575
val startDepth = state.size
7676
nextToken()
7777
while (state.size > startDepth) {

runtime/serde/serde-json/common/src/aws/smithy/kotlin/runtime/serde/json/JsonStreamReader.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ interface JsonStreamReader {
1414
/**
1515
* Grab the next token in the stream
1616
*/
17-
suspend fun nextToken(): JsonToken
17+
fun nextToken(): JsonToken
1818

1919
/**
2020
* Recursively skip the next token. Meant for discarding unwanted/unrecognized properties in a JSON document
2121
*/
22-
suspend fun skipNext()
22+
fun skipNext()
2323

2424
/**
2525
* Peek at the next token type
2626
*/
27-
suspend fun peek(): JsonToken
27+
fun peek(): JsonToken
2828
}
2929

3030
/*
@@ -34,7 +34,7 @@ interface JsonStreamReader {
3434
fun jsonStreamReader(payload: ByteArray): JsonStreamReader = JsonLexer(payload)
3535

3636
// return the next token and require that it be of type [TExpected] or else throw an exception
37-
internal suspend inline fun <reified TExpected : JsonToken> JsonStreamReader.nextTokenOf(): TExpected {
37+
internal inline fun <reified TExpected : JsonToken> JsonStreamReader.nextTokenOf(): TExpected {
3838
val token = this.nextToken()
3939
requireToken<TExpected>(token)
4040
return token as TExpected

0 commit comments

Comments
 (0)