IR: optimize IrDeclarationWithName.fqNameWhenAvailable
Do not construct lots of FqName objects, since doing so takes ~1% of JVM backend time.
This commit is contained in:
committed by
Space Team
parent
f1886f8219
commit
29fab6c3eb
@@ -316,13 +316,29 @@ val IrClass.isClass get() = kind == ClassKind.CLASS
|
||||
val IrClass.isObject get() = kind == ClassKind.OBJECT
|
||||
val IrClass.isAnonymousObject get() = isClass && name == SpecialNames.NO_NAME_PROVIDED
|
||||
val IrClass.isNonCompanionObject: Boolean get() = isObject && !isCompanion
|
||||
|
||||
val IrDeclarationWithName.fqNameWhenAvailable: FqName?
|
||||
get() = when (val parent = parent) {
|
||||
is IrDeclarationWithName -> parent.fqNameWhenAvailable?.child(name)
|
||||
is IrPackageFragment -> parent.packageFqName.child(name)
|
||||
else -> null
|
||||
get() {
|
||||
val sb = StringBuilder()
|
||||
return if (computeFqNameString(this, sb)) FqName(sb.toString()) else null
|
||||
}
|
||||
|
||||
private fun computeFqNameString(declaration: IrDeclarationWithName, result: StringBuilder): Boolean {
|
||||
when (val parent = declaration.parent) {
|
||||
is IrDeclarationWithName -> {
|
||||
if (!computeFqNameString(parent, result)) return false
|
||||
}
|
||||
is IrPackageFragment -> {
|
||||
val packageFqName = parent.packageFqName
|
||||
if (!packageFqName.isRoot) result.append(packageFqName)
|
||||
}
|
||||
else -> return false
|
||||
}
|
||||
if (result.isNotEmpty()) result.append('.')
|
||||
result.append(declaration.name.asString())
|
||||
return true
|
||||
}
|
||||
|
||||
val IrDeclaration.parentAsClass: IrClass
|
||||
get() = parent as? IrClass
|
||||
?: error("Parent of this declaration is not a class: ${render()}")
|
||||
|
||||
Reference in New Issue
Block a user