Skip to content

Commit ff8773a

Browse files
Small optimization for Heap.getAllClasses().
1 parent e53dac5 commit ff8773a

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
@@ -84,6 +84,7 @@
8484
import com.oracle.svm.core.thread.VMThreads.SafepointBehavior;
8585
import com.oracle.svm.core.util.UnsignedUtils;
8686
import com.oracle.svm.core.util.UserError;
87+
import com.oracle.svm.core.util.VMError;
8788

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

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

360-
ClassListBuilderVisitor(List<Class<?>> list) {
368+
ClassListBuilderVisitor(int dynamicHubCount, List<Class<?>> list) {
369+
this.dynamicHubCount = dynamicHubCount;
361370
this.list = list;
362371
}
363372

364373
@Override
365374
public <T> boolean visitNativeImageHeapRegion(T region, MemoryWalker.NativeImageHeapRegionAccess<T> access) {
366375
if (!access.isWritable(region) && !access.consistsOfHugeObjects(region)) {
367-
access.visitObjects(region, this);
376+
return access.visitObjects(region, this);
368377
}
369378
return true;
370379
}
@@ -374,6 +383,7 @@ public <T> boolean visitNativeImageHeapRegion(T region, MemoryWalker.NativeImage
374383
public boolean visitObject(Object o) {
375384
if (o instanceof Class<?>) {
376385
list.add((Class<?>) o);
386+
return list.size() != dynamicHubCount;
377387
}
378388
return true;
379389
}

0 commit comments

Comments
 (0)