Skip to content

Commit

Permalink
Read value of PSL-based suffix caches into a local variable before pr…
Browse files Browse the repository at this point in the history
…ocessing them.

Also annotate them with @lazyinit.

RELNOTES=n/a
PiperOrigin-RevId: 551650751
  • Loading branch information
java-team-github-bot authored and Google Java Core Libraries committed Jul 27, 2023
1 parent eaa62eb commit 347ef4e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
19 changes: 13 additions & 6 deletions android/guava/src/com/google/common/net/InternetDomainName.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.thirdparty.publicsuffix.PublicSuffixPatterns;
import com.google.thirdparty.publicsuffix.PublicSuffixType;
import java.util.List;
Expand Down Expand Up @@ -126,6 +127,7 @@ public final class InternetDomainName {
* value.
*/
@SuppressWarnings("Immutable")
@LazyInit
private int publicSuffixIndexCache = SUFFIX_NOT_INITIALIZED;

/**
Expand All @@ -136,6 +138,7 @@ public final class InternetDomainName {
* value.
*/
@SuppressWarnings("Immutable")
@LazyInit
private int registrySuffixIndexCache = SUFFIX_NOT_INITIALIZED;

/** Constructor used to implement {@link #from(String)}, and from subclasses. */
Expand Down Expand Up @@ -166,10 +169,12 @@ public final class InternetDomainName {
* suffix was found.
*/
private int publicSuffixIndex() {
if (publicSuffixIndexCache == SUFFIX_NOT_INITIALIZED) {
publicSuffixIndexCache = findSuffixOfType(Optional.<PublicSuffixType>absent());
int publicSuffixIndexLocal = publicSuffixIndexCache;
if (publicSuffixIndexLocal == SUFFIX_NOT_INITIALIZED) {
publicSuffixIndexCache =
publicSuffixIndexLocal = findSuffixOfType(Optional.<PublicSuffixType>absent());
}
return publicSuffixIndexCache;
return publicSuffixIndexLocal;
}

/**
Expand All @@ -179,10 +184,12 @@ private int publicSuffixIndex() {
* was found.
*/
private int registrySuffixIndex() {
if (registrySuffixIndexCache == SUFFIX_NOT_INITIALIZED) {
registrySuffixIndexCache = findSuffixOfType(Optional.of(PublicSuffixType.REGISTRY));
int registrySuffixIndexLocal = registrySuffixIndexCache;
if (registrySuffixIndexLocal == SUFFIX_NOT_INITIALIZED) {
registrySuffixIndexCache =
registrySuffixIndexLocal = findSuffixOfType(Optional.of(PublicSuffixType.REGISTRY));
}
return registrySuffixIndexCache;
return registrySuffixIndexLocal;
}

/**
Expand Down
19 changes: 13 additions & 6 deletions guava/src/com/google/common/net/InternetDomainName.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.thirdparty.publicsuffix.PublicSuffixPatterns;
import com.google.thirdparty.publicsuffix.PublicSuffixType;
import java.util.List;
Expand Down Expand Up @@ -126,6 +127,7 @@ public final class InternetDomainName {
* value.
*/
@SuppressWarnings("Immutable")
@LazyInit
private int publicSuffixIndexCache = SUFFIX_NOT_INITIALIZED;

/**
Expand All @@ -136,6 +138,7 @@ public final class InternetDomainName {
* value.
*/
@SuppressWarnings("Immutable")
@LazyInit
private int registrySuffixIndexCache = SUFFIX_NOT_INITIALIZED;

/** Constructor used to implement {@link #from(String)}, and from subclasses. */
Expand Down Expand Up @@ -166,10 +169,12 @@ public final class InternetDomainName {
* suffix was found.
*/
private int publicSuffixIndex() {
if (publicSuffixIndexCache == SUFFIX_NOT_INITIALIZED) {
publicSuffixIndexCache = findSuffixOfType(Optional.<PublicSuffixType>absent());
int publicSuffixIndexLocal = publicSuffixIndexCache;
if (publicSuffixIndexLocal == SUFFIX_NOT_INITIALIZED) {
publicSuffixIndexCache =
publicSuffixIndexLocal = findSuffixOfType(Optional.<PublicSuffixType>absent());
}
return publicSuffixIndexCache;
return publicSuffixIndexLocal;
}

/**
Expand All @@ -179,10 +184,12 @@ private int publicSuffixIndex() {
* was found.
*/
private int registrySuffixIndex() {
if (registrySuffixIndexCache == SUFFIX_NOT_INITIALIZED) {
registrySuffixIndexCache = findSuffixOfType(Optional.of(PublicSuffixType.REGISTRY));
int registrySuffixIndexLocal = registrySuffixIndexCache;
if (registrySuffixIndexLocal == SUFFIX_NOT_INITIALIZED) {
registrySuffixIndexCache =
registrySuffixIndexLocal = findSuffixOfType(Optional.of(PublicSuffixType.REGISTRY));
}
return registrySuffixIndexCache;
return registrySuffixIndexLocal;
}

/**
Expand Down

0 comments on commit 347ef4e

Please sign in to comment.