Cache locale string lookups instead of re-scanning the PO file every call
assetLocaleGetString rewinds and linearly scans/re-decompresses the whole locale file from byte 0 on every single call, with no caching - a text-heavy screen can easily make 10+ of these in a row (e.g. opening the game menu), and on PSP the containing archive is already fully resident in RAM, so the repeated cost is pure CPU (decompression + scanning), not I/O. Adds a fixed 128-entry move-to-front LRU cache keyed by (messageId, pluralCount), capped at 64/256 bytes per key/value (~40KB total) so the cost stays bounded no matter how large the game's script ends up being, rather than caching the whole locale file's text. The cache is a lazily-allocated pointer on assetlocalefile_t, not embedded inline - that struct lives inside the assetloaderoutput_t union shared by every asset type, and all ASSET_ENTRY_COUNT_MAX asset slots carry that union directly, so embedding it would have sized every slot up by ~40KB regardless of what asset type occupies it. Also fixes a bug this surfaced in test_assetlocale.c's own fixture: locale_teardown zeroed the locale struct directly instead of going through assetLocaleDispose, which would have leaked the new cache allocation across tests. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
1 parent
7a858cc424
commit
320c5e6ce5
3 files changed
+484
-10
No files matched your search
@@ -71,6 +71,12 @@ errorret_t assetLocaleDispose(assetentry_t *entry) {
|
||||
assertIsMainThread("Must be called from the main thread.");
|
||||
|
||||
assetlocalefile_t *localeFile = &entry->data.locale;
|
||||
|
||||
if(localeFile->cache != NULL) {
|
||||
memoryFree(localeFile->cache);
|
||||
localeFile->cache = NULL;
|
||||
}
|
||||
|
||||
errorChain(assetFileClose(&localeFile->file));
|
||||
return assetFileDispose(&localeFile->file);
|
||||
}
|
||||
@@ -470,6 +476,83 @@ errorret_t assetLocaleLineUnbuffer(
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetLocaleCacheFind(
|
||||
assetlocalefile_t *file,
|
||||
const char_t *messageId,
|
||||
const int32_t pluralCount,
|
||||
char_t *stringBuffer,
|
||||
const size_t stringBufferSize,
|
||||
bool_t *outHit
|
||||
) {
|
||||
assertNotNull(file, "Asset file cannot be NULL.");
|
||||
assertNotNull(messageId, "Message ID cannot be NULL.");
|
||||
assertNotNull(outHit, "outHit cannot be NULL.");
|
||||
|
||||
*outHit = false;
|
||||
if(file->cache == NULL) errorOk();
|
||||
|
||||
assetlocalecache_t *cache = file->cache;
|
||||
for(uint8_t i = 0; i < ASSET_LOCALE_CACHE_COUNT; i++) {
|
||||
assetlocalecacheentry_t *entry = &cache->entries[i];
|
||||
if(entry->messageId[0] == '\0') break;// unused tail - nothing further
|
||||
if(entry->pluralCount != pluralCount) continue;
|
||||
if(!stringEquals(entry->messageId, messageId)) continue;
|
||||
|
||||
// Move to the front of the LRU order (a no-op when already there).
|
||||
if(i > 0) {
|
||||
assetlocalecacheentry_t hit = *entry;
|
||||
memoryMove(
|
||||
&cache->entries[1], &cache->entries[0], i * sizeof(assetlocalecacheentry_t)
|
||||
);
|
||||
cache->entries[0] = hit;
|
||||
}
|
||||
|
||||
size_t len = strlen(cache->entries[0].value);
|
||||
if(len >= stringBufferSize) errorThrow("String buffer overflow");
|
||||
memoryCopy(stringBuffer, cache->entries[0].value, len + 1);
|
||||
*outHit = true;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void assetLocaleCacheInsert(
|
||||
assetlocalefile_t *file,
|
||||
const char_t *messageId,
|
||||
const int32_t pluralCount,
|
||||
const char_t *value
|
||||
) {
|
||||
assertNotNull(file, "Asset file cannot be NULL.");
|
||||
assertNotNull(messageId, "Message ID cannot be NULL.");
|
||||
assertNotNull(value, "Value cannot be NULL.");
|
||||
|
||||
// Caching is a pure optimization - a message ID or value too long for a
|
||||
// fixed cache slot is just never cached, not an error.
|
||||
if(strlen(messageId) >= ASSET_LOCALE_CACHE_KEY_MAX) return;
|
||||
if(strlen(value) >= ASSET_LOCALE_CACHE_VALUE_MAX) return;
|
||||
|
||||
if(file->cache == NULL) {
|
||||
file->cache = (assetlocalecache_t *)memoryAllocate(sizeof(assetlocalecache_t));
|
||||
memoryZero(file->cache, sizeof(assetlocalecache_t));
|
||||
}
|
||||
|
||||
// Shift every entry down one slot, dropping the least-recently-used one
|
||||
// off the end, to make room for the new entry at the front. This assumes
|
||||
// messageId is never already present elsewhere in the cache, which holds
|
||||
// as long as callers only insert after a confirmed assetLocaleCacheFind
|
||||
// miss (a hit would have returned before reaching this point).
|
||||
memoryMove(
|
||||
&file->cache->entries[1], &file->cache->entries[0],
|
||||
(ASSET_LOCALE_CACHE_COUNT - 1) * sizeof(assetlocalecacheentry_t)
|
||||
);
|
||||
|
||||
assetlocalecacheentry_t *entry = &file->cache->entries[0];
|
||||
stringCopy(entry->messageId, messageId, ASSET_LOCALE_CACHE_KEY_MAX);
|
||||
entry->pluralCount = pluralCount;
|
||||
stringCopy(entry->value, value, ASSET_LOCALE_CACHE_VALUE_MAX);
|
||||
}
|
||||
|
||||
errorret_t assetLocaleGetString(
|
||||
assetlocalefile_t *file,
|
||||
const char_t *messageId,
|
||||
@@ -482,6 +565,13 @@ errorret_t assetLocaleGetString(
|
||||
assertTrue(pluralCount >= 0, "Plural index cannot be negative.");
|
||||
assertNotNull(stringBuffer, "String buffer cannot be NULL.");
|
||||
assertTrue(stringBufferSize > 0, "String buffer size must be > 0");
|
||||
|
||||
bool_t cacheHit = false;
|
||||
errorChain(assetLocaleCacheFind(
|
||||
file, messageId, pluralCount, stringBuffer, stringBufferSize, &cacheHit
|
||||
));
|
||||
if(cacheHit) errorOk();
|
||||
|
||||
assetfilelinereader_t reader;
|
||||
|
||||
bool_t msgidFound = false, msgidPluralFound = false, msgstrFound = false;
|
||||
@@ -625,6 +715,8 @@ errorret_t assetLocaleGetString(
|
||||
errorThrow("Failed to find msgstr for message ID: %s", messageId);
|
||||
}
|
||||
|
||||
assetLocaleCacheInsert(file, messageId, pluralCount, stringBuffer);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user