From 08a9e7eae9389e6c0d5096b876d4cae3ddaa3eca Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Tue, 5 Sep 2023 15:46:19 +0200 Subject: [PATCH] [IR] Outline the main logic in annotation-related helpers Prefer smaller inline functions, because otherwise if they become hot spots, they will complicate performance analysis, and actually might make everything slower because of low inlining threshold in HotSpot. --- .../org/jetbrains/kotlin/ir/util/IrUtils.kt | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt index a4707e444f3..32276d3cddd 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt @@ -381,18 +381,27 @@ fun IrConstructorCall.getAnnotationStringValue(name: String): String { return (getValueArgument(parameter.index) as IrConst<*>).value as String } -inline fun IrConstructorCall.getAnnotationValueOrNull(name: String): T? { +inline fun IrConstructorCall.getAnnotationValueOrNull(name: String): T? = + getAnnotationValueOrNullImpl(name) as T? + +@PublishedApi +internal fun IrConstructorCall.getAnnotationValueOrNullImpl(name: String): Any? { val parameter = symbol.owner.valueParameters.atMostOne { it.name.asString() == name } - return parameter?.let { getValueArgument(it.index)?.let { (it as IrConst<*>).value as T } } + val argument = parameter?.let { getValueArgument(it.index) } + return (argument as IrConst<*>?)?.value } -inline fun IrDeclaration.getAnnotationArgumentValue(fqName: FqName, argumentName: String): T? { +inline fun IrDeclaration.getAnnotationArgumentValue(fqName: FqName, argumentName: String): T? = + getAnnotationArgumentValueImpl(fqName, argumentName) as T? + +@PublishedApi +internal fun IrDeclaration.getAnnotationArgumentValueImpl(fqName: FqName, argumentName: String): Any? { val annotation = this.annotations.findAnnotation(fqName) ?: return null for (index in 0 until annotation.valueArgumentsCount) { val parameter = annotation.symbol.owner.valueParameters[index] - if (parameter.name == Name.identifier(argumentName)) { - val actual = annotation.getValueArgument(index) as? IrConst<*> ?: return null - return actual.value as T + if (parameter.name.asString() == argumentName) { + val actual = annotation.getValueArgument(index) as? IrConst<*> + return actual?.value } } return null