Skip to content

Commit 9c6f676

Browse files
authored
Reduce chance ClassLoaderValue.computeValue() will be called but discarded (#34)
1 parent 88598cf commit 9c6f676

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

utils/src/main/java/datadog/instrument/utils/ClassLoaderValue.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ protected ClassLoaderValue() {
5252
/**
5353
* Computes the given class-loaders's derived value for this {@code ClassLoaderValue}.
5454
*
55-
* <p>This method will be invoked within the first thread that accesses the value with the {@link
56-
* #get} method. Normally, this method is invoked at most once per class-loader, but it may be
57-
* invoked again if there has been a call to {@link #remove remove}.
55+
* <p>This method will be invoked within the first thread that attempts to access the value with
56+
* the {@link #get} method. Normally, this method is invoked at most once per class-loader, but it
57+
* may be invoked again if there has been a call to {@link #remove remove}.
5858
*
5959
* <p>If this method throws an exception, the corresponding call to {@code get} will terminate
6060
* abnormally with that exception, and no class-loader value will be recorded.
@@ -94,8 +94,8 @@ public final V get(ClassLoader cl) {
9494
* Removes the associated value for the given class-loader.
9595
*
9696
* <p>If this is subsequently {@linkplain #get read} for the same class-loader, its value will be
97-
* reinitialized by invoking its {@link #computeValue computeValue} method. This may result in an
98-
* additional invocation of the {@code computeValue} method for the given class-loader.
97+
* reinitialized by invoking its {@link #computeValue} method. This may result in an additional
98+
* invocation of the {@code computeValue} method for the given class-loader.
9999
*
100100
* @param cl the class-loader whose value must be removed
101101
*/
@@ -146,7 +146,7 @@ final int size() {
146146
/**
147147
* Removes stale entries from {@code ClassLoaderValue}s, where the class-loader is now unused.
148148
*
149-
* <p>It is the caller's responsibility to decide how often to call {@code #removeStaleEntries}.
149+
* <p>It is responsibility of the caller to decide how often to call {@code removeStaleEntries}.
150150
* It may be periodically with a background thread, on certain requests, or some other condition.
151151
*/
152152
public static void removeStaleEntries() {
@@ -177,16 +177,17 @@ private V getSystemValue() {
177177
return value;
178178
}
179179

180+
/** Helper to make {@code computeValue} compatible with {@code computeIfAbsent}. */
181+
private V computeValueForKey(ClassLoaderKey key) {
182+
return computeValue(key.get());
183+
}
184+
180185
/** Lazily associate a computed value with a custom class-loader. */
181186
private V getOtherValue(ClassLoader cl) {
182187
//noinspection All: intentionally use lookup key without reference overhead
183188
V value = otherValues.get(new LookupKey(cl));
184189
if (value == null) {
185-
value = computeValue(cl);
186-
V existingValue = otherValues.putIfAbsent(getClassLoaderKey(cl), value);
187-
if (existingValue != null) {
188-
value = existingValue;
189-
}
190+
value = otherValues.computeIfAbsent(getClassLoaderKey(cl), this::computeValueForKey);
190191
}
191192
return value;
192193
}

0 commit comments

Comments
 (0)