Skip to content

Commit c8459b6

Browse files
committed
Merge bitcoin/bitcoin#35568: txospenderindex: disable bloom filters to optimize disk usage
6d0ea4c doc: add release notes (Andrew Toth) a2b1c86 txospenderindex: disable bloom filters to optimize disk usage (Andrew Toth) Pull request description: LevelDB bloom filters are only consulted on `Get` point reads. This can be verified in https://github.com/bitcoin/bitcoin/blob/master/src/leveldb/table/table.cc#L224-L228. `InternalGet` is the only place that consults the filter, and it is only reached via a `Get` or `Exists` point read. The filters are never consulted for iterator seeks with an iterator created via `NewIterator`. txospenderindex only reads via iterator seeks, so building them is wasted effort and space. For a db as large as txospenderindex, this results in measurable performance and disk usage. On master, a full sync took 4h37m, and the resulting db was 85.0 GiB. On this branch, a full sync took 3h57m, and the resulting db was 80.9 GiB. So this is a sync speedup of 39 minutes (1.17x), and a disk space reduction of 4.2 GiB. ACKs for top commit: l0rinc: ACK 6d0ea4c sedited: Re-ACK 6d0ea4c fjahr: Code review ACK 6d0ea4c Tree-SHA512: fb88b9f9a16ff31562d388e3fd9fd9590c7864dbe6093cd9430ecbce9cdc3f2a8d3fc612aade743d26ad4c6eca1e5dc9b3f1ca28d75caea1209e5c784895405d
2 parents ef101b0 + 6d0ea4c commit c8459b6

7 files changed

Lines changed: 14 additions & 11 deletions

File tree

doc/release-notes-35634.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
Indexes
22
-------
33

4-
- The transaction output spender index (`-txospenderindex`) now stores one byte
5-
less per spender entry for newly indexed blocks. Existing indexes remain
6-
compatible and no action is required. To apply the same saving to entries
7-
indexed before upgrading, stop the node, delete the
4+
- The transaction output spender index (`-txospenderindex`) now uses less disk
5+
space. Existing indexes remain compatible and no action is required. To reclaim
6+
the space for data indexed before upgrading, stop the node, delete the
87
`<datadir>/indexes/txospenderindex/` directory, and restart with
9-
`-txospenderindex` enabled to rebuild it.
8+
`-txospenderindex` enabled to rebuild it. (#35634, #35568)

src/dbwrapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ static void SetMaxOpenFiles(leveldb::Options *options) {
136136
options->max_open_files, default_open_files);
137137
}
138138

139-
static leveldb::Options GetOptions(size_t nCacheSize)
139+
static leveldb::Options GetOptions(size_t nCacheSize, bool bloom_filter)
140140
{
141141
leveldb::Options options;
142142
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
143143
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
144-
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
144+
options.filter_policy = bloom_filter ? leveldb::NewBloomFilterPolicy(10) : nullptr;
145145
options.compression = leveldb::kNoCompression;
146146
options.info_log = new CBitcoinLevelDBLogger();
147147
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
@@ -225,7 +225,7 @@ CDBWrapper::CDBWrapper(const DBParams& params)
225225
DBContext().iteroptions.verify_checksums = true;
226226
DBContext().iteroptions.fill_cache = false;
227227
DBContext().syncoptions.sync = true;
228-
DBContext().options = GetOptions(params.cache_bytes);
228+
DBContext().options = GetOptions(params.cache_bytes, params.bloom_filter);
229229
DBContext().options.create_if_missing = true;
230230
DBContext().options.max_file_size = params.max_file_size;
231231
assert(!(params.testing_env && params.memory_only));

src/dbwrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ struct DBParams {
5050
//! If true, store data obfuscated via simple XOR. If false, XOR with a
5151
//! zero'd byte array.
5252
bool obfuscate = false;
53+
//! If true, build a LevelDB bloom filter to accelerate point lookups.
54+
bool bloom_filter = true;
5355
//! Passed-through options.
5456
DBOptions options{};
5557
//! If non-null, use this as the leveldb::Env instead of the default.

src/index/base.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash)
6565
return locator;
6666
}
6767

68-
BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) :
68+
BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate, bool f_bloom) :
6969
CDBWrapper{DBParams{
7070
.path = path,
7171
.cache_bytes = n_cache_size,
7272
.memory_only = f_memory,
7373
.wipe_data = f_wipe,
7474
.obfuscate = f_obfuscate,
75+
.bloom_filter = f_bloom,
7576
.options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()}}
7677
{}
7778

src/index/base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class BaseIndex : public CValidationInterface
6565
{
6666
public:
6767
DB(const fs::path& path, size_t n_cache_size,
68-
bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
68+
bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false, bool f_bloom = true);
6969

7070
/// Read block locator of the chain that the index is in sync with.
7171
/// Note, the returned locator will be empty if no record exists.

src/index/txospenderindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct DBKey {
6262
};
6363

6464
TxoSpenderIndex::TxoSpenderIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
65-
: BaseIndex(std::move(chain), "txospenderindex", "txospenderidx"), m_db{std::make_unique<DB>(gArgs.GetDataDirNet() / "indexes" / "txospenderindex" / "db", n_cache_size, f_memory, f_wipe)}
65+
: BaseIndex(std::move(chain), "txospenderindex", "txospenderidx"), m_db{std::make_unique<DB>(gArgs.GetDataDirNet() / "indexes" / "txospenderindex" / "db", n_cache_size, f_memory, f_wipe, /*f_obfuscate=*/false, /*f_bloom=*/false)}
6666
{
6767
if (!m_db->Read("siphash_key", m_siphash_key)) {
6868
FastRandomContext rng(false);

src/test/fuzz/dbwrapper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ DBParams ConsumeDBParams(FuzzedDataProvider& provider, leveldb::Env* testing_env
188188
.path = "dbwrapper_fuzz",
189189
.cache_bytes = provider.ConsumeIntegralInRange<size_t>(64 << 10, 1_MiB),
190190
.obfuscate = obfuscate,
191+
.bloom_filter = provider.ConsumeBool(),
191192
.options = options,
192193
.testing_env = testing_env,
193194
.max_file_size = provider.ConsumeBool()

0 commit comments

Comments
 (0)