Improve performance of string lookup in AddGRFString

This commit is contained in:
Jonathan G Rennison
2022-05-30 23:51:44 +01:00
parent 9a131a90ca
commit d71e38446e
3 changed files with 35 additions and 12 deletions

View File

@@ -573,23 +573,30 @@ StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool ne
}
uint id;
for (id = 0; id < _num_grf_texts; id++) {
if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
break;
}
}
extern GRFFile *GetFileByGRFIDExpectCurrent(uint32 grfid);
GRFFile *grf = GetFileByGRFIDExpectCurrent(grfid);
if (grf == nullptr) return STR_EMPTY;
/* Too many strings allocated, return empty */
if (id == lengthof(_grf_text)) {
_grf_bug_too_many_strings = true;
return STR_EMPTY;
auto iter = grf->string_map.lower_bound(stringid);
if (iter != grf->string_map.end() && iter->first == stringid) {
/* Found */
id = iter->second;
} else {
/* Allocate new ID */
id = _num_grf_texts;
/* Too many strings allocated, return empty */
if (id == lengthof(_grf_text)) {
_grf_bug_too_many_strings = true;
return STR_EMPTY;
}
grf->string_map.insert(iter, std::make_pair(stringid, id));
_num_grf_texts++;
}
std::string newtext = TranslateTTDPatchCodes(grfid, langid_to_add, allow_newlines, text_to_add);
/* If we didn't find our stringid and grfid in the list, allocate a new id */
if (id == _num_grf_texts) _num_grf_texts++;
if (_grf_text[id].textholder.empty()) {
_grf_text[id].grfid = grfid;
_grf_text[id].stringid = stringid;