1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-05 00:57:08 +03:00

Revert "[ELF] Compile an Aho-Corasick tree into a DFA"

This reverts commit 31ca2cc2ae
because looks like it's slower than before.
This commit is contained in:
Rui Ueyama 2022-01-22 12:37:40 +09:00
parent 31ca2cc2ae
commit 8eae808223
2 changed files with 13 additions and 30 deletions

View File

@ -1131,22 +1131,14 @@ private:
u32 value = -1; u32 value = -1;
TrieNode *suffix_link = nullptr; TrieNode *suffix_link = nullptr;
std::unique_ptr<TrieNode> children[256]; std::unique_ptr<TrieNode> children[256];
u32 dfa_idx = -1;
};
struct DfaState {
u32 value = -1;
u32 children[256] = {};
}; };
void compile(); void compile();
void fix_suffix_links(TrieNode &node); void fix_suffix_links(TrieNode &node);
void fix_value(); void fix_value();
void encode(TrieNode &node);
std::vector<std::string> strings; std::vector<std::string> strings;
std::unique_ptr<TrieNode> root; std::unique_ptr<TrieNode> root;
std::vector<DfaState> dfa_states;
std::vector<std::pair<GlobPattern, u32>> globs; std::vector<std::pair<GlobPattern, u32>> globs;
std::vector<u16> versions; std::vector<u16> versions;
std::once_flag once_flag; std::once_flag once_flag;

View File

@ -29,11 +29,21 @@ std::optional<u16> VersionMatcher::find(std::string_view str) {
std::call_once(once_flag, [&]() { compile(); }); std::call_once(once_flag, [&]() { compile(); });
// Match against simple glob patterns // Match against simple glob patterns
DfaState *dfa = &dfa_states[0]; TrieNode *node = root.get();
auto walk = [&](u8 c) { auto walk = [&](u8 c) {
dfa = &dfa_states[dfa->children[c]]; for (;;) {
idx = std::min(idx, dfa->value); if (node->children[c]) {
node = node->children[c].get();
idx = std::min(idx, node->value);
return;
}
if (!node->suffix_link)
return;
node = node->suffix_link;
idx = std::min(idx, node->value);
}
}; };
walk('\0'); walk('\0');
@ -107,7 +117,6 @@ bool VersionMatcher::add(std::string_view pat, u16 ver) {
void VersionMatcher::compile() { void VersionMatcher::compile() {
fix_suffix_links(*root); fix_suffix_links(*root);
fix_value(); fix_value();
encode(*root);
compiled = true; compiled = true;
} }
@ -154,22 +163,4 @@ void VersionMatcher::fix_value() {
} while (!queue.empty()); } while (!queue.empty());
} }
void VersionMatcher::encode(TrieNode &node) {
if (node.dfa_idx != -1)
return;
node.dfa_idx = dfa_states.size();
dfa_states.push_back({node.value});
for (i64 i = 0; i < 256; i++) {
for (TrieNode *t = &node; t; t = t->suffix_link) {
if (std::unique_ptr<TrieNode> &next = t->children[i]) {
encode(*next);
dfa_states[node.dfa_idx].children[i] = next->dfa_idx;
break;
}
}
}
}
} // namespace mold::elf } // namespace mold::elf