Optimize Annotations.isEmpty in some implementations

This commit is contained in:
Alexander Udalov
2017-07-04 13:49:50 +03:00
parent 9ee4b39e1b
commit 7ec67505c5
4 changed files with 27 additions and 16 deletions
@@ -17,8 +17,8 @@
package org.jetbrains.kotlin.descriptors.annotations
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
interface Annotated {
val annotations: Annotations
@@ -74,8 +74,7 @@ interface Annotations : Iterable<AnnotationDescriptor> {
}
fun checkAnnotationName(annotation: AnnotationDescriptor, fqName: FqName): Boolean {
val descriptor = annotation.annotationClass
return descriptor != null && fqName.toUnsafe() == DescriptorUtils.getFqName(descriptor)
return annotation.annotationClass?.fqNameUnsafe == fqName.toUnsafe()
}
class FilteredAnnotations(
@@ -103,16 +102,14 @@ class FilteredAnnotations(
return delegate.getAllAnnotations().filter { shouldBeReturned(it.annotation) }
}
override fun iterator() = delegate.filter { shouldBeReturned(it) }.iterator()
override fun iterator() = delegate.filter(this::shouldBeReturned).iterator()
private fun shouldBeReturned(annotation: AnnotationDescriptor): Boolean {
val descriptor = annotation.annotationClass
return descriptor != null && DescriptorUtils.getFqName(descriptor).let { fqName ->
fqName.isSafe && fqNameFilter(fqName.toSafe())
}
}
override fun isEmpty() = delegate.any(this::shouldBeReturned)
override fun isEmpty() = !iterator().hasNext()
private fun shouldBeReturned(annotation: AnnotationDescriptor): Boolean =
annotation.annotationClass?.fqNameUnsafe.let { fqName ->
fqName != null && fqName.isSafe && fqNameFilter(fqName.toSafe())
}
}
class CompositeAnnotations(
@@ -222,7 +222,7 @@ class MemberDeserializer(private val c: DeserializationContext) {
if (!Flags.HAS_ANNOTATIONS.get(flags)) {
return Annotations.EMPTY
}
return DeserializedAnnotationsWithPossibleTargets(c.storageManager) {
return NonEmptyDeserializedAnnotationsWithPossibleTargets(c.storageManager) {
c.containingDeclaration.asProtoContainer()?.let {
c.components.annotationAndConstantLoader.loadCallableAnnotations(it, proto, kind).toList()
}.orEmpty()
@@ -255,7 +255,7 @@ class MemberDeserializer(private val c: DeserializationContext) {
return valueParameters.mapIndexed { i, proto ->
val flags = if (proto.hasFlags()) proto.flags else 0
val annotations = if (containerOfCallable != null && Flags.HAS_ANNOTATIONS.get(flags)) {
DeserializedAnnotations(c.storageManager) {
NonEmptyDeserializedAnnotations(c.storageManager) {
c.components.annotationAndConstantLoader
.loadValueParameterAnnotations(containerOfCallable, callable, kind, i, proto)
.toList()
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
class DeserializedAnnotations(
open class DeserializedAnnotations(
storageManager: StorageManager,
compute: () -> List<AnnotationDescriptor>
) : Annotations {
@@ -43,7 +43,14 @@ class DeserializedAnnotations(
override fun iterator(): Iterator<AnnotationDescriptor> = annotations.iterator()
}
class DeserializedAnnotationsWithPossibleTargets(
class NonEmptyDeserializedAnnotations(
storageManager: StorageManager,
compute: () -> List<AnnotationDescriptor>
) : DeserializedAnnotations(storageManager, compute) {
override fun isEmpty(): Boolean = false
}
open class DeserializedAnnotationsWithPossibleTargets(
storageManager: StorageManager,
compute: () -> List<AnnotationWithTarget>
) : Annotations {
@@ -64,3 +71,10 @@ class DeserializedAnnotationsWithPossibleTargets(
return annotations.asSequence().filter { it.target == null }.map { it.annotation }.iterator()
}
}
class NonEmptyDeserializedAnnotationsWithPossibleTargets(
storageManager: StorageManager,
compute: () -> List<AnnotationWithTarget>
) : DeserializedAnnotationsWithPossibleTargets(storageManager, compute) {
override fun isEmpty(): Boolean = false
}
@@ -80,7 +80,7 @@ class DeserializedClassDescriptor(
if (!Flags.HAS_ANNOTATIONS.get(classProto.flags)) {
Annotations.EMPTY
}
else DeserializedAnnotations(c.storageManager) {
else NonEmptyDeserializedAnnotations(c.storageManager) {
c.components.annotationAndConstantLoader.loadClassAnnotations(thisAsProtoContainer).toList()
}