Skip to content

Commit 42723dc

Browse files
committed
8304420: Regression ~11% with Javac-Generates on all platforms in b14
Reviewed-by: vromero
1 parent 19f2edd commit 42723dc

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.EnumMap;
3232
import java.util.Map;
3333
import java.util.Optional;
34+
import java.util.function.Function;
3435
import java.util.function.Predicate;
3536

3637
import javax.lang.model.type.*;
@@ -182,8 +183,7 @@ public boolean isPartial() {
182183
* @return the constant value attribute of this type
183184
*/
184185
public Object constValue() {
185-
return getMetadata(TypeMetadata.ConstantValue.class)
186-
.map(ConstantValue::value).orElse(null);
186+
return getMetadata(TypeMetadata.ConstantValue.class, ConstantValue::value, null);
187187
}
188188

189189
/** Is this a constant type whose value is false?
@@ -361,11 +361,23 @@ public List<TypeMetadata> getMetadata() {
361361
/**
362362
* Get the type metadata of the given kind associated with this type (if any).
363363
*/
364-
public <M extends TypeMetadata> Optional<M> getMetadata(Class<M> metadataClass) {
365-
return metadata.stream()
366-
.filter(m -> metadataClass.isAssignableFrom(m.getClass()))
367-
.map(metadataClass::cast)
368-
.findFirst();
364+
@SuppressWarnings("unchecked")
365+
public <M extends TypeMetadata> M getMetadata(Class<M> metadataClass) {
366+
return getMetadata(metadataClass, Function.identity(), null);
367+
}
368+
369+
/**
370+
* Get the type metadata of the given kind associated with this type (if any),
371+
* and apply the provided mapping function.
372+
*/
373+
@SuppressWarnings("unchecked")
374+
public <M extends TypeMetadata, Z> Z getMetadata(Class<M> metadataClass, Function<M, Z> metadataFunc, Z defaultValue) {
375+
for (TypeMetadata m : metadata) {
376+
if (m.getClass() == metadataClass) {
377+
return metadataFunc.apply((M)m);
378+
}
379+
}
380+
return defaultValue;
369381
}
370382

371383
/**
@@ -374,17 +386,20 @@ public <M extends TypeMetadata> Optional<M> getMetadata(Class<M> metadataClass)
374386
* an exception is thrown.
375387
*/
376388
public Type addMetadata(TypeMetadata md) {
377-
Assert.check(getMetadata(md.getClass()).isEmpty());
378-
return cloneWithMetadata(metadata.append(md));
389+
Assert.check(getMetadata(md.getClass()) == null);
390+
return cloneWithMetadata(metadata.prepend(md));
379391
}
380392

381393
/**
382394
* Create a new copy of this type but without the specified type metadata.
383395
*/
384396
public Type dropMetadata(Class<? extends TypeMetadata> metadataClass) {
385-
List<TypeMetadata> newMetadata = metadata.stream()
386-
.filter(m -> !metadataClass.isAssignableFrom(m.getClass()))
387-
.collect(List.collector());
397+
List<TypeMetadata> newMetadata = List.nil();
398+
for (TypeMetadata m : metadata) {
399+
if (m.getClass() != metadataClass) {
400+
newMetadata = newMetadata.prepend(m);
401+
}
402+
}
388403
return cloneWithMetadata(newMetadata);
389404
}
390405

@@ -442,13 +457,12 @@ public Type annotatedType(final List<Attribute.TypeCompound> annos) {
442457
}
443458

444459
public boolean isAnnotated() {
445-
return getMetadata(TypeMetadata.Annotations.class).isPresent();
460+
return getMetadata(TypeMetadata.Annotations.class) != null;
446461
}
447462

448463
@Override @DefinedBy(Api.LANGUAGE_MODEL)
449464
public List<Attribute.TypeCompound> getAnnotationMirrors() {
450-
return getMetadata(TypeMetadata.Annotations.class)
451-
.map(Annotations::annotations).orElse(List.nil());
465+
return getMetadata(TypeMetadata.Annotations.class, Annotations::annotations, List.nil());
452466
}
453467

454468

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,8 @@ public void annotateTypeSecondStage(JCTree tree, List<JCAnnotation> annotations,
10511051
List<Attribute.TypeCompound> compounds = fromAnnotations(annotations);
10521052
Assert.check(annotations.size() == compounds.size());
10531053
// the type already has annotation metadata, but it's empty
1054-
Annotations metadata = storeAt.getMetadata(Annotations.class).orElseThrow(AssertionError::new);
1054+
Annotations metadata = storeAt.getMetadata(Annotations.class);
1055+
Assert.checkNonNull(metadata);
10551056
Assert.check(metadata.annotationBuffer().isEmpty());
10561057
metadata.annotationBuffer().appendList(compounds);
10571058
});

0 commit comments

Comments
 (0)