Skip to content

Commit 2d3bd21

Browse files
committed
add more optimistic get() before computeIfAbsent() calls
1 parent 6cbf411 commit 2d3bd21

File tree

6 files changed

+18
-6
lines changed

6 files changed

+18
-6
lines changed

core/src/main/java/org/jboss/jandex/AnnotationOverlayImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ public final Collection<AnnotationInstance> annotations(Declaration declaration)
243243

244244
Collection<AnnotationInstance> getAnnotationsFor(Declaration declaration) {
245245
EquivalenceKey key = EquivalenceKey.of(declaration);
246+
// optimistic `get` to avoid `computeIfAbsent` for most calls
247+
Collection<AnnotationInstance> result = overlay.get(key);
248+
if (result != null) {
249+
return result;
250+
}
246251
return overlay.computeIfAbsent(key, new Function<EquivalenceKey, Collection<AnnotationInstance>>() {
247252
@Override
248253
public Collection<AnnotationInstance> apply(EquivalenceKey ignored) {

core/src/main/java/org/jboss/jandex/DotName.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,11 @@ public String apply(DotName name) {
354354
public String toString(char delim) {
355355
if (componentized) {
356356
if (delim == '.' && startsWithJava()) {
357-
// optimistic get to avoid computeIfAbsent for most calls
357+
// optimistic `get` to avoid `computeIfAbsent` for most calls
358358
String name = JAVA_STRINGS.get(this);
359359
if (name != null) {
360360
return name;
361361
}
362-
363362
return JAVA_STRINGS.computeIfAbsent(this, COMPONENTIZED_TO_STRING);
364363
}
365364
StringBuilder builder = new StringBuilder(stringLength());

core/src/main/java/org/jboss/jandex/EquivalenceKey.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,11 @@ public ClassTypeEquivalenceKey apply(DotName name) {
519519

520520
private static ClassTypeEquivalenceKey of(DotName name) {
521521
if (name.startsWithJava()) {
522-
// optimistic get to avoid computeIfAbsent for most calls
522+
// optimistic `get` to avoid `computeIfAbsent` for most calls
523523
ClassTypeEquivalenceKey equivalenceKey = JAVA_INSTANCES.get(name);
524524
if (equivalenceKey != null) {
525525
return equivalenceKey;
526526
}
527-
528527
return JAVA_INSTANCES.computeIfAbsent(name, FACTORY);
529528
}
530529
return new ClassTypeEquivalenceKey(name);

core/src/main/java/org/jboss/jandex/MethodSignatureKey.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public MethodSignatureKey apply(MethodInfo method) {
6565

6666
static MethodSignatureKey of(MethodInfo method) {
6767
if (method.declaringClass().name().startsWithJava()) {
68+
// optimistic `get` to avoid `computeIfAbsent` for most calls
69+
MethodSignatureKey key = JAVA_METHOD_SIGNATURES.get(method);
70+
if (key != null) {
71+
return key;
72+
}
6873
return JAVA_METHOD_SIGNATURES.computeIfAbsent(method, CREATE_METHOD_SIGNATURE);
6974
}
7075
return CREATE_METHOD_SIGNATURE.apply(method);

core/src/main/java/org/jboss/jandex/MutableAnnotationOverlayImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ final class MutableAnnotationOverlayImpl extends AnnotationOverlayImpl implement
1818
@Override
1919
Collection<AnnotationInstance> getAnnotationsFor(Declaration declaration) {
2020
EquivalenceKey key = EquivalenceKey.of(declaration);
21+
// optimistic `get` to avoid `computeIfAbsent` for most calls
22+
Collection<AnnotationInstance> result = overlay.get(key);
23+
if (result != null) {
24+
return result;
25+
}
2126
return overlay.computeIfAbsent(key, new Function<EquivalenceKey, Collection<AnnotationInstance>>() {
2227
@Override
2328
public Collection<AnnotationInstance> apply(EquivalenceKey ignored) {

gizmo2/src/main/java/org/jboss/jandex/gizmo2/Jandex2Gizmo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ public class Jandex2Gizmo {
5959
* @param name the Jandex {@code DotName} (must not be {@code null})
6060
*/
6161
public static ClassDesc classDescOf(DotName name) {
62-
// optimistic get to avoid computeIfAbsent for most calls
62+
// optimistic `get` to avoid `computeIfAbsent` for most calls
6363
ClassDesc classDesc = CLASS_DESC_CACHE.get(name);
6464
if (classDesc != null) {
6565
return classDesc;
6666
}
67-
6867
return CLASS_DESC_CACHE.computeIfAbsent(name, nameParam -> {
6968
if (nameParam.prefix() == null) {
7069
String local = nameParam.local();

0 commit comments

Comments
 (0)