Skip to content

Commit 0fc4391

Browse files
add rwlock rationale
1 parent 7f86393 commit 0fc4391

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

storage/src/qmdb/current/batch.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,15 @@ where
679679
/// [`Db::prune`](super::db::Db::prune), and [`Db::rewind`](super::db::Db::rewind) can mutate
680680
/// the bitmap in place while live batches concurrently read through it.
681681
///
682+
/// # Why in-place mutation under a lock
683+
///
684+
/// Snapshot-based alternatives (per-apply clone, page-level copy-on-write, etc.) all require
685+
/// cloning at least the bitmap's top-level pointer structure on every apply. For large DBs that
686+
/// cost grows linearly with the total bit count and every live batch retains its snapshot's
687+
/// memory until dropped, so memory use would grow with both bitmap size and batch lifetime.
688+
/// Mutating in place keeps memory bounded by the actual bitmap size regardless of how many
689+
/// batches are alive or how long they live. The per-call read lock is the cost we pay for that.
690+
///
682691
/// # Reading through invalid batches
683692
///
684693
/// The bitmap behind this lock represents *committed* state. If a caller holds a

storage/src/qmdb/current/db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ where
186186
) -> Result<OpsRootWitness<H::Digest>, Error<F>> {
187187
let storage = self.grafted_storage();
188188
let grafted_root =
189-
compute_grafted_root::<F, H, _, _, N>(hasher, &self.status, &storage).await?;
190-
let partial_chunk = partial_chunk::<_, N>(&self.status)
189+
compute_grafted_root::<F, H, _, _, N>(hasher, self.status.as_ref(), &storage).await?;
190+
let partial_chunk = partial_chunk::<_, N>(self.status.as_ref())
191191
.map(|(chunk, next_bit)| (next_bit, hasher.digest(&chunk)));
192192
Ok(OpsRootWitness {
193193
grafted_root,
@@ -1179,7 +1179,7 @@ mod tests {
11791179
let mut next_idx = 0;
11801180
populate_fixed_db::<mmr::Family, _>(&mut db, next_idx, 256).await;
11811181
next_idx += 256;
1182-
while partial_chunk::<_, 32>(&db.status).is_some() {
1182+
while partial_chunk::<_, 32>(db.status.as_ref()).is_some() {
11831183
populate_fixed_db::<mmr::Family, _>(&mut db, next_idx, 1).await;
11841184
next_idx += 1;
11851185
}

0 commit comments

Comments
 (0)