-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Make some constant SubscribableListener instances cheaper #124452
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 1 commit
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 |
|---|---|---|
|
|
@@ -110,20 +110,6 @@ public SubscribableListener() { | |
| this(EMPTY); | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link SubscribableListener} which has already succeeded with the given result. | ||
| */ | ||
| public static <T> SubscribableListener<T> newSucceeded(T result) { | ||
| return new SubscribableListener<>(new SuccessResult<>(result)); | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link SubscribableListener} which has already failed with the given exception. | ||
| */ | ||
| public static <T> SubscribableListener<T> newFailed(Exception exception) { | ||
| return new SubscribableListener<>(new FailureResult(exception, exception)); | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link SubscribableListener}, fork a computation to complete it, and return the listener. If the forking itself throws an | ||
| * exception then the exception is caught and fed to the returned listener. | ||
|
|
@@ -586,4 +572,33 @@ private Runnable scheduleTimeout(TimeValue timeout, ThreadPool threadPool, Execu | |
| private Object compareAndExchangeState(Object expectedValue, Object newValue) { | ||
| return VH_STATE_FIELD.compareAndExchange(this, expectedValue, newValue); | ||
| } | ||
|
|
||
| @SuppressWarnings("rawtypes") | ||
| private static final SubscribableListener NULL_SUCCESS = newSucceeded(null); | ||
|
|
||
| /** | ||
| * Same as {@link #newSucceeded(Object)} but always returns the same instance with result value {@code null}. | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static <T> SubscribableListener<T> nullSuccess() { | ||
| return NULL_SUCCESS; | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link SubscribableListener} which has already succeeded with the given result. | ||
| */ | ||
| public static <T> SubscribableListener<T> newSucceeded(T result) { | ||
| var res = new SubscribableListener<T>(); | ||
| VH_STATE_FIELD.setRelease(res, new SuccessResult<>(result)); | ||
|
||
| return res; | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link SubscribableListener} which has already failed with the given exception. | ||
| */ | ||
| public static <T> SubscribableListener<T> newFailed(Exception exception) { | ||
| var res = new SubscribableListener<T>(); | ||
| VH_STATE_FIELD.setRelease(res, new FailureResult(exception, exception)); | ||
| return res; | ||
| } | ||
| } | ||
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 allocates a
new Object()for the initial state, can we find a way not to do that?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.
Ah 🤦 gotta fix that too, otherwise this is kinda pointless. Sec on it
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.
Alright, not sure you'll like this one :) But if I get rid of
EMPTYthis all falls into place just fine doesn't it?