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
+17 -1
View File
@@ -671,6 +671,7 @@ void LiteWalletController::pumpAsyncOpen()
outcome = std::move(openResult_->value());
openResult_->reset();
}
invalidateWalletExists(); // a real open result was consumed: the wallet file may have changed
if (openThread_.joinable()) openThread_.join(); // producer set its result, then exits
if (outcome.ok) {
@@ -842,6 +843,7 @@ void LiteWalletController::pumpLifecycleResult()
out = std::move(lifecycleResult_->value());
lifecycleResult_->reset();
}
invalidateWalletExists(); // a real lifecycle result was consumed: the wallet file may have changed
if (lifecycleThread_.joinable()) lifecycleThread_.join(); // producer set its result, then exits
// Finalize on the main thread: flip walletOpen()/status, persist, start sync/worker on success;
@@ -1400,7 +1402,21 @@ bool LiteWalletController::walletExists() const
{
// chainName_ is always a backend-accepted value ("main"/"test"/"regtest"); litelib_wallet_
// exists panics on unknown chains, but settings migration guarantees a valid one.
return bridge_ && bridge_->walletExists(chainName_);
// Cached ~2s: called every frame while no wallet is open (main-thread only).
const auto now = std::chrono::steady_clock::now();
if (wexCached_ && now - wexAt_ < std::chrono::seconds(2)) return wexValue_;
wexValue_ = bridge_ && bridge_->walletExists(chainName_);
wexCached_ = true;
wexAt_ = now;
return wexValue_;
}
bool LiteWalletController::walletExistsFresh() const
{
wexValue_ = bridge_ && bridge_->walletExists(chainName_);
wexCached_ = true;
wexAt_ = std::chrono::steady_clock::now();
return wexValue_;
}
namespace {