Skip to content

Commit 643e90f

Browse files
[GR-51393] Small optimization for Heap.getAllClasses().
PullRequest: graal/17373
2 parents 9cdc261 + ff8773a commit 643e90f

File tree

1 file changed

+19
-9
lines changed
  • substratevm/src/com.oracle.svm.core.genscavenge/src/com/oracle/svm/core/genscavenge

1 file changed

+19
-9
lines changed

substratevm/src/com.oracle.svm.core.genscavenge/src/com/oracle/svm/core/genscavenge/HeapImpl.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import com.oracle.svm.core.thread.VMThreads.SafepointBehavior;
8484
import com.oracle.svm.core.util.UnsignedUtils;
8585
import com.oracle.svm.core.util.UserError;
86+
import com.oracle.svm.core.util.VMError;
8687

8788
import jdk.graal.compiler.api.directives.GraalDirectives;
8889
import jdk.graal.compiler.api.replacements.Fold;
@@ -339,31 +340,39 @@ public int getClassCount() {
339340
protected List<Class<?>> getAllClasses() {
340341
/* Two threads might race to set classList, but they compute the same result. */
341342
if (classList == null) {
342-
ArrayList<Class<?>> list = new ArrayList<>(1024);
343-
for (ImageHeapInfo info = firstImageHeapInfo; info != null; info = info.next) {
344-
ImageHeapWalker.walkRegions(info, new ClassListBuilderVisitor(list));
345-
}
346-
list.trimToSize();
347-
343+
ArrayList<Class<?>> list = findAllDynamicHubs();
348344
/* Ensure that other threads see consistent values once the list is published. */
349345
MembarNode.memoryBarrier(MembarNode.FenceKind.STORE_STORE);
350346
classList = list;
351347
}
352-
assert classList.size() == getClassCount();
353348
return classList;
354349
}
355350

351+
private ArrayList<Class<?>> findAllDynamicHubs() {
352+
int dynamicHubCount = getClassCount();
353+
354+
ArrayList<Class<?>> list = new ArrayList<>(dynamicHubCount);
355+
for (ImageHeapInfo info = firstImageHeapInfo; info != null; info = info.next) {
356+
ImageHeapWalker.walkRegions(info, new ClassListBuilderVisitor(list.size() + info.dynamicHubCount, list));
357+
}
358+
359+
VMError.guarantee(dynamicHubCount == list.size(), "Found fewer DynamicHubs in the image heap than expected.");
360+
return list;
361+
}
362+
356363
private static class ClassListBuilderVisitor implements MemoryWalker.ImageHeapRegionVisitor, ObjectVisitor {
364+
private final int dynamicHubCount;
357365
private final List<Class<?>> list;
358366

359-
ClassListBuilderVisitor(List<Class<?>> list) {
367+
ClassListBuilderVisitor(int dynamicHubCount, List<Class<?>> list) {
368+
this.dynamicHubCount = dynamicHubCount;
360369
this.list = list;
361370
}
362371

363372
@Override
364373
public <T> boolean visitNativeImageHeapRegion(T region, MemoryWalker.NativeImageHeapRegionAccess<T> access) {
365374
if (!access.isWritable(region) && !access.consistsOfHugeObjects(region)) {
366-
access.visitObjects(region, this);
375+
return access.visitObjects(region, this);
367376
}
368377
return true;
369378
}
@@ -373,6 +382,7 @@ public <T> boolean visitNativeImageHeapRegion(T region, MemoryWalker.NativeImage
373382
public boolean visitObject(Object o) {
374383
if (o instanceof Class<?>) {
375384
list.add((Class<?>) o);
385+
return list.size() != dynamicHubCount;
376386
}
377387
return true;
378388
}

0 commit comments

Comments
 (0)