Date: Mon, 5 Jun 2023 20:26:02 +0800
Subject: 8223162: Improve ergonomics for Sparse PRT entry sizing

---
 .../share/vm/gc_implementation/g1/heapRegionRemSet.cpp    | 8 ++++----
 .../share/vm/gc_implementation/g1/heapRegionRemSet.hpp    | 1 +
 hotspot/src/share/vm/gc_implementation/g1/sparsePRT.hpp   | 4 +---
 3 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/hotspot/src/share/vm/gc_implementation/g1/heapRegionRemSet.cpp b/hotspot/src/share/vm/gc_implementation/g1/heapRegionRemSet.cpp
index 8167d2b09..9e9391ba6 100644
--- a/hotspot/src/share/vm/gc_implementation/g1/heapRegionRemSet.cpp
+++ b/hotspot/src/share/vm/gc_implementation/g1/heapRegionRemSet.cpp
@@ -868,12 +868,12 @@ HeapRegionRemSet::HeapRegionRemSet(G1BlockOffsetSharedArray* bosa,
 }
 
 void HeapRegionRemSet::setup_remset_size() {
-  // Setup sparse and fine-grain tables sizes.
-  // table_size = base * (log(region_size / 1M) + 1)
   const int LOG_M = 20;
-  int region_size_log_mb = MAX2(HeapRegion::LogOfHRGrainBytes - LOG_M, 0);
+  guarantee(HeapRegion::LogOfHRGrainBytes >= LOG_M, err_msg("Code assumes the region size >= 1M, but is " SIZE_FORMAT "B", HeapRegion::GrainBytes));
+
+  int region_size_log_mb = HeapRegion::LogOfHRGrainBytes - LOG_M;
   if (FLAG_IS_DEFAULT(G1RSetSparseRegionEntries)) {
-    G1RSetSparseRegionEntries = G1RSetSparseRegionEntriesBase * (region_size_log_mb + 1);
+    G1RSetSparseRegionEntries = G1RSetSparseRegionEntriesBase * ((size_t)1 << (region_size_log_mb + 1));
   }
   if (FLAG_IS_DEFAULT(G1RSetRegionEntries)) {
     G1RSetRegionEntries = G1RSetRegionEntriesBase * (region_size_log_mb + 1);
diff --git a/hotspot/src/share/vm/gc_implementation/g1/heapRegionRemSet.hpp b/hotspot/src/share/vm/gc_implementation/g1/heapRegionRemSet.hpp
index 77751b4a9..6659dc550 100644
--- a/hotspot/src/share/vm/gc_implementation/g1/heapRegionRemSet.hpp
+++ b/hotspot/src/share/vm/gc_implementation/g1/heapRegionRemSet.hpp
@@ -271,6 +271,7 @@ public:
   HeapRegionRemSet(G1BlockOffsetSharedArray* bosa, HeapRegion* hr);
 
   static uint num_par_rem_sets();
+  // Setup sparse and fine-grain tables sizes.
   static void setup_remset_size();
 
   HeapRegion* hr() const {
diff --git a/hotspot/src/share/vm/gc_implementation/g1/sparsePRT.hpp b/hotspot/src/share/vm/gc_implementation/g1/sparsePRT.hpp
index 17bd4a145..3d2de1a95 100644
--- a/hotspot/src/share/vm/gc_implementation/g1/sparsePRT.hpp
+++ b/hotspot/src/share/vm/gc_implementation/g1/sparsePRT.hpp
@@ -218,9 +218,7 @@ class SparsePRT VALUE_OBJ_CLASS_SPEC {
 
   HeapRegion* _hr;
 
-  enum SomeAdditionalPrivateConstants {
-    InitialCapacity = 16
-  };
+  static const size_t InitialCapacity = 8;
 
   void expand();
 
-- 
2.22.0

