Skip to content

Commit

Permalink
Don't allocate multiple ImmediateCancelledFuture instances if cancell…
Browse files Browse the repository at this point in the history
…ation causes are not enabled

PiperOrigin-RevId: 431542091
  • Loading branch information
java-team-github-bot authored and Google Java Core Libraries committed Feb 28, 2022
1 parent 1962106 commit ac11adc
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public abstract class AbstractFuture<V extends @Nullable Object> extends Interna
implements ListenableFuture<V> {
// NOTE: Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||

static final boolean GENERATE_CANCELLATION_CAUSES;
private static final boolean GENERATE_CANCELLATION_CAUSES;

static {
// System.getProperty may throw if the security policy does not permit access.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ private Futures() {}
* @since 14.0
*/
public static <V extends @Nullable Object> ListenableFuture<V> immediateCancelledFuture() {
ListenableFuture<Object> instance = ImmediateCancelledFuture.INSTANCE;
if (instance != null) {
return (ListenableFuture<V>) instance;
}
return new ImmediateCancelledFuture<>();
return new ImmediateCancelledFuture<V>();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ static final class ImmediateFailedFuture<V extends @Nullable Object> extends Tru
}

static final class ImmediateCancelledFuture<V extends @Nullable Object> extends TrustedFuture<V> {
static final @Nullable ImmediateCancelledFuture<Object> INSTANCE =
AbstractFuture.GENERATE_CANCELLATION_CAUSES ? null : new ImmediateCancelledFuture<>();

ImmediateCancelledFuture() {
cancel(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
public abstract class AbstractFuture<V> extends InternalFutureFailureAccess
implements ListenableFuture<V> {

static final boolean GENERATE_CANCELLATION_CAUSES = false;

/**
* Tag interface marking trusted subclasses. This enables some optimizations. The implementation
* of this interface must also be an AbstractFuture and must not override or expose for overriding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public abstract class AbstractFuture<V extends @Nullable Object> extends Interna
implements ListenableFuture<V> {
// NOTE: Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||

static final boolean GENERATE_CANCELLATION_CAUSES;
private static final boolean GENERATE_CANCELLATION_CAUSES;

static {
// System.getProperty may throw if the security policy does not permit access.
Expand Down
6 changes: 1 addition & 5 deletions guava/src/com/google/common/util/concurrent/Futures.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,7 @@ private Futures() {}
* @since 14.0
*/
public static <V extends @Nullable Object> ListenableFuture<V> immediateCancelledFuture() {
ListenableFuture<Object> instance = ImmediateCancelledFuture.INSTANCE;
if (instance != null) {
return (ListenableFuture<V>) instance;
}
return new ImmediateCancelledFuture<>();
return new ImmediateCancelledFuture<V>();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ static final class ImmediateFailedFuture<V extends @Nullable Object> extends Tru
}

static final class ImmediateCancelledFuture<V extends @Nullable Object> extends TrustedFuture<V> {
static final @Nullable ImmediateCancelledFuture<Object> INSTANCE =
AbstractFuture.GENERATE_CANCELLATION_CAUSES ? null : new ImmediateCancelledFuture<>();

ImmediateCancelledFuture() {
cancel(false);
}
Expand Down

0 comments on commit ac11adc

Please sign in to comment.