Remove reference to temporary thread local explicitly (KT-28940)

There're issue with storing variables in memory longer than needed.
This commit is contained in:
Nikolay Krasko
2019-10-22 19:35:43 +03:00
parent 588218658c
commit 1b96e14b1e
@@ -404,13 +404,8 @@ public class LockBasedStorageManager implements StorageManager {
} }
private static abstract class LockBasedLazyValueWithPostCompute<T> extends LockBasedLazyValue<T> { private static abstract class LockBasedLazyValueWithPostCompute<T> extends LockBasedLazyValue<T> {
@NotNull @Nullable
private final ThreadLocal<Object> valuePostCompute = new ThreadLocal<Object>() { private volatile ThreadLocal<Object> valuePostCompute = null;
@Override
protected Object initialValue() {
return NotValue.NOT_COMPUTED;
}
};
public LockBasedLazyValueWithPostCompute( public LockBasedLazyValueWithPostCompute(
@NotNull LockBasedStorageManager storageManager, @NotNull LockBasedStorageManager storageManager,
@@ -421,10 +416,13 @@ public class LockBasedStorageManager implements StorageManager {
@Override @Override
public T invoke() { public T invoke() {
Object _value = valuePostCompute.get(); ThreadLocal<Object> postComputeCache = valuePostCompute;
if (!(_value instanceof NotValue)) { if (postComputeCache != null) {
// This thread is counting the value so allow an early publication Object _value = postComputeCache.get();
return WrappedValues.unescapeThrowable(_value); if (!(_value instanceof NotValue)) {
// This thread is counting the value so allow an early publication
return WrappedValues.unescapeThrowable(_value);
}
} }
return super.invoke(); return super.invoke();
@@ -433,11 +431,21 @@ public class LockBasedStorageManager implements StorageManager {
// Doing something in post-compute helps prevent infinite recursion // Doing something in post-compute helps prevent infinite recursion
@Override @Override
protected final void postCompute(T value) { protected final void postCompute(T value) {
valuePostCompute.set(value); ThreadLocal<Object> postComputeCache = new ThreadLocal<Object>() {
@Override
protected Object initialValue() {
return NotValue.NOT_COMPUTED;
}
};
postComputeCache.set(value);
try { try {
valuePostCompute = postComputeCache;
doPostCompute(value); doPostCompute(value);
} finally { } finally {
valuePostCompute.remove(); postComputeCache.remove();
valuePostCompute = null;
} }
} }