8304420: Regression ~11% with Javac-Generates on all platforms in b14

Reviewed-by: vromero
This commit is contained in:
Maurizio Cimadamore 2023-03-20 18:44:20 +00:00
parent 19f2edd9b7
commit 42723dcb18
2 changed files with 31 additions and 16 deletions

View File

@ -31,6 +31,7 @@ import java.util.Collections;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import javax.lang.model.type.*; import javax.lang.model.type.*;
@ -182,8 +183,7 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
* @return the constant value attribute of this type * @return the constant value attribute of this type
*/ */
public Object constValue() { public Object constValue() {
return getMetadata(TypeMetadata.ConstantValue.class) return getMetadata(TypeMetadata.ConstantValue.class, ConstantValue::value, null);
.map(ConstantValue::value).orElse(null);
} }
/** Is this a constant type whose value is false? /** Is this a constant type whose value is false?
@ -361,11 +361,23 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
/** /**
* Get the type metadata of the given kind associated with this type (if any). * Get the type metadata of the given kind associated with this type (if any).
*/ */
public <M extends TypeMetadata> Optional<M> getMetadata(Class<M> metadataClass) { @SuppressWarnings("unchecked")
return metadata.stream() public <M extends TypeMetadata> M getMetadata(Class<M> metadataClass) {
.filter(m -> metadataClass.isAssignableFrom(m.getClass())) return getMetadata(metadataClass, Function.identity(), null);
.map(metadataClass::cast) }
.findFirst();
/**
* Get the type metadata of the given kind associated with this type (if any),
* and apply the provided mapping function.
*/
@SuppressWarnings("unchecked")
public <M extends TypeMetadata, Z> Z getMetadata(Class<M> metadataClass, Function<M, Z> metadataFunc, Z defaultValue) {
for (TypeMetadata m : metadata) {
if (m.getClass() == metadataClass) {
return metadataFunc.apply((M)m);
}
}
return defaultValue;
} }
/** /**
@ -374,17 +386,20 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
* an exception is thrown. * an exception is thrown.
*/ */
public Type addMetadata(TypeMetadata md) { public Type addMetadata(TypeMetadata md) {
Assert.check(getMetadata(md.getClass()).isEmpty()); Assert.check(getMetadata(md.getClass()) == null);
return cloneWithMetadata(metadata.append(md)); return cloneWithMetadata(metadata.prepend(md));
} }
/** /**
* Create a new copy of this type but without the specified type metadata. * Create a new copy of this type but without the specified type metadata.
*/ */
public Type dropMetadata(Class<? extends TypeMetadata> metadataClass) { public Type dropMetadata(Class<? extends TypeMetadata> metadataClass) {
List<TypeMetadata> newMetadata = metadata.stream() List<TypeMetadata> newMetadata = List.nil();
.filter(m -> !metadataClass.isAssignableFrom(m.getClass())) for (TypeMetadata m : metadata) {
.collect(List.collector()); if (m.getClass() != metadataClass) {
newMetadata = newMetadata.prepend(m);
}
}
return cloneWithMetadata(newMetadata); return cloneWithMetadata(newMetadata);
} }
@ -442,13 +457,12 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
} }
public boolean isAnnotated() { public boolean isAnnotated() {
return getMetadata(TypeMetadata.Annotations.class).isPresent(); return getMetadata(TypeMetadata.Annotations.class) != null;
} }
@Override @DefinedBy(Api.LANGUAGE_MODEL) @Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Attribute.TypeCompound> getAnnotationMirrors() { public List<Attribute.TypeCompound> getAnnotationMirrors() {
return getMetadata(TypeMetadata.Annotations.class) return getMetadata(TypeMetadata.Annotations.class, Annotations::annotations, List.nil());
.map(Annotations::annotations).orElse(List.nil());
} }

View File

@ -1051,7 +1051,8 @@ public class Annotate {
List<Attribute.TypeCompound> compounds = fromAnnotations(annotations); List<Attribute.TypeCompound> compounds = fromAnnotations(annotations);
Assert.check(annotations.size() == compounds.size()); Assert.check(annotations.size() == compounds.size());
// the type already has annotation metadata, but it's empty // the type already has annotation metadata, but it's empty
Annotations metadata = storeAt.getMetadata(Annotations.class).orElseThrow(AssertionError::new); Annotations metadata = storeAt.getMetadata(Annotations.class);
Assert.checkNonNull(metadata);
Assert.check(metadata.annotationBuffer().isEmpty()); Assert.check(metadata.annotationBuffer().isEmpty());
metadata.annotationBuffer().appendList(compounds); metadata.annotationBuffer().appendList(compounds);
}); });