Check the account keys from the settings cache

Every frame asked whether this device holds an account by reading its
public and private keys, values too long for the settings cache, so each
frame queried SQLite and copied kilobytes: a twentieth of a Lumi frame on
the Moto. The cache now remembers a long value as present without its
bytes, and the account check asks only whether each key is present.
This commit is contained in:
waozi committed 2026-10-19 14:34:13 -03:00
1 parent a0c05e5e97
commit d25420e61d
3 files changed
+46 -6

No files matched your search

+2 -3
View File
@@ -137,9 +137,8 @@ StoredAccountMode :: () -> s32 {
if delegate_environment {
return AccountModeDelegate
}
if storage_get_setting_text("sync_public_id").count != 0 &&
storage_get_setting_text("sync_public_key").count != 0 &&
storage_get_setting_text("sync_private_key").count != 0 {
if storage_setting_present("sync_public_id") && storage_setting_present("sync_public_key") &&
storage_setting_present("sync_private_key") {
return AccountModeOwner
}
return AccountModeNone
+24 -3
View File
@@ -134,6 +134,7 @@ storage_get_setting_int :: (key: string, fallback: s32) -> s32 {
// Dew, Lumi, sync and app versions read settings on every frame, and each
// read prepared its SQL again: a sixth of a phone's frame. Short values are
// kept until stored data or the user changes (see storage_data_revision).
// A longer value, such as an account key, is only remembered as present.
SETTINGS_CACHE_SLOTS :: 64
SETTINGS_CACHE_KEY :: 96
SETTINGS_CACHE_VALUE :: 512
@@ -141,6 +142,7 @@ SettingsCacheEntry :: struct {
key: [SETTINGS_CACHE_KEY]u8
key_count: s32
value: [SETTINGS_CACHE_VALUE]u8
// -1 for a value too long to keep.
value_count: s32
}
settings_cache: [SETTINGS_CACHE_SLOTS]SettingsCacheEntry;
@@ -178,7 +180,6 @@ SettingsCacheFind :: (key: string) -> s32 {
}
SettingsCacheStore :: (key: string, value: string) {
if value.count >= SETTINGS_CACHE_VALUE { return }
slot := settings_cache_count
if slot < SETTINGS_CACHE_SLOTS {
settings_cache_count += 1
@@ -189,6 +190,10 @@ SettingsCacheStore :: (key: string, value: string) {
entry := *settings_cache[slot]
for at: cast(s32)0..cast(s32)key.count - 1 { entry.key[at] = key[at] }
entry.key_count = cast(s32)key.count
if value.count >= SETTINGS_CACHE_VALUE {
entry.value_count = -1
return
}
for at: cast(s32)0..cast(s32)value.count - 1 { entry.value[at] = value[at] }
entry.value_count = cast(s32)value.count
}
@@ -209,7 +214,7 @@ storage_get_setting_text :: (key: string) -> string {
if cached {
sqlite3_mutex_enter(mutex)
slot := SettingsCacheFind(key)
if slot >= 0 {
if slot >= 0 && settings_cache[slot].value_count >= 0 {
entry := *settings_cache[slot]
for at: cast(s32)0..entry.value_count - 1 { text_value[at] = entry.value[at] }
text_value[entry.value_count] = cast(u8)0
@@ -240,11 +245,27 @@ storage_get_setting_text :: (key: string) -> string {
SqliteFinalize(stmt)
value := ifx text_value[0] != cast(u8)0 then AppTextFromCString(text_value, 8192) else ""
if cached {
SettingsCacheStore(key, value)
if SettingsCacheFind(key) < 0 { SettingsCacheStore(key, value) }
}
return value
}
// Whether a setting has a value, answered from the cache even when the
// value is too long to keep there.
#program_export
storage_setting_present :: (key: string) -> bool {
if storage_db_handle() == null || key.count == 0 { return false }
if key.count < SETTINGS_CACHE_KEY {
mutex := sqlite3_db_mutex(storage_db_handle())
sqlite3_mutex_enter(mutex)
slot := SettingsCacheFind(key)
present := slot >= 0 && settings_cache[slot].value_count != 0
sqlite3_mutex_leave(mutex)
if slot >= 0 { return present }
}
return storage_get_setting_text(key).count != 0
}
#program_export
storage_setting_text_equals :: (key: string, value: string) -> s32 {
stmt: *void = null
@@ -96,6 +96,25 @@ test_unmatched_end_releases_connection :: () {
storage_close()
}
// A value too long for the settings cache is still read whole, and its
// presence comes from the cache until stored data changes.
test_long_values_are_read_and_present :: () {
root := make_root("long")
check_true("init long settings", storage_init(root) != 0)
long_value: [700]u8
for at: cast(s32)0..698 { long_value[at] = cast(u8)(97 + at % 26) }
long_value[699] = 0
long := TextFromBytes(long_value[:699])
storage_set_setting_text("long_key", long)
check_true("long setting is present", storage_setting_present("long_key"))
check_true("long setting reads whole", storage_get_setting_text("long_key") == long)
check_true("long setting reads whole again", storage_get_setting_text("long_key") == long)
check_true("missing setting is absent", !storage_setting_present("missing_key"))
storage_set_setting_text("missing_key", "now")
check_true("a write makes a setting present", storage_setting_present("missing_key"))
storage_close()
}
#program_export
Check :: () -> s32 {
guard := support_begin("/tmp/inbe-storage-settings-threads-zi-test/")
@@ -106,5 +125,6 @@ Check :: () -> s32 {
unused alarm(60)
test_concurrent_batches_commit_and_release()
test_unmatched_end_releases_connection()
test_long_values_are_read_and_present()
return failures
}