Skip to content

Commit

Permalink
Mark block in corresponding encodeStructure/decodeStructure extension…
Browse files Browse the repository at this point in the history
…s as crossinline to reduce amount of bytecode (#1917)

Fixes #1750
  • Loading branch information
qwwdfsad committed Apr 27, 2022
1 parent 4228656 commit 05f0dfd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
15 changes: 4 additions & 11 deletions core/commonMain/src/kotlinx/serialization/encoding/Decoding.kt
Expand Up @@ -562,19 +562,12 @@ public interface CompositeDecoder {
*/
public inline fun <T> Decoder.decodeStructure(
descriptor: SerialDescriptor,
block: CompositeDecoder.() -> T
crossinline block: CompositeDecoder.() -> T
): T {
val composite = beginStructure(descriptor)
var ex: Throwable? = null
try {
return composite.block()
} catch (e: Throwable) {
ex = e
throw e
} finally {
// End structure only if there is no exception, otherwise it can be swallowed
if (ex == null) composite.endStructure(descriptor)
}
val result = composite.block()
composite.endStructure(descriptor)
return result
}

private const val decodeMethodDeprecated = "Please migrate to decodeElement method which accepts old value." +
Expand Down
24 changes: 9 additions & 15 deletions core/commonMain/src/kotlinx/serialization/encoding/Encoding.kt
Expand Up @@ -473,18 +473,13 @@ public interface CompositeEncoder {
/**
* Begins a structure, encodes it using the given [block] and ends it.
*/
public inline fun Encoder.encodeStructure(descriptor: SerialDescriptor, block: CompositeEncoder.() -> Unit) {
public inline fun Encoder.encodeStructure(
descriptor: SerialDescriptor,
crossinline block: CompositeEncoder.() -> Unit
) {
val composite = beginStructure(descriptor)
var ex: Throwable? = null
try {
composite.block()
} catch (e: Throwable) {
ex = e
throw e
} finally {
// End structure only if there is no exception, otherwise it can be swallowed
if (ex == null) composite.endStructure(descriptor)
}
composite.block()
composite.endStructure(descriptor)
}

/**
Expand All @@ -495,10 +490,9 @@ public inline fun Encoder.encodeCollection(
collectionSize: Int,
crossinline block: CompositeEncoder.() -> Unit
) {
with(beginCollection(descriptor, collectionSize)) {
block()
endStructure(descriptor)
}
val composite = beginCollection(descriptor, collectionSize)
composite.block()
composite.endStructure(descriptor)
}

/**
Expand Down
Expand Up @@ -40,7 +40,7 @@ public abstract class AbstractPolymorphicSerializer<T : Any> internal constructo
var klassName: String? = null
var value: Any? = null
if (decodeSequentially()) {
return decodeSequentially(this)
return@decodeStructure decodeSequentially(this)
}

mainLoop@ while (true) {
Expand Down

0 comments on commit 05f0dfd

Please sign in to comment.