Skip to content

Commit

Permalink
Remove allocation on translation key lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Jan 19, 2024
1 parent 10d16ea commit ff0a9f2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
10 changes: 4 additions & 6 deletions Common/Data/Text/I18n.cpp
Expand Up @@ -78,26 +78,24 @@ const char *I18NCategory::T(const char *key, const char *def) {
}

// Replace the \n's with \\n's so that key values with newlines will be found correctly.
std::string modifiedKey = key;
modifiedKey = ReplaceAll(modifiedKey, "\n", "\\n");

auto iter = map_.find(modifiedKey);
auto iter = map_.find(key);
if (iter != map_.end()) {
return iter->second.text.c_str();
} else {
std::lock_guard<std::mutex> guard(missedKeyLock_);
if (def)
missedKeyLog_[key] = def;
else
missedKeyLog_[key] = modifiedKey;
missedKeyLog_[key] = key;
return def ? def : key;
}
}

void I18NCategory::SetMap(const std::map<std::string, std::string> &m) {
for (auto iter = m.begin(); iter != m.end(); ++iter) {
if (map_.find(iter->first) == map_.end()) {
std::string text = ReplaceAll(iter->second, "\\n", "\n");
std::string text = ReplaceAll(iter->second, "\n", "\\n");
_dbg_assert_(iter->first.find('\n') == std::string::npos);
map_[iter->first] = I18NEntry(text);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Common/Data/Text/I18n.h
Expand Up @@ -91,15 +91,15 @@ class I18NCategory {
return missedKeyLog_;
}

const std::map<std::string, I18NEntry> &GetMap() { return map_; }
const std::map<std::string, I18NEntry, std::less<>> &GetMap() { return map_; }
void ClearMissed() { missedKeyLog_.clear(); }
void Clear();

private:
I18NCategory(I18NRepo *repo, const char *name) {}
void SetMap(const std::map<std::string, std::string> &m);

std::map<std::string, I18NEntry> map_;
std::map<std::string, I18NEntry, std::less<>> map_;
mutable std::mutex missedKeyLock_;
std::map<std::string, std::string> missedKeyLog_;

Expand Down

0 comments on commit ff0a9f2

Please sign in to comment.