Skip to content

Commit 4ae2365

Browse files
codebase should use std::unordered_map instead of std::map for faster lookup
1 parent af98964 commit 4ae2365

2 files changed

Lines changed: 7 additions & 9 deletions

File tree

src/generate_vocabulary.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def generate_base_vocabulary_header():
4141
f"{HEADER_CLANG_FORMAT_OFF}\n"
4242
"#ifndef __VOCABULARY_H__\n"
4343
"#define __VOCABULARY_H__\n\n"
44-
"#include <map>\n"
44+
"#include <unordered_map>\n"
4545
"#include <string>\n"
4646
"#include <vector>\n"
4747
"#include <memory>\n"
@@ -51,7 +51,7 @@ def generate_base_vocabulary_header():
5151
"class VocabularyBase {\n"
5252
"public:\n"
5353
" virtual ~VocabularyBase() {}\n"
54-
" virtual const std::map<std::string, IR2Vec::Vector>& getVocabulary() const = 0;\n"
54+
" virtual const std::unordered_map<std::string, IR2Vec::Vector>& getVocabulary() const = 0;\n"
5555
"};\n\n"
5656
"class VocabularyFactory {\n"
5757
"public:\n"
@@ -69,24 +69,22 @@ def generate_vocabulary_class(vocab_file, class_name):
6969
f"{HEADER_CLANG_FORMAT_OFF}\n"
7070
f"#ifndef __{class_name.upper()}__\n"
7171
f"#define __{class_name.upper()}__\n\n"
72-
f"#include <map>\n"
72+
f"#include <unordered_map>\n"
7373
f"#include <string>\n"
7474
f"#include <vector>\n"
7575
f'#include "Vocabulary.h" // Include the base class\n\n'
7676
f"namespace IR2Vec {{\n\n"
7777
f"class {class_name} : public VocabularyBase {{\n"
7878
f"public:\n"
79-
f" const std::map<std::string, IR2Vec::Vector>& getVocabulary() const override{{\n"
79+
f" const std::unordered_map<std::string, IR2Vec::Vector>& getVocabulary() const override{{\n"
8080
f" return vocabulary;\n"
8181
f" }}\n"
8282
f"private:\n"
83-
f" static const std::map<std::string, IR2Vec::Vector> vocabulary;\n"
83+
f" static const std::unordered_map<std::string, IR2Vec::Vector> vocabulary;\n"
8484
f"}};\n"
8585
)
8686

87-
opening = (
88-
f"\nconst std::map<std::string, IR2Vec::Vector> {class_name}::vocabulary = {{\n"
89-
)
87+
opening = f"\nconst std::unordered_map<std::string, IR2Vec::Vector> {class_name}::vocabulary = {{\n"
9088
closing = """\
9189
};
9290
} // namespace IR2Vec

src/include/IR2Vec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Embeddings {
2828
llvm::SmallMapVector<const llvm::BasicBlock *, IR2Vec::Vector, 16> bbVecMap;
2929
llvm::SmallMapVector<const llvm::Function *, Vector, 16> funcVecMap;
3030
Vector pgmVector;
31-
std::map<std::string, IR2Vec::Vector> vocabulary;
31+
std::unordered_map<std::string, IR2Vec::Vector> vocabulary;
3232

3333
public:
3434
Embeddings() = default;

0 commit comments

Comments
 (0)