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

perf: replace rbtree with vector of pair #684

Merged
merged 1 commit into from
Aug 7, 2023
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
10 changes: 8 additions & 2 deletions src/rime/dict/entry_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//
// 2011-11-27 GONG Chen <chen.sst@gmail.com>
//
#include <algorithm>
#include <fstream>
#include <utility>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <rime/algo/strings.h>
Expand Down Expand Up @@ -192,12 +194,14 @@ void EntryCollector::CreateEntry(const string& word,
bool is_word = (e->raw_code.size() == 1);
if (is_word) {
auto& weights = words[e->text];
if (weights.find(code_str) != weights.end()) {
if (std::find_if(weights.begin(), weights.end(), [&](const auto& p) {
return p.first == code_str;
}) != weights.end()) {
LOG(WARNING) << "duplicate word definition '" << e->text << "': ["
<< code_str << "].";
return;
}
weights[code_str] += e->weight;
weights.push_back(std::make_pair(code_str, e->weight));
total_weight[e->text] += e->weight;
}
entries.emplace_back(std::move(e));
Expand All @@ -214,6 +218,8 @@ bool EntryCollector::TranslateWord(const string& word, vector<string>* result) {
}
const auto& w = words.find(word);
if (w != words.end()) {
std::sort(w->second.begin(), w->second.end(),
[](const auto& a, const auto& b) { return a.first < b.first; });
for (const auto& v : w->second) {
const double kMinimalWeight = 0.05; // 5%
double min_weight = total_weight[word] * kMinimalWeight;
Expand Down
6 changes: 4 additions & 2 deletions src/rime/dict/entry_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ struct RawDictEntry {

// code -> weight
using WeightMap = map<string, double>;
// word -> { code -> weight }
using WordMap = hash_map<string, WeightMap>;
// word -> [ { code, weight } ]
// For the sake of memory usage, don't use word -> { code -> weight } as there
// may be many words, but may not be many representations for a word
using WordMap = hash_map<string, vector<pair<string, double>>>;
// [ (word, weight), ... ]
using EncodeQueue = std::queue<pair<string, string>>;

Expand Down