From 347ef4ec219fed3b2801c094d800af1dd5be1a76 Mon Sep 17 00:00:00 2001 From: Google Java Core Libraries Date: Thu, 27 Jul 2023 15:02:47 -0700 Subject: [PATCH] Read value of PSL-based suffix caches into a local variable before processing them. Also annotate them with @LazyInit. RELNOTES=n/a PiperOrigin-RevId: 551650751 --- .../google/common/net/InternetDomainName.java | 19 +++++++++++++------ .../google/common/net/InternetDomainName.java | 19 +++++++++++++------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/android/guava/src/com/google/common/net/InternetDomainName.java b/android/guava/src/com/google/common/net/InternetDomainName.java index ba28e674793f..d098bf4fb0b9 100644 --- a/android/guava/src/com/google/common/net/InternetDomainName.java +++ b/android/guava/src/com/google/common/net/InternetDomainName.java @@ -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; @@ -126,6 +127,7 @@ public final class InternetDomainName { * value. */ @SuppressWarnings("Immutable") + @LazyInit private int publicSuffixIndexCache = SUFFIX_NOT_INITIALIZED; /** @@ -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. */ @@ -166,10 +169,12 @@ public final class InternetDomainName { * suffix was found. */ private int publicSuffixIndex() { - if (publicSuffixIndexCache == SUFFIX_NOT_INITIALIZED) { - publicSuffixIndexCache = findSuffixOfType(Optional.absent()); + int publicSuffixIndexLocal = publicSuffixIndexCache; + if (publicSuffixIndexLocal == SUFFIX_NOT_INITIALIZED) { + publicSuffixIndexCache = + publicSuffixIndexLocal = findSuffixOfType(Optional.absent()); } - return publicSuffixIndexCache; + return publicSuffixIndexLocal; } /** @@ -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; } /** diff --git a/guava/src/com/google/common/net/InternetDomainName.java b/guava/src/com/google/common/net/InternetDomainName.java index ba28e674793f..d098bf4fb0b9 100644 --- a/guava/src/com/google/common/net/InternetDomainName.java +++ b/guava/src/com/google/common/net/InternetDomainName.java @@ -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; @@ -126,6 +127,7 @@ public final class InternetDomainName { * value. */ @SuppressWarnings("Immutable") + @LazyInit private int publicSuffixIndexCache = SUFFIX_NOT_INITIALIZED; /** @@ -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. */ @@ -166,10 +169,12 @@ public final class InternetDomainName { * suffix was found. */ private int publicSuffixIndex() { - if (publicSuffixIndexCache == SUFFIX_NOT_INITIALIZED) { - publicSuffixIndexCache = findSuffixOfType(Optional.absent()); + int publicSuffixIndexLocal = publicSuffixIndexCache; + if (publicSuffixIndexLocal == SUFFIX_NOT_INITIALIZED) { + publicSuffixIndexCache = + publicSuffixIndexLocal = findSuffixOfType(Optional.absent()); } - return publicSuffixIndexCache; + return publicSuffixIndexLocal; } /** @@ -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; } /**