Skip to content

Commit cfa8abd

Browse files
authored
[NFC] Optimize ModuleUtils type-scanning code (#8572)
Before, we always did a loop on `type.getHeapTypeChildren()` which means setting up a scanner object and going through a generic path. Instead, handle the common cases directly. This avoids any generic path in the common case. This makes us 1% faster on `-O3`, as measured by instruction count, number of branches, and walltime. The noise in the first two is incredibly small, so this looks reliably faster.
1 parent b09fad0 commit cfa8abd

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/ir/module-utils.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,15 @@ struct TypeInfos {
363363
}
364364
}
365365
void note(Type type) {
366-
for (HeapType ht : type.getHeapTypeChildren()) {
367-
note(ht);
366+
// Handle the common case of a ref directly, to avoid a scan of children.
367+
if (type.isRef()) {
368+
note(type.getHeapType());
369+
return;
370+
}
371+
if (type.isTuple()) {
372+
for (HeapType ht : type.getHeapTypeChildren()) {
373+
note(ht);
374+
}
368375
}
369376
}
370377
// Ensure a type is included without increasing its count.
@@ -374,8 +381,14 @@ struct TypeInfos {
374381
}
375382
}
376383
void include(Type type) {
377-
for (HeapType ht : type.getHeapTypeChildren()) {
378-
include(ht);
384+
if (type.isRef()) {
385+
include(type.getHeapType());
386+
return;
387+
}
388+
if (type.isTuple()) {
389+
for (HeapType ht : type.getHeapTypeChildren()) {
390+
include(ht);
391+
}
379392
}
380393
}
381394
void noteControlFlow(Signature sig) {

0 commit comments

Comments
 (0)