Skip to content

Commit 6c8a8f7

Browse files
committed
merge bitcoin#24924: Make WalletLoading benchmark run faster
1 parent 7b09514 commit 6c8a8f7

File tree

3 files changed

+91
-17
lines changed

3 files changed

+91
-17
lines changed

src/bench/wallet_loading.cpp

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@
1717
#include <optional>
1818

1919
using wallet::CWallet;
20+
using wallet::DatabaseFormat;
2021
using wallet::DatabaseOptions;
21-
using wallet::DatabaseStatus;
2222
using wallet::ISMINE_SPENDABLE;
2323
using wallet::MakeWalletDatabase;
24+
using wallet::TxStateInactive;
2425
using wallet::WALLET_FLAG_DESCRIPTORS;
2526
using wallet::WalletContext;
27+
using wallet::WalletDatabase;
2628

27-
static const std::shared_ptr<CWallet> BenchLoadWallet(WalletContext& context, DatabaseOptions& options)
29+
static const std::shared_ptr<CWallet> BenchLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, DatabaseOptions& options)
2830
{
29-
DatabaseStatus status;
3031
bilingual_str error;
3132
std::vector<bilingual_str> warnings;
32-
auto database = MakeWalletDatabase("", options, status, error);
33-
assert(database);
3433
auto wallet = CWallet::Create(context, "", std::move(database), options.create_flags, error, warnings);
3534
NotifyWalletLoaded(context, wallet);
3635
if (context.chain) {
@@ -46,9 +45,47 @@ static void BenchUnloadWallet(std::shared_ptr<CWallet>&& wallet)
4645
UnloadWallet(std::move(wallet));
4746
}
4847

48+
static void AddTx(CWallet& wallet)
49+
{
50+
bilingual_str error;
51+
CTxDestination dest;
52+
wallet.GetNewDestination("", dest, error);
53+
54+
CMutableTransaction mtx;
55+
mtx.vout.push_back({COIN, GetScriptForDestination(dest)});
56+
mtx.vin.push_back(CTxIn());
57+
58+
wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInactive{});
59+
}
60+
61+
static std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database, DatabaseOptions& options)
62+
{
63+
auto new_database = CreateMockWalletDatabase(options);
64+
65+
// Get a cursor to the original database
66+
auto batch = database.MakeBatch();
67+
batch->StartCursor();
68+
69+
// Get a batch for the new database
70+
auto new_batch = new_database->MakeBatch();
71+
72+
// Read all records from the original database and write them to the new one
73+
while (true) {
74+
CDataStream key(SER_DISK, CLIENT_VERSION);
75+
CDataStream value(SER_DISK, CLIENT_VERSION);
76+
bool complete;
77+
batch->ReadAtCursor(key, value, complete);
78+
if (complete) break;
79+
new_batch->Write(key, value);
80+
}
81+
82+
return new_database;
83+
}
84+
4985
static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
5086
{
5187
const auto test_setup = MakeNoLogFileContext<TestingSetup>();
88+
test_setup->m_args.ForceSetArg("-unsafesqlitesync", "1");
5289

5390
WalletContext context;
5491
context.args = &test_setup->m_args;
@@ -57,27 +94,40 @@ static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
5794
// Setup the wallet
5895
// Loading the wallet will also create it
5996
DatabaseOptions options;
60-
if (!legacy_wallet) options.create_flags = WALLET_FLAG_DESCRIPTORS;
61-
auto wallet = BenchLoadWallet(context, options);
97+
if (legacy_wallet) {
98+
options.require_format = DatabaseFormat::BERKELEY;
99+
} else {
100+
options.create_flags = WALLET_FLAG_DESCRIPTORS;
101+
options.require_format = DatabaseFormat::SQLITE;
102+
}
103+
auto database = CreateMockWalletDatabase(options);
104+
auto wallet = BenchLoadWallet(std::move(database), context, options);
62105

63106
// Generate a bunch of transactions and addresses to put into the wallet
64-
for (int i = 0; i < 5000; ++i) {
65-
generatetoaddress(test_setup->m_node, getnewaddress(*wallet));
107+
for (int i = 0; i < 1000; ++i) {
108+
AddTx(*wallet);
66109
}
67110

111+
database = DuplicateMockDatabase(wallet->GetDatabase(), options);
112+
68113
// reload the wallet for the actual benchmark
69114
BenchUnloadWallet(std::move(wallet));
70115

71-
bench.minEpochIterations(10).run([&] {
72-
wallet = BenchLoadWallet(context, options);
116+
bench.epochs(5).run([&] {
117+
wallet = BenchLoadWallet(std::move(database), context, options);
73118

74119
// Cleanup
120+
database = DuplicateMockDatabase(wallet->GetDatabase(), options);
75121
BenchUnloadWallet(std::move(wallet));
76122
});
77123
}
78124

125+
#ifdef USE_BDB
79126
static void WalletLoadingLegacy(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/true); }
80-
static void WalletLoadingDescriptors(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/false); }
81-
82127
BENCHMARK(WalletLoadingLegacy);
128+
#endif
129+
130+
#ifdef USE_SQLITE
131+
static void WalletLoadingDescriptors(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/false); }
83132
BENCHMARK(WalletLoadingDescriptors);
133+
#endif

src/wallet/walletdb.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,13 +1235,36 @@ std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase()
12351235
}
12361236

12371237
/** Return object for accessing temporary in-memory database. */
1238-
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase()
1238+
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase(DatabaseOptions& options)
12391239
{
1240-
DatabaseOptions options;
1240+
1241+
std::optional<DatabaseFormat> format;
1242+
if (options.require_format) format = options.require_format;
1243+
if (!format) {
1244+
#ifdef USE_BDB
1245+
format = DatabaseFormat::BERKELEY;
1246+
#endif
12411247
#ifdef USE_SQLITE
1242-
return std::make_unique<SQLiteDatabase>("", "", options, true);
1243-
#elif defined(USE_BDB)
1248+
format = DatabaseFormat::SQLITE;
1249+
#endif
1250+
}
1251+
1252+
if (format == DatabaseFormat::SQLITE) {
1253+
#ifdef USE_SQLITE
1254+
return std::make_unique<SQLiteDatabase>(":memory:", "", options, true);
1255+
#endif
1256+
assert(false);
1257+
}
1258+
1259+
#ifdef USE_BDB
12441260
return std::make_unique<BerkeleyDatabase>(std::make_shared<BerkeleyEnvironment>(), "", options);
12451261
#endif
1262+
assert(false);
1263+
}
1264+
1265+
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase()
1266+
{
1267+
DatabaseOptions options;
1268+
return CreateMockWalletDatabase(options);
12461269
}
12471270
} // namespace wallet

src/wallet/walletdb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, st
267267
std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
268268

269269
/** Return object for accessing temporary in-memory database. */
270+
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase(DatabaseOptions& options);
270271
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
271272
} // namespace wallet
272273

0 commit comments

Comments
 (0)