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
Diff view
64 changes: 51 additions & 13 deletions atomic-repository/src/repository/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,8 +1092,13 @@ where
/// transaction and visibility closure. `retrieve_span_if_present` builds a
/// fresh context per call — which decodes and verifies the whole visible
/// closure — so a per-file caller on a large view pays the closure decode
/// thousands of times; holders of this struct pay it once. Safe to share
/// across rayon workers: first use builds, the rest reuse.
/// thousands of times; holders of this struct pay it once.
///
/// Building the context runs rayon jobs, so a caller never waits for another
/// caller's build: the worker building it may itself be waiting for a worker
/// that needs the context for another file of the same walk. A caller that
/// finds none builds its own, and the first one finished is kept. Parallel
/// walks call [`Self::prime`] first so that only one is ever built.
pub(super) struct SharedSpanContext<'a, T, C>
where
T: atomic_core::pristine::span_patch_storage::SpanPatchReadTxn
Expand Down Expand Up @@ -1135,22 +1140,55 @@ where
if !has_visible_span_root(self.txn, self.store, inode, self.visibility)? {
return Ok(None);
}
let context = self
.context
.get_or_init(|| {
atomic_core::pristine::span_patch_storage::SpanRenderContext::new(
self.context()?
.render(inode)
.map(Some)
.map_err(|error| RepositoryError::Database(error.to_string()))
}

/// Build the context on the calling thread when any of `inodes` is
/// span-rooted on this view, so that a parallel walk over them shares one
/// build instead of every worker that arrives early making its own. A
/// failure is left for the walk to report at the path that hits it.
pub(super) fn prime(&self, inodes: impl IntoIterator<Item = Inode>) {
let any_span_root = inodes.into_iter().any(|inode| {
// Only an indexed root can need the context. Asking about a file
// without one would load its origin change, which the walk does
// for each file on its own workers.
matches!(self.txn.span_root(inode), Ok(Some(_)))
&& matches!(
has_visible_span_root(self.txn, self.store, inode, self.visibility),
Ok(true)
)
});
if any_span_root {
// The outcome, an error included, is stored for the renders.
let _stored = self.context();
}
}

fn context(
&self,
) -> Result<&atomic_core::pristine::span_patch_storage::SpanRenderContext<'a, T>, RepositoryError>
{
let stored = match self.context.get() {
Some(stored) => stored,
None => {
// Built outside the cell: initialising it in place would park
// every other caller behind a build that may be waiting for
// one of them.
let built = atomic_core::pristine::span_patch_storage::SpanRenderContext::new(
self.txn,
self.store,
self.visibility,
)
.map_err(|error| error.to_string())
})
.map_err(|error| error.to_string());
self.context.get_or_init(|| built)
}
};
stored
.as_ref()
.map_err(|error| RepositoryError::Database(error.clone()))?;
context
.render(inode)
.map(Some)
.map_err(|error| RepositoryError::Database(error.to_string()))
.map_err(|error| RepositoryError::Database(error.clone()))
}
}

Expand Down
11 changes: 10 additions & 1 deletion atomic-repository/src/repository/materialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2605,9 +2605,18 @@ impl Repository {
let filter = crate::content_filter::GitAttributesFilter::for_repository(&self.root);
// One span context per walk: per-file contexts each decode and
// verify the whole visible closure, which on a large span-backed
// view costs more than the render itself.
// view costs more than the render itself. It is built here rather
// than by whichever workers reach a span-rooted file first.
let shared_spans =
super::content::SharedSpanContext::new(&txn, &self.change_store, &visibility);
shared_spans.prime(
paths
.iter()
.filter(|path| !conflict_entries.contains_key(*path))
.map(|path| &projection.present[path])
.filter(|item| !item.is_directory)
.map(|item| item.inode),
);
for chunk in paths.chunks(256) {
let rendered: Vec<Result<ViewEntry, RepositoryError>> = chunk
.par_iter()
Expand Down
1 change: 1 addition & 0 deletions atomic-repository/src/repository/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod tag_projection_tests;
mod tracking_tests;
mod tree_projection_tests;
mod verify_tests;
mod view_entries_span_context_tests;
mod view_tests;
mod working_copy_identity_tests;
mod working_copy_reconcile_tests;
Expand Down
Loading
Loading