Skip to content

Commit 3b0ab2c

Browse files
authored
[NFC] Make public type collection more efficient (#7561)
Previously public type collection worked by collecting all types in the module, classifying their visibility, then picking out all the public types to return. However, visibility classification requires directly finding the public types from the imports and exports in the first place, so the initial step of collecting all the types was unnecessary. Refactor the code so `getPublicHeapTypes` calculates the public types directly from the imports and exports and `classifyTypes` uses those results to classify visibility only when collecting all types from the module anyway.
1 parent ad4672e commit 3b0ab2c

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

src/ir/module-utils.cpp

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,46 @@ namespace {
590590

591591
void classifyTypeVisibility(Module& wasm,
592592
InsertOrderedMap<HeapType, HeapTypeInfo>& types) {
593-
// We will need to traverse the types used by public types and mark them
594-
// public as well.
593+
for (auto type : getPublicHeapTypes(wasm)) {
594+
if (auto it = types.find(type); it != types.end()) {
595+
it->second.visibility = Visibility::Public;
596+
}
597+
}
598+
for (auto& [type, info] : types) {
599+
if (info.visibility != Visibility::Public) {
600+
info.visibility = Visibility::Private;
601+
}
602+
}
603+
}
604+
605+
void setIndices(IndexedHeapTypes& indexedTypes) {
606+
for (Index i = 0; i < indexedTypes.types.size(); i++) {
607+
indexedTypes.indices[indexedTypes.types[i]] = i;
608+
}
609+
}
610+
611+
} // anonymous namespace
612+
613+
std::vector<HeapType> collectHeapTypes(Module& wasm) {
614+
auto info = collectHeapTypeInfo(wasm);
615+
std::vector<HeapType> types;
616+
types.reserve(info.size());
617+
for (auto& [type, _] : info) {
618+
types.push_back(type);
619+
}
620+
return types;
621+
}
622+
623+
std::vector<HeapType> getPublicHeapTypes(Module& wasm) {
624+
// Look at the types of imports as exports to get an initial set of public
625+
// types, then traverse the types used by public types and collect the
626+
// transitively reachable public types as well.
595627
std::vector<HeapType> workList;
596628
std::unordered_set<RecGroup> publicGroups;
597629

630+
// The collected types.
631+
std::vector<HeapType> publicTypes;
632+
598633
auto notePublic = [&](HeapType type) {
599634
if (type.isBasic()) {
600635
return;
@@ -604,12 +639,8 @@ void classifyTypeVisibility(Module& wasm,
604639
// The groups in this type have already been marked public.
605640
return;
606641
}
607-
for (auto member : type.getRecGroup()) {
608-
if (auto it = types.find(member); it != types.end()) {
609-
it->second.visibility = Visibility::Public;
610-
}
611-
workList.push_back(member);
612-
}
642+
publicTypes.insert(publicTypes.end(), group.begin(), group.end());
643+
workList.insert(workList.end(), group.begin(), group.end());
613644
};
614645

615646
ModuleUtils::iterImportedTags(wasm, [&](Tag* tag) { notePublic(tag->type); });
@@ -675,46 +706,10 @@ void classifyTypeVisibility(Module& wasm,
675706
}
676707
}
677708

678-
for (auto& [_, info] : types) {
679-
if (info.visibility != Visibility::Public) {
680-
info.visibility = Visibility::Private;
681-
}
682-
}
683-
684709
// TODO: In an open world, we need to consider subtypes of public types public
685710
// as well, or potentially even consider all types to be public unless
686711
// otherwise annotated.
687-
}
688-
689-
void setIndices(IndexedHeapTypes& indexedTypes) {
690-
for (Index i = 0; i < indexedTypes.types.size(); i++) {
691-
indexedTypes.indices[indexedTypes.types[i]] = i;
692-
}
693-
}
694-
695-
} // anonymous namespace
696-
697-
std::vector<HeapType> collectHeapTypes(Module& wasm) {
698-
auto info = collectHeapTypeInfo(wasm);
699-
std::vector<HeapType> types;
700-
types.reserve(info.size());
701-
for (auto& [type, _] : info) {
702-
types.push_back(type);
703-
}
704-
return types;
705-
}
706-
707-
std::vector<HeapType> getPublicHeapTypes(Module& wasm) {
708-
auto info = collectHeapTypeInfo(
709-
wasm, TypeInclusion::BinaryTypes, VisibilityHandling::FindVisibility);
710-
std::vector<HeapType> types;
711-
types.reserve(info.size());
712-
for (auto& [type, typeInfo] : info) {
713-
if (typeInfo.visibility == Visibility::Public) {
714-
types.push_back(type);
715-
}
716-
}
717-
return types;
712+
return publicTypes;
718713
}
719714

720715
std::vector<HeapType> getPrivateHeapTypes(Module& wasm) {

0 commit comments

Comments
 (0)