Skip to content

Commit 05f0dfd

Browse files
authored
Mark block in corresponding encodeStructure/decodeStructure extensions as crossinline to reduce amount of bytecode (#1917)
Fixes #1750
1 parent 4228656 commit 05f0dfd

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

core/commonMain/src/kotlinx/serialization/encoding/Decoding.kt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -562,19 +562,12 @@ public interface CompositeDecoder {
562562
*/
563563
public inline fun <T> Decoder.decodeStructure(
564564
descriptor: SerialDescriptor,
565-
block: CompositeDecoder.() -> T
565+
crossinline block: CompositeDecoder.() -> T
566566
): T {
567567
val composite = beginStructure(descriptor)
568-
var ex: Throwable? = null
569-
try {
570-
return composite.block()
571-
} catch (e: Throwable) {
572-
ex = e
573-
throw e
574-
} finally {
575-
// End structure only if there is no exception, otherwise it can be swallowed
576-
if (ex == null) composite.endStructure(descriptor)
577-
}
568+
val result = composite.block()
569+
composite.endStructure(descriptor)
570+
return result
578571
}
579572

580573
private const val decodeMethodDeprecated = "Please migrate to decodeElement method which accepts old value." +

core/commonMain/src/kotlinx/serialization/encoding/Encoding.kt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -473,18 +473,13 @@ public interface CompositeEncoder {
473473
/**
474474
* Begins a structure, encodes it using the given [block] and ends it.
475475
*/
476-
public inline fun Encoder.encodeStructure(descriptor: SerialDescriptor, block: CompositeEncoder.() -> Unit) {
476+
public inline fun Encoder.encodeStructure(
477+
descriptor: SerialDescriptor,
478+
crossinline block: CompositeEncoder.() -> Unit
479+
) {
477480
val composite = beginStructure(descriptor)
478-
var ex: Throwable? = null
479-
try {
480-
composite.block()
481-
} catch (e: Throwable) {
482-
ex = e
483-
throw e
484-
} finally {
485-
// End structure only if there is no exception, otherwise it can be swallowed
486-
if (ex == null) composite.endStructure(descriptor)
487-
}
481+
composite.block()
482+
composite.endStructure(descriptor)
488483
}
489484

490485
/**
@@ -495,10 +490,9 @@ public inline fun Encoder.encodeCollection(
495490
collectionSize: Int,
496491
crossinline block: CompositeEncoder.() -> Unit
497492
) {
498-
with(beginCollection(descriptor, collectionSize)) {
499-
block()
500-
endStructure(descriptor)
501-
}
493+
val composite = beginCollection(descriptor, collectionSize)
494+
composite.block()
495+
composite.endStructure(descriptor)
502496
}
503497

504498
/**

core/commonMain/src/kotlinx/serialization/internal/AbstractPolymorphicSerializer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public abstract class AbstractPolymorphicSerializer<T : Any> internal constructo
4040
var klassName: String? = null
4141
var value: Any? = null
4242
if (decodeSequentially()) {
43-
return decodeSequentially(this)
43+
return@decodeStructure decodeSequentially(this)
4444
}
4545

4646
mainLoop@ while (true) {

0 commit comments

Comments
 (0)