Skip to content

Commit d8e11ba

Browse files
committed
Cache enclosed descriptors by name
1 parent 163e0c4 commit d8e11ba

File tree

1 file changed

+39
-25
lines changed
  • compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl

1 file changed

+39
-25
lines changed

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,12 @@ class ResolverImpl(
524524
is PsiMethod -> {
525525
// TODO: get rid of hardcoded check if possible.
526526
val property = if (psi.name.startsWith("set") || psi.name.startsWith("get")) {
527+
val propName = psi.name.substring(3).replaceFirstChar(Char::lowercaseChar)
527528
moduleClassResolver
528529
.resolveContainingClass(psi)
529530
?.findEnclosedDescriptor(
530-
kindFilter = DescriptorKindFilter.CALLABLES
531+
kindFilter = DescriptorKindFilter.CALLABLES,
532+
name = propName
531533
) {
532534
(it as? PropertyDescriptor)?.getter?.findPsi() == psi ||
533535
(it as? PropertyDescriptor)?.setter?.findPsi() == psi
@@ -542,6 +544,7 @@ class ResolverImpl(
542544
}
543545
containingClass.findEnclosedDescriptor(
544546
kindFilter = DescriptorKindFilter.FUNCTIONS,
547+
name = if (psi.name == containingClass.name.asString()) "<init>" else psi.name,
545548
filter = filter
546549
)
547550
}
@@ -551,6 +554,7 @@ class ResolverImpl(
551554
.resolveClass(JavaFieldImpl(psi).containingClass)
552555
?.findEnclosedDescriptor(
553556
kindFilter = DescriptorKindFilter.VARIABLES,
557+
name = psi.name,
554558
filter = { it.findPsi() == psi }
555559
)
556560
}
@@ -747,6 +751,7 @@ class ResolverImpl(
747751
moduleClassResolver.resolveContainingClass(owner)
748752
?.findEnclosedDescriptor(
749753
kindFilter = DescriptorKindFilter.FUNCTIONS,
754+
name = owner.name,
750755
filter = { it.findPsi() == owner }
751756
) as FunctionDescriptor
752757
} as DeclarationDescriptor
@@ -1403,6 +1408,39 @@ class ResolverImpl(
14031408
}.toMap()
14041409

14051410
internal fun findPsiJavaFile(path: String): PsiFile? = psiJavaFiles.get(path)
1411+
1412+
// Always construct the key on the fly, so that it and value can be reclaimed any time.
1413+
private val contributedDescriptorsCache =
1414+
WeakHashMap<Pair<MemberScope, DescriptorKindFilter>, Map<String, List<DeclarationDescriptor>>>()
1415+
1416+
private inline fun MemberScope.findEnclosedDescriptor(
1417+
kindFilter: DescriptorKindFilter,
1418+
name: String,
1419+
crossinline filter: (DeclarationDescriptor) -> Boolean,
1420+
): DeclarationDescriptor? {
1421+
val nameToDescriptors = contributedDescriptorsCache.computeIfAbsent(Pair(this, kindFilter)) {
1422+
getContributedDescriptors(kindFilter).groupBy { it.name.asString() }
1423+
}
1424+
return nameToDescriptors.get(name)?.firstOrNull(filter)
1425+
}
1426+
1427+
private inline fun ClassDescriptor.findEnclosedDescriptor(
1428+
kindFilter: DescriptorKindFilter,
1429+
name: String,
1430+
crossinline filter: (DeclarationDescriptor) -> Boolean,
1431+
): DeclarationDescriptor? {
1432+
return this.unsubstitutedMemberScope.findEnclosedDescriptor(
1433+
kindFilter = kindFilter,
1434+
name = name,
1435+
filter = filter
1436+
) ?: this.staticScope.findEnclosedDescriptor(
1437+
kindFilter = kindFilter,
1438+
name = name,
1439+
filter = filter
1440+
) ?: constructors.firstOrNull {
1441+
kindFilter.accepts(it) && filter(it)
1442+
}
1443+
}
14061444
}
14071445

14081446
// TODO: cross module resolution
@@ -1470,30 +1508,6 @@ private fun Name.getNonSpecialIdentifier(): String {
14701508
}
14711509
}
14721510

1473-
private inline fun MemberScope.findEnclosedDescriptor(
1474-
kindFilter: DescriptorKindFilter,
1475-
crossinline filter: (DeclarationDescriptor) -> Boolean,
1476-
): DeclarationDescriptor? {
1477-
return getContributedDescriptors(
1478-
kindFilter = kindFilter
1479-
).firstOrNull(filter)
1480-
}
1481-
1482-
private inline fun ClassDescriptor.findEnclosedDescriptor(
1483-
kindFilter: DescriptorKindFilter,
1484-
crossinline filter: (DeclarationDescriptor) -> Boolean,
1485-
): DeclarationDescriptor? {
1486-
return this.unsubstitutedMemberScope.findEnclosedDescriptor(
1487-
kindFilter = kindFilter,
1488-
filter = filter
1489-
) ?: this.staticScope.findEnclosedDescriptor(
1490-
kindFilter = kindFilter,
1491-
filter = filter
1492-
) ?: constructors.firstOrNull {
1493-
kindFilter.accepts(it) && filter(it)
1494-
}
1495-
}
1496-
14971511
internal fun KSAnnotated.findAnnotationFromUseSiteTarget(): Sequence<KSAnnotation> {
14981512
return when (this) {
14991513
is KSPropertyGetter -> (this.receiver as? KSDeclarationImpl)?.let {

0 commit comments

Comments
 (0)