Skip to content

Commit

Permalink
Roll back middle atttempt at micro-optimizing singletonIterator.
Browse files Browse the repository at this point in the history
The overall impact of my optimization attempts is unclear, so I'm going to put us back in the state we were in 6 months ago: That old implementation used slightly more memory than the later ones, and it can't be any faster now than it was then, but at least we know that it won't be any slower.

PiperOrigin-RevId: 630373651
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed May 3, 2024
1 parent 2f4154d commit addb4a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
25 changes: 12 additions & 13 deletions android/guava/src/com/google/common/collect/Iterators.java
Expand Up @@ -1105,31 +1105,30 @@ protected T get(int index) {

private static final class SingletonIterator<T extends @Nullable Object>
extends UnmodifiableIterator<T> {
private @Nullable Object valueOrThis;
private static final Object SENTINEL = new Object();

private @Nullable Object valueOrSentinel;

SingletonIterator(T value) {
this.valueOrThis = value;
this.valueOrSentinel = value;
}

@Override
public boolean hasNext() {
return valueOrThis != this;
return valueOrSentinel != SENTINEL;
}

@Override
@ParametricNullness
public T next() {
Object result = valueOrThis;
valueOrThis = this;
// We put the common case first, even though it's unlikely to matter if the code is run much:
// https://shipilev.net/jvm/anatomy-quarks/28-frequency-based-code-layout/
if (result != this) {
// The field held either a `T` or `this`, and it turned out not to be `this`.
@SuppressWarnings("unchecked")
T t = (T) result;
return t;
if (valueOrSentinel == SENTINEL) {
throw new NoSuchElementException();
}
throw new NoSuchElementException();
// The field held either a T or SENTINEL, and it turned out not to be SENTINEL.
@SuppressWarnings("unchecked")
T t = (T) valueOrSentinel;
valueOrSentinel = SENTINEL;
return t;
}
}

Expand Down
25 changes: 12 additions & 13 deletions guava/src/com/google/common/collect/Iterators.java
Expand Up @@ -1105,31 +1105,30 @@ protected T get(int index) {

private static final class SingletonIterator<T extends @Nullable Object>
extends UnmodifiableIterator<T> {
private @Nullable Object valueOrThis;
private static final Object SENTINEL = new Object();

private @Nullable Object valueOrSentinel;

SingletonIterator(T value) {
this.valueOrThis = value;
this.valueOrSentinel = value;
}

@Override
public boolean hasNext() {
return valueOrThis != this;
return valueOrSentinel != SENTINEL;
}

@Override
@ParametricNullness
public T next() {
Object result = valueOrThis;
valueOrThis = this;
// We put the common case first, even though it's unlikely to matter if the code is run much:
// https://shipilev.net/jvm/anatomy-quarks/28-frequency-based-code-layout/
if (result != this) {
// The field held either a `T` or `this`, and it turned out not to be `this`.
@SuppressWarnings("unchecked")
T t = (T) result;
return t;
if (valueOrSentinel == SENTINEL) {
throw new NoSuchElementException();
}
throw new NoSuchElementException();
// The field held either a T or SENTINEL, and it turned out not to be SENTINEL.
@SuppressWarnings("unchecked")
T t = (T) valueOrSentinel;
valueOrSentinel = SENTINEL;
return t;
}
}

Expand Down

0 comments on commit addb4a8

Please sign in to comment.