Skip to content

[NFC] Use a sorted vector instead of a map in ConstraintAnalysis - #9154

Open
kripken wants to merge 13 commits into
WebAssembly:mainfrom
kripken:c.sorted
Open

kripken wants to merge 13 commits into
WebAssembly:mainfrom
kripken:c.sorted

Conversation

@kripken

@kripken kripken commented Sep 25, 2026

Copy link
Copy Markdown
Member

We usually have only a few useful constraints, so the map is mostly
empty. An unordered_map is then pretty inefficient, and it is better
to use a vector. Sorting it makes lookup still pretty fast.

This especially helps in ORing two sets of constraints, as we can
just do an intersection (the OR result is only interesting if we had
something in both inputs - otherwise we can prove nothing), which
is efficient on sorted vectors.

To implement this, generalize the existing SortedVector.

This makes the pass 50% faster on average, though the spread
is wide (20%-almost 2X faster). I tested around 10 real-world
wasm files and did not see a single one with less than an 18%
speedup, and never a regression.

@kripken
kripken requested a review from a team as a code owner September 25, 2026 19:03
@kripken
kripken requested review from aheejin and removed request for a team September 25, 2026 19:03
Comment thread src/ir/constraint.cpp
if (constraints.provesNothing()) {
setProvesNothing(index);
} else {
if (!constraints.provesNothing()) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by fix, see the comment on line 1054 - we already prove nothing at this point.

Comment thread src/ir/constraint.cpp
// We just proved we are in unreachable code.
unreachable = true;
map.clear();
refs.clear();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another drive-by trivial fix (irrelevent for correctness, see the comment on new line 1215).

Comment thread src/ir/constraint.cpp

auto& refIndexes = iter->second;
auto refIndexes = std::move(iter->value);
refs.erase(iter);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(as above, we kept around stale refs unnecessarily; added a comment in the header to mention that this function is called when we wipe out all the info)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant