Skip to content

Commit 3990615

Browse files
[NFC] Use unordered_set in effects.h and CodePushing (#8586)
This avoids large slowdowns in cases with very long string names, etc.
1 parent baa1564 commit 3990615

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ CMakeUserPresets.json
6161

6262
# files related to clangd cache
6363
.cache/*
64+
65+
.venv/

src/ir/effects.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define wasm_ir_effects_h
1919

2020
#include <cassert>
21+
#include <unordered_set>
2122

2223
#include "ir/intrinsics.h"
2324
#include "pass.h"
@@ -141,8 +142,8 @@ class EffectAnalyzer {
141142

142143
std::set<Index> localsRead;
143144
std::set<Index> localsWritten;
144-
std::set<Name> mutableGlobalsRead;
145-
std::set<Name> globalsWritten;
145+
std::unordered_set<Name> mutableGlobalsRead;
146+
std::unordered_set<Name> globalsWritten;
146147

147148
// The nested depth of try-catch_all. If an instruction that may throw is
148149
// inside an inner try-catch_all, we don't mark it as 'throws_', because it
@@ -513,8 +514,8 @@ class EffectAnalyzer {
513514
return hasAnything();
514515
}
515516

516-
std::set<Name> breakTargets;
517-
std::set<Name> delegateTargets;
517+
std::unordered_set<Name> breakTargets;
518+
std::unordered_set<Name> delegateTargets;
518519

519520
private:
520521
struct InternalAnalyzer

src/passes/CodeFolding.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
//
5757

5858
#include <iterator>
59+
#include <unordered_map>
60+
#include <unordered_set>
5961

6062
#include "ir/branch-utils.h"
6163
#include "ir/effects.h"
@@ -74,9 +76,9 @@ static const Index WORTH_ADDING_BLOCK_TO_REMOVE_THIS_MUCH = 3;
7476
struct ExpressionMarker
7577
: public PostWalker<ExpressionMarker,
7678
UnifiedExpressionVisitor<ExpressionMarker>> {
77-
std::set<Expression*>& marked;
79+
std::unordered_set<Expression*>& marked;
7880

79-
ExpressionMarker(std::set<Expression*>& marked, Expression* expr)
81+
ExpressionMarker(std::unordered_set<Expression*>& marked, Expression* expr)
8082
: marked(marked) {
8183
walk(expr);
8284
}
@@ -122,13 +124,16 @@ struct CodeFolding
122124

123125
// pass state
124126

125-
std::map<Name, std::vector<Tail>> breakTails; // break target name => tails
126-
// that reach it
127+
std::unordered_map<Name, std::vector<Tail>>
128+
breakTails; // break target name => tails
129+
// that reach it
127130
std::vector<Tail> unreachableTails; // tails leading to (unreachable)
128131
std::vector<Tail> returnTails; // tails leading to (return)
129-
std::set<Name> unoptimizables; // break target names that we can't handle
130-
std::set<Expression*> modifieds; // modified code should not be processed
131-
// again, wait for next pass
132+
std::unordered_set<Name>
133+
unoptimizables; // break target names that we can't handle
134+
std::unordered_set<Expression*>
135+
modifieds; // modified code should not be processed
136+
// again, wait for next pass
132137

133138
// walking
134139

@@ -644,17 +649,18 @@ struct CodeFolding
644649
if (next.size() >= 2) {
645650
// now we want to find a mergeable item - any item that is equal among a
646651
// subset
647-
std::map<Expression*, size_t> hashes; // expression => hash value
652+
std::unordered_map<Expression*, size_t>
653+
hashes; // expression => hash value
648654
// hash value => expressions with that hash
649-
std::map<size_t, std::vector<Expression*>> hashed;
655+
std::unordered_map<size_t, std::vector<Expression*>> hashed;
650656
for (auto& tail : next) {
651657
auto* item = getItem(tail, num);
652658
auto hash = hashes[item] = ExpressionAnalyzer::hash(item);
653659
hashed[hash].push_back(item);
654660
}
655661
// look at each hash value exactly once. we do this in a deterministic
656662
// order by iterating over a vector retaining insertion order.
657-
std::set<size_t> seen;
663+
std::unordered_set<size_t> seen;
658664
for (auto& tail : next) {
659665
auto* item = getItem(tail, num);
660666
auto digest = hashes[item];

0 commit comments

Comments
 (0)