@@ -1693,19 +1693,20 @@ class ShenandoahParallelHeapRegionTask : public WorkerTask {
1693
1693
private:
1694
1694
ShenandoahHeap* const _heap;
1695
1695
ShenandoahHeapRegionClosure* const _blk;
1696
+ size_t const _stride;
1696
1697
1697
1698
shenandoah_padding (0 );
1698
1699
volatile size_t _index;
1699
1700
shenandoah_padding (1 );
1700
1701
1701
1702
public:
1702
- ShenandoahParallelHeapRegionTask (ShenandoahHeapRegionClosure* blk) :
1703
+ ShenandoahParallelHeapRegionTask (ShenandoahHeapRegionClosure* blk, size_t stride ) :
1703
1704
WorkerTask (" Shenandoah Parallel Region Operation" ),
1704
- _heap (ShenandoahHeap::heap()), _blk(blk), _index(0 ) {}
1705
+ _heap (ShenandoahHeap::heap()), _blk(blk), _stride(stride), _index(0 ) {}
1705
1706
1706
1707
void work (uint worker_id) {
1707
1708
ShenandoahParallelWorkerSession worker_session (worker_id);
1708
- size_t stride = ShenandoahParallelRegionStride ;
1709
+ size_t stride = _stride ;
1709
1710
1710
1711
size_t max = _heap->num_regions ();
1711
1712
while (Atomic::load (&_index) < max) {
@@ -1724,8 +1725,20 @@ class ShenandoahParallelHeapRegionTask : public WorkerTask {
1724
1725
1725
1726
void ShenandoahHeap::parallel_heap_region_iterate (ShenandoahHeapRegionClosure* blk) const {
1726
1727
assert (blk->is_thread_safe (), " Only thread-safe closures here" );
1727
- if (num_regions () > ShenandoahParallelRegionStride) {
1728
- ShenandoahParallelHeapRegionTask task (blk);
1728
+ const uint active_workers = workers ()->active_workers ();
1729
+ const size_t n_regions = num_regions ();
1730
+ size_t stride = ShenandoahParallelRegionStride;
1731
+ if (stride == 0 && active_workers > 1 ) {
1732
+ // Automatically derive the stride to balance the work between threads
1733
+ // evenly. Do not try to split work if below the reasonable threshold.
1734
+ constexpr size_t threshold = 4096 ;
1735
+ stride = n_regions <= threshold ?
1736
+ threshold :
1737
+ (n_regions + active_workers - 1 ) / active_workers;
1738
+ }
1739
+
1740
+ if (n_regions > stride && active_workers > 1 ) {
1741
+ ShenandoahParallelHeapRegionTask task (blk, stride);
1729
1742
workers ()->run_task (&task);
1730
1743
} else {
1731
1744
heap_region_iterate (blk);
0 commit comments