Skip to content

Commit c864b52

Browse files
committed
Peer review feedback: suppliers for total instance size cache
1 parent 126278c commit c864b52

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/main/java/com/sun/tools/hat/internal/model/JavaClass.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.Collections;
3737
import java.util.List;
3838

39+
import com.google.common.base.Supplier;
40+
import com.google.common.base.Suppliers;
3941
import com.google.common.collect.Iterables;
4042
import com.sun.tools.hat.internal.parser.ReadBuffer;
4143

@@ -76,8 +78,7 @@ public class JavaClass extends JavaHeapObject {
7678
// Total number of fields including inherited ones
7779
private int totalNumFields;
7880
// Cache of total instance size
79-
private volatile long totalInstanceSize = -1L;
80-
81+
private final Supplier<Long> totalInstanceSizeSupplier = Suppliers.memoize(this::getTotalInstanceSizeSupplierImpl);
8182

8283
public JavaClass(long id, String name, long superclassId, long loaderId,
8384
long signersId, long protDomainId,
@@ -388,14 +389,10 @@ public int getInstanceSize() {
388389
* arrays.
389390
*/
390391
public long getTotalInstanceSize() {
391-
if (totalInstanceSize >= 0) {
392-
return totalInstanceSize;
393-
} else {
394-
return cacheTotalInstanceSize();
395-
}
392+
return totalInstanceSizeSupplier.get();
396393
}
397394

398-
public synchronized long cacheTotalInstanceSize() {
395+
private long getTotalInstanceSizeSupplierImpl() {
399396
int count = instances.size();
400397
if (count == 0 || !isArray()) {
401398
totalInstanceSize = (long) count * instanceSize;

0 commit comments

Comments
 (0)