Is there a better (meaning faster) way than the co...
# compiler
j
Is there a better (meaning faster) way than the code below to get all top level extension functions on a certain
KotlinType
? It works but it is pretty slow as it fetches all top level extensions that are available (even the ones not imported). I can add restriction on the package and only get imported functions if it can help make things faster. Thanks a lot 🙂
Copy code
fun getExtensionsOnType(type: KotlinType, scope: LexicalScope): List<DeclarationDescriptor> {
  val extensionKindFilter = kindFilter exclude DescriptorKindExclude.NonExtensions
  return scope.collectDescriptorsFiltered(extensionKindFilter, nameFilter)
    .filter {
      val receiverType = (it as? CallableDescriptor)?.extensionReceiverParameter?.value?.type
      receiverType != null && this.isSubtypeOf(receiverType)
    }
}
r
Did you profile the time it takes to traverse the tree and filter? If these are already computed descriptors it’s gonna be faster than analysing a partial chunk of the tree but if they are not computed then it may run analysis. Also this is subjective whether you are doing this for the CLI compiler or running this in the Kotlin Plugin in idea.
In the CLI the time you take it will be minimal since you can compute this ahead of compilation but in the IDE it will most likely be in a loop in analysis typechecking and getting those descriptors continually as you type.