8000 perf: fix backend mutex serialisation in CommittedFactLoaderImpl::resolve (#252) by adityamukho · Pull Request #279 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
14 changes: 6 additions & 8 deletions src/storage/btree_v6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,12 @@ where
/// Read-only [`StorageBackend`] adapter that locks `Arc<Mutex<B>>` only for the
/// duration of a single [`StorageBackend::read_page`] call.
///
/// Used exclusively by [`OnDiskIndexReader::range_scan_*`] so that the backend
/// mutex is held only while reading one cold page from disk, rather than for the
/// entire range scan. On a cache hit [`PageCache::get_or_load`] never calls
/// `read_page`, so no lock is acquired at all. All methods other than `read_page`
/// are unimplemented and will panic if called.
// Instantiated inside OnDiskIndexReader::range_scan_* methods.
#[allow(dead_code)]
struct MutexStorageBackend<B>(Arc<Mutex<B>>);
/// Used by [`OnDiskIndexReader::range_scan_*`] and [`crate::storage::persistent_facts`]
/// so that the backend mutex is held only while reading one cold page from disk,
/// rather than for the entire operation. On a cache hit [`PageCache::get_or_load`]
/// never calls `read_page`, so no lock is acquired at all. All methods other than
/// `read_page` are unimplemented and will panic if called.
pub(crate) struct MutexStorageBackend<B>(pub(crate) Arc<Mutex<B>>);

impl<B: StorageBackend> StorageBackend for MutexStorageBackend<B> {
fn read_page(&self, page_id: u64) -> anyhow::Result<Vec<u8>> {
Expand Down
14 changes: 8 additions & 6 deletions src/storage/persistent_facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::graph::types::Fact;
use crate::storage::FACT_PAGE_FORMAT_PACKED;
use crate::storage::btree::{read_aevt_index, read_avet_index, read_eavt_index, read_vaet_index};
use crate::storage::btree_v6::{
OnDiskIndexReader, btree_entries, build_btree, merge_sorted_vecs, stream_all_entries,
MutexStorageBackend, OnDiskIndexReader, btree_entries, build_btree, merge_sorted_vecs,
stream_all_entries,
};
use crate::storage::cache::PageCache;
use crate::storage::index::{AevtKey, AvetKey, EavtKey, FactRef, VaetKey, encode_value};
Expand Down Expand Up @@ -63,11 +64,12 @@ impl<B: StorageBackend + 'static> crate::storage::CommittedFactReader
&self,
fact_ref: crate::storage::index::FactRef,
) -> anyhow::Result<crate::graph::types::Fact> {
let backend = self
.backend
.lock()
.map_err(|_| anyhow::anyhow!("backend mutex poisoned"))?;
let page = self.page_cache.get_or_load(fact_ref.page_id, &*backend)?;
// Use MutexStorageBackend so the backend mutex is acquired only when
// get_or_load actually needs to read a cold page from disk. On cache
// hits get_or_load returns without ever calling read_page, so no lock
// is acquired and concurrent readers do not serialize.
let adapter = MutexStorageBackend(Arc::clone(&self.backend));
let page = self.page_cache.get_or_load(fact_ref.page_id, &adapter)?;
crate::storage::packed_pages::read_slot(&page, fact_ref.slot_index)
}

Expand Down
Loading
0