perf(lite): cache walletExists() to stop per-frame FFI

While no lite wallet is open the startup auto-open check called
walletExists() every frame. That call crosses into the backend and
touches the filesystem (and prints), so it ran a disk stat on every
frame the app idled at the first-run prompt.

- walletExists() now caches its result for about two seconds on the main
  thread, and the cache is invalidated whenever an open or lifecycle
  result is consumed, since the wallet file may have changed.
- The auto-open condition is reordered so the cheap timer checks gate the
  call, and the click-time overwrite decision uses a new
  walletExistsFresh() that bypasses the cache.

Tests: the cache returns the first queried value within the TTL and the
fresh read re-queries the backend and refreshes the cache.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-10-13 17:42:30 -05:00
co-authored by Claude Opus 4.8
parent 29c1674094
commit d601f786e2
4 changed files with 45 additions and 4 deletions
+8
View File
@@ -193,6 +193,7 @@ public:
// True if a wallet file already exists on disk for this chain. Use to auto-open on startup
// (open via openWallet({}) needs no passphrase — initialize_existing just loads the file).
bool walletExists() const;
bool walletExistsFresh() const; // uncached; also refreshes the cache
const WalletBackendStatus& status() const { return status_; }
LiteWalletLifecycleAvailability availability() const { return lifecycle_.availability(); }
@@ -399,6 +400,13 @@ private:
// mutex would instead serialize sync against syncstatus polling and defeat the design.
std::shared_ptr<LiteClientBridge> bridge_;
std::string chainName_; // backend chain id (for walletExists); from connection settings
// walletExists() is called every frame while no wallet is open; the backend call touches the
// filesystem (and prints). Cache it (main-thread only - no lock). walletExistsFresh() bypasses
// the cache for the click-time overwrite decision.
mutable bool wexCached_ = false;
mutable bool wexValue_ = false;
mutable std::chrono::steady_clock::time_point wexAt_{};
void invalidateWalletExists() { wexCached_ = false; }
LiteConnectionSettings connectionSettings_; // kept for the failover candidate-server list
WalletCapabilities capabilities_; // for thread-local lifecycle services (async path)
LiteWalletLifecycleOptions lifecycleOptions_; // ditto (allowBridgeCalls / rollout gate)