Skip to content

Commit d9352e2

Browse files
committed
avoid unnecessarily holding onto Region in CacheRegionStatisticsImpl
1 parent 6a95a1c commit d9352e2

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.Serializable;
88
import java.util.concurrent.atomic.LongAdder;
99

10+
import org.checkerframework.checker.nullness.qual.Nullable;
1011
import org.hibernate.cache.spi.ExtendedStatisticsSupport;
1112
import org.hibernate.cache.spi.Region;
1213
import org.hibernate.stat.CacheRegionStatistics;
@@ -17,20 +18,25 @@
1718
* @author Alex Snaps
1819
*/
1920
public class CacheRegionStatisticsImpl implements CacheRegionStatistics, Serializable {
20-
private final transient Region region;
21+
private final String regionName;
22+
private final transient @Nullable ExtendedStatisticsSupport extendedStatisticsSupport;
2123

2224
private final LongAdder hitCount = new LongAdder();
2325
private final LongAdder missCount = new LongAdder();
2426
private final LongAdder putCount = new LongAdder();
2527
private final LongAdder removeCount = new LongAdder();
2628

2729
CacheRegionStatisticsImpl(Region region) {
28-
this.region = region;
30+
regionName = region.getName();
31+
extendedStatisticsSupport =
32+
region instanceof ExtendedStatisticsSupport extended
33+
? extended
34+
: null;
2935
}
3036

3137
@Override
3238
public String getRegionName() {
33-
return region.getName();
39+
return regionName;
3440
}
3541

3642
@Override
@@ -55,23 +61,23 @@ public long getRemoveCount() {
5561

5662
@Override
5763
public long getElementCountInMemory() {
58-
return region instanceof ExtendedStatisticsSupport extended
59-
? extended.getElementCountInMemory()
60-
: NO_EXTENDED_STAT_SUPPORT_RETURN;
64+
return extendedStatisticsSupport == null
65+
? NO_EXTENDED_STAT_SUPPORT_RETURN
66+
: extendedStatisticsSupport.getElementCountInMemory();
6167
}
6268

6369
@Override
6470
public long getElementCountOnDisk() {
65-
return region instanceof ExtendedStatisticsSupport extended
66-
? extended.getElementCountOnDisk()
67-
: NO_EXTENDED_STAT_SUPPORT_RETURN;
71+
return extendedStatisticsSupport == null
72+
? NO_EXTENDED_STAT_SUPPORT_RETURN
73+
: extendedStatisticsSupport.getElementCountOnDisk();
6874
}
6975

7076
@Override
7177
public long getSizeInMemory() {
72-
return region instanceof ExtendedStatisticsSupport extended
73-
? extended.getSizeInMemory()
74-
: NO_EXTENDED_STAT_SUPPORT_RETURN;
78+
return extendedStatisticsSupport == null
79+
? NO_EXTENDED_STAT_SUPPORT_RETURN
80+
: extendedStatisticsSupport.getSizeInMemory();
7581
}
7682

7783
void incrementHitCount() {
@@ -93,15 +99,18 @@ public void incrementRemoveCount() {
9399

94100
@Override
95101
public String toString() {
96-
return "CacheRegionStatistics"
97-
+ "[region=" + region.getName()
98-
+ ",hitCount=" + hitCount
99-
+ ",missCount=" + missCount
100-
+ ",putCount=" + putCount
101-
+ ",removeCount=" + removeCount
102-
+ ",elementCountInMemory=" + getElementCountInMemory()
103-
+ ",elementCountOnDisk=" + getElementCountOnDisk()
104-
+ ",sizeInMemory=" + getSizeInMemory()
105-
+ ']';
102+
final var string =
103+
new StringBuilder("CacheRegionStatistics")
104+
.append( "[region=" ).append( regionName )
105+
.append( ",hitCount=" ).append( hitCount )
106+
.append( ",missCount=" ).append( missCount )
107+
.append( ",putCount=" ).append( putCount )
108+
.append( ",removeCount=" ).append( removeCount );
109+
if ( extendedStatisticsSupport != null ) {
110+
string.append( ",elementCountInMemory=" ).append( getElementCountInMemory() )
111+
.append( ",elementCountOnDisk=" ).append( getElementCountOnDisk() )
112+
.append( ",sizeInMemory=" ).append( getSizeInMemory() );
113+
}
114+
return string.append( ']' ).toString();
106115
}
107116
}

0 commit comments

Comments
 (0)