mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-22 06:28:39 +02:00
CW-1024 Improve performance of xmr wallet (#2128)
* - enabled development options in CI builds. - Implemented caching for account retrieval. - refactor transaction handling in `dashboard_view_model.dart` to improve efficiency and reduce unnecessary updates in xmr. - `DevMoneroCallProfilerPage`, for profiling performance of xmr,wow,zano wallet calls. * use FeatureFlag.hasDevOptions * prevent crashes in monero_c by using mutexes properly improve performance of _transactionDisposer remove unnecessary checks * remove logging, bring back simplified logic * update _transactionDisposer on length and confirmation of first and last transaction * address comments from review * don't throw unhandled exceptions in unawaited async code * use cached transaction list in getAllSubaddresses, fix usage of txHistoryMutex * [DNM] fix: crashes when opening wallet, performance issue when syncing and update dependencies * Revert "use cached transaction list in getAllSubaddresses, fix usage of txHistoryMutex" This reverts commit 4c4c33ac6a47603e970a6c8d940e90204525b241. * Revert "[DNM] fix: crashes when opening wallet, performance issue when syncing and update dependencies" This reverts commit d7603445ad6ae76d76bf179c34728ce242c8c610. * Revert "use cached transaction list in getAllSubaddresses, fix usage of txHistoryMutex" This reverts commit 4c4c33ac6a47603e970a6c8d940e90204525b241. * update shared_preferences * improve state management performance by not rendering multiple changes in transaction screen on a single frame * fix wallet switching
This commit is contained in:
@@ -35,6 +35,7 @@ import 'package:cake_wallet/entities/hardware_wallet/require_hardware_wallet_con
|
||||
import 'package:cake_wallet/entities/parse_address_from_domain.dart';
|
||||
import 'package:cake_wallet/exchange/provider/trocador_exchange_provider.dart';
|
||||
import 'package:cake_wallet/src/screens/dev/monero_background_sync.dart';
|
||||
import 'package:cake_wallet/src/screens/dev/moneroc_call_profiler.dart';
|
||||
import 'package:cake_wallet/src/screens/settings/background_sync_page.dart';
|
||||
import 'package:cake_wallet/view_model/dev/monero_background_sync.dart';
|
||||
import 'package:cake_wallet/view_model/link_view_model.dart';
|
||||
@@ -1450,5 +1451,6 @@ Future<void> setup({
|
||||
getIt.registerFactory(() => SeedVerificationPage(getIt.get<WalletSeedViewModel>()));
|
||||
|
||||
getIt.registerFactory(() => DevMoneroBackgroundSyncPage(getIt.get<DevMoneroBackgroundSync>()));
|
||||
getIt.registerFactory(() => DevMoneroCallProfilerPage());
|
||||
_isSetupFinished = true;
|
||||
}
|
||||
|
||||
@@ -424,4 +424,10 @@ class CWMonero extends Monero {
|
||||
bool isViewOnly() {
|
||||
return isViewOnlyBySpendKey(null);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, List<int>> debugCallLength() {
|
||||
return monero_wallet_api.debugCallLength();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ import 'package:cake_wallet/src/screens/dashboard/pages/nft_details_page.dart';
|
||||
import 'package:cake_wallet/src/screens/dashboard/pages/transactions_page.dart';
|
||||
import 'package:cake_wallet/src/screens/dashboard/sign_page.dart';
|
||||
import 'package:cake_wallet/src/screens/dev/monero_background_sync.dart';
|
||||
import 'package:cake_wallet/src/screens/dev/moneroc_call_profiler.dart';
|
||||
import 'package:cake_wallet/src/screens/disclaimer/disclaimer_page.dart';
|
||||
import 'package:cake_wallet/src/screens/exchange/exchange_page.dart';
|
||||
import 'package:cake_wallet/src/screens/exchange/exchange_template_page.dart';
|
||||
@@ -841,6 +842,11 @@ Route<dynamic> createRoute(RouteSettings settings) {
|
||||
builder: (_) => getIt.get<DevMoneroBackgroundSyncPage>(),
|
||||
);
|
||||
|
||||
case Routes.devMoneroCallProfiler:
|
||||
return MaterialPageRoute<void>(
|
||||
builder: (_) => getIt.get<DevMoneroCallProfilerPage>(),
|
||||
);
|
||||
|
||||
default:
|
||||
return MaterialPageRoute<void>(
|
||||
builder: (_) => Scaffold(
|
||||
|
||||
+1
-1
@@ -112,7 +112,7 @@ class Routes {
|
||||
static const torPage = '/tor_page';
|
||||
static const backgroundSync = '/background_sync';
|
||||
static const devMoneroBackgroundSync = '/dev/monero_background_sync';
|
||||
|
||||
static const devMoneroCallProfiler = '/dev/monero_call_profiler';
|
||||
static const signPage = '/sign_page';
|
||||
static const connectDevices = '/device/connect';
|
||||
static const urqrAnimatedPage = '/urqr/animated_page';
|
||||
|
||||
@@ -0,0 +1,253 @@
|
||||
// code shamelessly stolen from xmruw
|
||||
// https://raw.githubusercontent.com/MrCyjaneK/unnamed_monero_wallet/refs/heads/master-rewrite/lib/pages/debug/performance.dart
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:cake_wallet/di.dart';
|
||||
import 'package:cake_wallet/monero/monero.dart';
|
||||
import 'package:cake_wallet/src/widgets/primary_button.dart';
|
||||
import 'package:cake_wallet/view_model/dashboard/dashboard_view_model.dart';
|
||||
import 'package:cake_wallet/wownero/wownero.dart';
|
||||
import 'package:cake_wallet/zano/zano.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:cake_wallet/src/screens/base_page.dart';
|
||||
|
||||
class DevMoneroCallProfilerPage extends BasePage {
|
||||
DevMoneroCallProfilerPage();
|
||||
|
||||
@override
|
||||
String? get title => "[dev] xmr call profiler";
|
||||
|
||||
@override
|
||||
Widget body(BuildContext context) {
|
||||
return PerformanceDebug();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class PerformanceDebug extends StatefulWidget {
|
||||
const PerformanceDebug({super.key});
|
||||
|
||||
@override
|
||||
State<PerformanceDebug> createState() => _PerformanceDebugState();
|
||||
}
|
||||
|
||||
enum ProfilableWallet {
|
||||
monero,
|
||||
wownero,
|
||||
zano,
|
||||
}
|
||||
|
||||
class _PerformanceDebugState extends State<PerformanceDebug> {
|
||||
List<Widget> widgets = [];
|
||||
|
||||
final dashboardViewModel = getIt.get<DashboardViewModel>();
|
||||
|
||||
late ProfilableWallet wallet = switch (dashboardViewModel.wallet.type) {
|
||||
WalletType.monero => ProfilableWallet.monero,
|
||||
WalletType.wownero => ProfilableWallet.wownero,
|
||||
WalletType.zano => ProfilableWallet.zano,
|
||||
_ => throw Exception("Unknown wallet type"),
|
||||
};
|
||||
final precalc = 1700298;
|
||||
|
||||
late Map<String, List<int>> debugCallLength = switch (wallet) {
|
||||
ProfilableWallet.monero => monero!.debugCallLength(),
|
||||
ProfilableWallet.wownero => wownero!.debugCallLength(),
|
||||
ProfilableWallet.zano => zano!.debugCallLength(),
|
||||
};
|
||||
|
||||
int getOpenWalletTime() {
|
||||
if (debugCallLength["MONERO_Wallet_init"] == null) {
|
||||
return precalc;
|
||||
}
|
||||
if (debugCallLength["MONERO_Wallet_init"]!.isEmpty) {
|
||||
return precalc;
|
||||
}
|
||||
return debugCallLength["MONERO_Wallet_init"]!.last;
|
||||
}
|
||||
|
||||
late final String perfInfo = """
|
||||
---- Performance tuning
|
||||
This page lists all calls that take place during the app runtime.-
|
||||
As per Flutter docs we can read:
|
||||
> Flutter aims to provide 60 frames per second (fps) performance, or 120 fps-
|
||||
performance on devices capable of 120Hz updates.
|
||||
|
||||
With that in mind we will aim to render frames every 8.3ms (~8333 µs). It is-
|
||||
however acceptable to reach 16.6 ms (~16666 µs) but we should also keep in mind-
|
||||
that there are also UI costs that aren't part of this benchmark.
|
||||
|
||||
For some calls it is also acceptable to exceed this amount of time, for example-
|
||||
MONERO_Wallet_init takes ~${getOpenWalletTime()}µs-
|
||||
(${(getOpenWalletTime() / frameTime).toStringAsFixed(2)} frames). That time would-
|
||||
be unnaceptable in most situations but since we call this function only when-
|
||||
opening the wallet it is completely fine to freeze the UI for the time being --
|
||||
as the user won't even notice that something happened.
|
||||
|
||||
---- Details
|
||||
count: how many times did we call this function [total time (% of frame)]
|
||||
average: average execution time (% of frame)
|
||||
min: fastest execution (% of frame)
|
||||
max: slowest execution (% of frame)
|
||||
95th: 95% of the time, the function is faster than this amount of time (% of frame)
|
||||
"""
|
||||
.split("-\n")
|
||||
.join(" ");
|
||||
|
||||
late final frameTime = 8333;
|
||||
late final frameGreenTier = frameTime ~/ 100;
|
||||
late final frameBlueTier = frameTime ~/ 10;
|
||||
late final frameBlueGreyTier = frameTime ~/ 2;
|
||||
late final frameYellowTier = frameTime;
|
||||
late final frameOrangeTier = frameTime * 2;
|
||||
|
||||
Color? perfc(num frame) {
|
||||
if (frame < frameGreenTier) return Colors.green;
|
||||
if (frame < frameBlueTier) return Colors.blue;
|
||||
if (frame < frameBlueGreyTier) return Colors.blueGrey;
|
||||
if (frame < frameGreenTier) return Colors.green;
|
||||
if (frame < frameYellowTier) return Colors.yellow;
|
||||
if (frame < frameOrangeTier) return Colors.orange;
|
||||
return Colors.red;
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_buildWidgets();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
SelectableText cw(String text, Color? color) {
|
||||
return SelectableText(
|
||||
text,
|
||||
style: TextStyle(color: color),
|
||||
);
|
||||
}
|
||||
|
||||
void _buildWidgets() {
|
||||
List<Widget> ws = [];
|
||||
ws.add(Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SelectableText(perfInfo),
|
||||
cw("< 1% of a frame (max: $frameGreenTierµs)", Colors.green),
|
||||
cw("< 10% of a frame (max: $frameBlueTierµs)", Colors.blue),
|
||||
cw("< 50% of a frame (max: $frameBlueGreyTierµs)", Colors.blueGrey),
|
||||
cw("< 100% of a frame (max: $frameYellowTierµs)", Colors.yellow),
|
||||
cw("< 200% of a frame (max: $frameOrangeTierµs)", Colors.orange),
|
||||
cw("> 200% of a frame (UI junk visible)", Colors.red),
|
||||
],
|
||||
));
|
||||
final keys = debugCallLength.keys.toList();
|
||||
keys.sort((s1, s2) =>
|
||||
_n95th(debugCallLength[s2]!) -
|
||||
_n95th(debugCallLength[s1]!));
|
||||
for (var key in keys) {
|
||||
final value = debugCallLength[key];
|
||||
if (value == null) continue;
|
||||
final avg = _avg(value);
|
||||
final min = _min(value);
|
||||
final max = _max(value);
|
||||
final np = _n95th(value);
|
||||
final total = _total(value);
|
||||
ws.add(
|
||||
Card(
|
||||
child: ListTile(
|
||||
title: Text(
|
||||
key,
|
||||
style: TextStyle(color: perfc(np)),
|
||||
),
|
||||
subtitle: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(children: [
|
||||
cw("count: ${value.length}", null),
|
||||
const Spacer(),
|
||||
cw("${_str(total / 1000)}ms", perfc(total)),
|
||||
]),
|
||||
cw("average: ${_str(avg)}µs (~${_str(avg / (frameTime))}f)",
|
||||
perfc(avg)),
|
||||
cw("min: $minµs (~${_str(min / (frameTime) * 100)})",
|
||||
perfc(min)),
|
||||
cw("max: $maxµs (~${_str(max / (frameTime) * 100)}%)",
|
||||
perfc(max)),
|
||||
cw("95th: $npµs (~${_str(np / (frameTime) * 100)}%)",
|
||||
perfc(np)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (debugCallLength.isNotEmpty) {
|
||||
ws.add(
|
||||
PrimaryButton(
|
||||
text: "Purge statistics",
|
||||
onPressed: _purgeStats,
|
||||
color: Colors.red,
|
||||
textColor: Colors.white,
|
||||
),
|
||||
);
|
||||
}
|
||||
setState(() {
|
||||
widgets = ws;
|
||||
});
|
||||
}
|
||||
|
||||
void _purgeStats() {
|
||||
debugCallLength.clear();
|
||||
_buildWidgets();
|
||||
}
|
||||
|
||||
int _min(List<int> l) {
|
||||
return l.reduce(min);
|
||||
}
|
||||
|
||||
int _max(List<int> l) {
|
||||
return l.reduce(max);
|
||||
}
|
||||
|
||||
int _n95th(List<int> l) {
|
||||
final l0 = l.toList();
|
||||
l0.sort();
|
||||
int i = (0.95 * l.length).ceil() - 1;
|
||||
return l0[i];
|
||||
}
|
||||
|
||||
double _avg(List<int> l) {
|
||||
int c = 0;
|
||||
for (var i = 0; i < l.length; i++) {
|
||||
c += l[i];
|
||||
}
|
||||
return c / l.length;
|
||||
}
|
||||
|
||||
int _total(List<int> l) {
|
||||
int c = 0;
|
||||
for (var i = 0; i < l.length; i++) {
|
||||
c += l[i];
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
String _str(num d) => d.toStringAsFixed(2);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: Column(
|
||||
children: widgets,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import 'package:cake_wallet/src/screens/settings/widgets/settings_cell_with_arro
|
||||
import 'package:cake_wallet/src/screens/settings/widgets/settings_picker_cell.dart';
|
||||
import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart';
|
||||
import 'package:cake_wallet/src/screens/settings/widgets/settings_version_cell.dart';
|
||||
import 'package:cake_wallet/utils/feature_flag.dart';
|
||||
import 'package:cake_wallet/view_model/settings/other_settings_view_model.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -64,12 +65,18 @@ class OtherSettingsPage extends BasePage {
|
||||
handler: (BuildContext context) =>
|
||||
Navigator.of(context).pushNamed(Routes.readDisclaimer),
|
||||
),
|
||||
if (kDebugMode && _otherSettingsViewModel.walletType == WalletType.monero)
|
||||
if (FeatureFlag.hasDevOptions && _otherSettingsViewModel.walletType == WalletType.monero)
|
||||
SettingsCellWithArrow(
|
||||
title: '[dev] monero background sync',
|
||||
handler: (BuildContext context) =>
|
||||
Navigator.of(context).pushNamed(Routes.devMoneroBackgroundSync),
|
||||
),
|
||||
if (FeatureFlag.hasDevOptions && [WalletType.monero, WalletType.wownero, WalletType.zano].contains(_otherSettingsViewModel.walletType))
|
||||
SettingsCellWithArrow(
|
||||
title: '[dev] xmr call profiler',
|
||||
handler: (BuildContext context) =>
|
||||
Navigator.of(context).pushNamed(Routes.devMoneroCallProfiler),
|
||||
),
|
||||
Spacer(),
|
||||
SettingsVersionCell(
|
||||
title: S.of(context).version(_otherSettingsViewModel.currentVersion)),
|
||||
|
||||
@@ -6,4 +6,5 @@ class FeatureFlag {
|
||||
static const bool isInAppTorEnabled = false;
|
||||
static const bool isBackgroundSyncEnabled = true;
|
||||
static const int verificationWordsCount = kDebugMode ? 0 : 2;
|
||||
static const bool hasDevOptions = bool.fromEnvironment('hasDevOptions', defaultValue: kDebugMode);
|
||||
}
|
||||
@@ -271,32 +271,10 @@ abstract class DashboardViewModelBase with Store {
|
||||
});
|
||||
|
||||
_transactionDisposer?.reaction.dispose();
|
||||
|
||||
_transactionDisposer = reaction(
|
||||
(_) => appStore.wallet!.transactionHistory.transactions.values.toList(),
|
||||
(List<TransactionInfo> txs) {
|
||||
|
||||
transactions.clear();
|
||||
|
||||
transactions.addAll(
|
||||
txs.where((tx) {
|
||||
if (wallet.type == WalletType.monero) {
|
||||
return monero!.getTransactionInfoAccountId(tx) == monero!.getCurrentAccount(wallet).id;
|
||||
}
|
||||
if (wallet.type == WalletType.wownero) {
|
||||
return wow.wownero!.getTransactionInfoAccountId(tx) == wow.wownero!.getCurrentAccount(wallet).id;
|
||||
}
|
||||
return true;
|
||||
}).map(
|
||||
(tx) => TransactionListItem(
|
||||
transaction: tx,
|
||||
balanceViewModel: balanceViewModel,
|
||||
settingsStore: appStore.settingsStore,
|
||||
key: ValueKey('${wallet.type.name}_transaction_history_item_${tx.id}_key'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
(_) => appStore.wallet!.transactionHistory.transactions.length *
|
||||
appStore.wallet!.transactionHistory.transactions.values.first.confirmations,
|
||||
_transactionDisposerCallback
|
||||
);
|
||||
|
||||
if (hasSilentPayments) {
|
||||
@@ -311,6 +289,50 @@ abstract class DashboardViewModelBase with Store {
|
||||
reaction((_) => settingsStore.mwebAlwaysScan, (bool value) => _checkMweb());
|
||||
}
|
||||
|
||||
|
||||
bool _isTransactionDisposerCallbackRunning = false;
|
||||
|
||||
void _transactionDisposerCallback(int _) async {
|
||||
// Simple check to prevent the callback from being called multiple times in the same frame
|
||||
if (_isTransactionDisposerCallbackRunning) return;
|
||||
_isTransactionDisposerCallbackRunning = true;
|
||||
await Future.delayed(Duration.zero);
|
||||
|
||||
|
||||
try {
|
||||
final currentAccountId = wallet.type == WalletType.monero
|
||||
? monero!.getCurrentAccount(wallet).id
|
||||
: wallet.type == WalletType.wownero
|
||||
? wow.wownero!.getCurrentAccount(wallet).id
|
||||
: null;
|
||||
final List<TransactionInfo> relevantTxs = [];
|
||||
|
||||
for (final tx in appStore.wallet!.transactionHistory.transactions.values) {
|
||||
bool isRelevant = true;
|
||||
if (wallet.type == WalletType.monero) {
|
||||
isRelevant = monero!.getTransactionInfoAccountId(tx) == currentAccountId;
|
||||
} else if (wallet.type == WalletType.wownero) {
|
||||
isRelevant = wow.wownero!.getTransactionInfoAccountId(tx) == currentAccountId;
|
||||
}
|
||||
|
||||
if (isRelevant) {
|
||||
relevantTxs.add(tx);
|
||||
}
|
||||
}
|
||||
// printV("Transaction disposer callback (relevantTxs: ${relevantTxs.length} current: ${transactions.length})");
|
||||
|
||||
transactions.clear();
|
||||
transactions.addAll(relevantTxs.map((tx) => TransactionListItem(
|
||||
transaction: tx,
|
||||
balanceViewModel: balanceViewModel,
|
||||
settingsStore: appStore.settingsStore,
|
||||
key: ValueKey('${wallet.type.name}_transaction_history_item_${tx.id}_key'),
|
||||
)));
|
||||
} finally {
|
||||
_isTransactionDisposerCallbackRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
void _checkMweb() {
|
||||
if (hasMweb) {
|
||||
mwebEnabled = bitcoin!.getMwebEnabled(wallet);
|
||||
@@ -789,30 +811,9 @@ abstract class DashboardViewModelBase with Store {
|
||||
_transactionDisposer?.reaction.dispose();
|
||||
|
||||
_transactionDisposer = reaction(
|
||||
(_) => appStore.wallet!.transactionHistory.transactions.values.toList(),
|
||||
(List<TransactionInfo> txs) {
|
||||
|
||||
transactions.clear();
|
||||
|
||||
transactions.addAll(
|
||||
txs.where((tx) {
|
||||
if (wallet.type == WalletType.monero) {
|
||||
return monero!.getTransactionInfoAccountId(tx) == monero!.getCurrentAccount(wallet).id;
|
||||
}
|
||||
if (wallet.type == WalletType.wownero) {
|
||||
return wow.wownero!.getTransactionInfoAccountId(tx) == wow.wownero!.getCurrentAccount(wallet).id;
|
||||
}
|
||||
return true;
|
||||
}).map(
|
||||
(tx) => TransactionListItem(
|
||||
transaction: tx,
|
||||
balanceViewModel: balanceViewModel,
|
||||
settingsStore: appStore.settingsStore,
|
||||
key: ValueKey('${wallet.type.name}_transaction_history_item_${tx.id}_key'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
(_) => appStore.wallet!.transactionHistory.transactions.length *
|
||||
appStore.wallet!.transactionHistory.transactions.values.first.confirmations,
|
||||
_transactionDisposerCallback
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -361,4 +361,9 @@ class CWWownero extends Wownero {
|
||||
void wownerocCheck() {
|
||||
checkIfMoneroCIsFine();
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, List<int>> debugCallLength() {
|
||||
return wownero_wallet_api.debugCallLength();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,4 +131,9 @@ class CWZano extends Zano {
|
||||
|
||||
@override
|
||||
bool validateAddress(String address) => ZanoUtils.validateAddress(address);
|
||||
|
||||
@override
|
||||
Map<String, List<int>> debugCallLength() {
|
||||
return api.debugCallLength();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user