From 7c893a79ac87ebf1e35a325282934b4efebb9275 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 25 Sep 2025 11:53:07 +0200 Subject: [PATCH] avoid unnecessarily holding onto Region in CacheRegionStatisticsImpl --- .../internal/CacheRegionStatisticsImpl.java | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java index e39fc0fd8d04..9f814ebcf119 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java @@ -7,6 +7,7 @@ import java.io.Serializable; import java.util.concurrent.atomic.LongAdder; +import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.cache.spi.ExtendedStatisticsSupport; import org.hibernate.cache.spi.Region; import org.hibernate.stat.CacheRegionStatistics; @@ -17,7 +18,8 @@ * @author Alex Snaps */ public class CacheRegionStatisticsImpl implements CacheRegionStatistics, Serializable { - private final transient Region region; + private final String regionName; + private final transient @Nullable ExtendedStatisticsSupport extendedStatisticsSupport; private final LongAdder hitCount = new LongAdder(); private final LongAdder missCount = new LongAdder(); @@ -25,12 +27,16 @@ public class CacheRegionStatisticsImpl implements CacheRegionStatistics, Seriali private final LongAdder removeCount = new LongAdder(); CacheRegionStatisticsImpl(Region region) { - this.region = region; + regionName = region.getName(); + extendedStatisticsSupport = + region instanceof ExtendedStatisticsSupport extended + ? extended + : null; } @Override public String getRegionName() { - return region.getName(); + return regionName; } @Override @@ -55,23 +61,23 @@ public long getRemoveCount() { @Override public long getElementCountInMemory() { - return region instanceof ExtendedStatisticsSupport extended - ? extended.getElementCountInMemory() - : NO_EXTENDED_STAT_SUPPORT_RETURN; + return extendedStatisticsSupport == null + ? NO_EXTENDED_STAT_SUPPORT_RETURN + : extendedStatisticsSupport.getElementCountInMemory(); } @Override public long getElementCountOnDisk() { - return region instanceof ExtendedStatisticsSupport extended - ? extended.getElementCountOnDisk() - : NO_EXTENDED_STAT_SUPPORT_RETURN; + return extendedStatisticsSupport == null + ? NO_EXTENDED_STAT_SUPPORT_RETURN + : extendedStatisticsSupport.getElementCountOnDisk(); } @Override public long getSizeInMemory() { - return region instanceof ExtendedStatisticsSupport extended - ? extended.getSizeInMemory() - : NO_EXTENDED_STAT_SUPPORT_RETURN; + return extendedStatisticsSupport == null + ? NO_EXTENDED_STAT_SUPPORT_RETURN + : extendedStatisticsSupport.getSizeInMemory(); } void incrementHitCount() { @@ -93,15 +99,18 @@ public void incrementRemoveCount() { @Override public String toString() { - return "CacheRegionStatistics" - + "[region=" + region.getName() - + ",hitCount=" + hitCount - + ",missCount=" + missCount - + ",putCount=" + putCount - + ",removeCount=" + removeCount - + ",elementCountInMemory=" + getElementCountInMemory() - + ",elementCountOnDisk=" + getElementCountOnDisk() - + ",sizeInMemory=" + getSizeInMemory() - + ']'; + final var string = + new StringBuilder("CacheRegionStatistics") + .append( "[region=" ).append( regionName ) + .append( ",hitCount=" ).append( hitCount ) + .append( ",missCount=" ).append( missCount ) + .append( ",putCount=" ).append( putCount ) + .append( ",removeCount=" ).append( removeCount ); + if ( extendedStatisticsSupport != null ) { + string.append( ",elementCountInMemory=" ).append( getElementCountInMemory() ) + .append( ",elementCountOnDisk=" ).append( getElementCountOnDisk() ) + .append( ",sizeInMemory=" ).append( getSizeInMemory() ); + } + return string.append( ']' ).toString(); } }