From 273889d1a930df8abe851a3cb307c626d794227f Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Wed, 12 Sep 2018 15:34:15 +0300 Subject: [PATCH] Add our own primitive boxing methods to stdlib These methods are very thin wrappers around primitive wrapper classes constructors. They are used by coroutines code which returns primitives and this way HotSpot is able to throw the allocations away completely. #KT-26591 Fixed --- .../kotlin/coroutines/jvm/internal/boxing.kt | 48 +++++++++++++++++++ .../kotlin-stdlib-runtime-merged.txt | 11 +++++ 2 files changed, 59 insertions(+) create mode 100644 libraries/stdlib/coroutines/jvm/src/kotlin/coroutines/jvm/internal/boxing.kt diff --git a/libraries/stdlib/coroutines/jvm/src/kotlin/coroutines/jvm/internal/boxing.kt b/libraries/stdlib/coroutines/jvm/src/kotlin/coroutines/jvm/internal/boxing.kt new file mode 100644 index 00000000000..9d0c2041b6f --- /dev/null +++ b/libraries/stdlib/coroutines/jvm/src/kotlin/coroutines/jvm/internal/boxing.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +@file:JvmName("Boxing") + +@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") + +package kotlin.coroutines.jvm.internal + +/* + * Box primitive to Java wrapper class by allocating the wrapper object. + * + * This allows HotSpot JIT to eliminate allocations completely in coroutines code with primitives. + */ + +@SinceKotlin("1.3") +@PublishedApi +internal fun boxBoolean(primitive: Boolean): java.lang.Boolean = java.lang.Boolean.valueOf(primitive) as java.lang.Boolean + +@SinceKotlin("1.3") +@PublishedApi +internal fun boxByte(primitive: Byte): java.lang.Byte = java.lang.Byte.valueOf(primitive) as java.lang.Byte + +@SinceKotlin("1.3") +@PublishedApi +internal fun boxShort(primitive: Short): java.lang.Short = java.lang.Short(primitive) + +@SinceKotlin("1.3") +@PublishedApi +internal fun boxInt(primitive: Int): java.lang.Integer = java.lang.Integer(primitive) + +@SinceKotlin("1.3") +@PublishedApi +internal fun boxLong(primitive: Long): java.lang.Long = java.lang.Long(primitive) + +@SinceKotlin("1.3") +@PublishedApi +internal fun boxFloat(primitive: Float): java.lang.Float = java.lang.Float(primitive) + +@SinceKotlin("1.3") +@PublishedApi +internal fun boxDouble(primitive: Double): java.lang.Double = java.lang.Double(primitive) + +@SinceKotlin("1.3") +@PublishedApi +internal fun boxChar(primitive: Char): java.lang.Character = java.lang.Character(primitive) diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index a6418948dff..4cdcc6aa49e 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -2852,6 +2852,17 @@ public final class kotlin/coroutines/intrinsics/IntrinsicsKt { public static final fun intercepted (Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation; } +public final class kotlin/coroutines/jvm/internal/Boxing { + public static final fun boxBoolean (Z)Ljava/lang/Boolean; + public static final fun boxByte (B)Ljava/lang/Byte; + public static final fun boxChar (C)Ljava/lang/Character; + public static final fun boxDouble (D)Ljava/lang/Double; + public static final fun boxFloat (F)Ljava/lang/Float; + public static final fun boxInt (I)Ljava/lang/Integer; + public static final fun boxLong (J)Ljava/lang/Long; + public static final fun boxShort (S)Ljava/lang/Short; +} + public abstract interface class kotlin/coroutines/jvm/internal/CoroutineStackFrame { public abstract fun getCallerFrame ()Lkotlin/coroutines/jvm/internal/CoroutineStackFrame; public abstract fun getStackTraceElement ()Ljava/lang/StackTraceElement;