-
Notifications
You must be signed in to change notification settings - Fork 94
Short circuit value cache look up to avoid object allocations #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package org.dataloader.impl; | ||
|
||
|
||
import org.dataloader.Try; | ||
import org.dataloader.ValueCache; | ||
import org.dataloader.annotations.Internal; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
|
@@ -20,37 +22,55 @@ | |
@Internal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe promote it to public? We expose a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The NOOP is a default impl for never caching. I want it to stay that way. There is no real useful code here honestly for library consumers |
||
public class NoOpValueCache<K, V> implements ValueCache<K, V> { | ||
|
||
public static NoOpValueCache<?, ?> NOOP = new NoOpValueCache<>(); | ||
/** | ||
* a no op value cache instance | ||
*/ | ||
public static final NoOpValueCache<?, ?> NOOP = new NoOpValueCache<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we make it public: |
||
|
||
// avoid object allocation by using a final field | ||
private final ValueCachingNotSupported NOT_SUPPORTED = new ValueCachingNotSupported(); | ||
private final CompletableFuture<V> NOT_SUPPORTED_CF = CompletableFutureKit.failedFuture(NOT_SUPPORTED); | ||
private final CompletableFuture<Void> NOT_SUPPORTED_VOID_CF = CompletableFuture.completedFuture(null); | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public CompletableFuture<V> get(K key) { | ||
return CompletableFutureKit.failedFuture(new UnsupportedOperationException()); | ||
return NOT_SUPPORTED_CF; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<Try<V>>> getValues(List<K> keys) throws ValueCachingNotSupported { | ||
throw NOT_SUPPORTED; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public CompletableFuture<V> set(K key, V value) { | ||
return CompletableFuture.completedFuture(value); | ||
return NOT_SUPPORTED_CF; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<V>> setValues(List<K> keys, List<V> values) throws ValueCachingNotSupported { | ||
throw NOT_SUPPORTED; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public CompletableFuture<Void> delete(K key) { | ||
return CompletableFuture.completedFuture(null); | ||
return NOT_SUPPORTED_VOID_CF; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public CompletableFuture<Void> clear() { | ||
return CompletableFuture.completedFuture(null); | ||
return NOT_SUPPORTED_VOID_CF; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is magic - the exception has no stack trace (we don't want one) AND its fast to allocate because of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it need it to be synchronized?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No - it is in the base class. I will change this