Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize user dictionary lookup performance #940

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/rime/dict/user_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct DfsState {
TickCount present_tick;
Code code;
vector<double> credibility;
map<int, DictEntryList> query_result;
hash_map<int, DictEntryList> query_result;
an<DbAccessor> accessor;
string key;
string value;
Expand All @@ -43,7 +43,8 @@ struct DfsState {
bool IsPrefixMatch(const string& prefix) {
return boost::starts_with(key, prefix);
}
void RecruitEntry(size_t pos, map<string, SyllableId>* syllabary = nullptr);
void RecruitEntry(size_t pos,
hash_map<string, SyllableId>* syllabary = nullptr);
bool NextEntry() {
if (!accessor->GetNextRecord(&key, &value)) {
key.clear();
Expand All @@ -68,7 +69,8 @@ struct DfsState {
}
};

void DfsState::RecruitEntry(size_t pos, map<string, SyllableId>* syllabary) {
void DfsState::RecruitEntry(size_t pos,
hash_map<string, SyllableId>* syllabary) {
string full_code;
auto e = UserDictionary::CreateDictEntry(key, value, present_tick,
credibility.back(),
Expand Down Expand Up @@ -292,7 +294,8 @@ void UserDictionary::DfsLookup(const SyllableGraph& syll_graph,
}
}

static an<UserDictEntryCollector> collect(map<int, DictEntryList>* source) {
static an<UserDictEntryCollector> collect(
hash_map<int, DictEntryList>* source) {
auto result = New<UserDictEntryCollector>();
for (auto& x : *source) {
(*result)[x.first].SetEntries(std::move(x.second));
Expand Down Expand Up @@ -502,7 +505,10 @@ bool UserDictionary::TranslateCodeToString(const Code& code, string* result) {
return false;
result->clear();
for (const SyllableId& syllable_id : code) {
string spelling = table_->GetSyllableById(syllable_id);
string spelling = rev_syllabary_.count(syllable_id)
? rev_syllabary_[syllable_id]
: (rev_syllabary_[syllable_id] =
table_->GetSyllableById(syllable_id));
if (spelling.empty()) {
LOG(ERROR) << "Error translating syllable_id '" << syllable_id << "'.";
result->clear();
Expand Down
5 changes: 3 additions & 2 deletions src/rime/dict/user_dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class UserDictionary : public Class<UserDictionary, const Ticket&> {
an<Db> db_;
an<Table> table_;
an<Prism> prism_;
map<string, SyllableId> syllabary_;
hash_map<string, SyllableId> syllabary_;
hash_map<SyllableId, string> rev_syllabary_;
TickCount tick_ = 0;
time_t transaction_time_ = 0;
};
Expand All @@ -111,7 +112,7 @@ class UserDictionaryComponent : public UserDictionary::Component {
UserDictionary* Create(const string& dict_name, const string& db_class);

private:
map<string, weak<Db>> db_pool_;
hash_map<string, weak<Db>> db_pool_;
};

} // namespace rime
Expand Down