Skip to content

Commit

Permalink
index: move Revset::change_id_index() to Index
Browse files Browse the repository at this point in the history
We current have `Revset::change_id_index()` for creating a
`ChangeIdIndex` for a given revset. I think it will be hard to make it
performant for general revsets, especially in very large repos and
with custom index implementations, like the one we have at Google. If
we instead restrict it to including all ancestors of a set of heads, I
think it will be much easier to implement. We only use
`Revset::change_id_index()` with revsets including all visible commits
today, so we won't lose any current functionality by making it more
restricted.
  • Loading branch information
martinvonz committed Jan 7, 2024
1 parent 595d4a8 commit 72c6155
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 32 deletions.
18 changes: 16 additions & 2 deletions lib/src/default_index/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ impl<'a> CompositeIndex<'a> {
candidate_positions
}

pub(super) fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + 'a> {
Box::new(ChangeIdIndexImpl::new(*self, heads))
}

pub(super) fn evaluate_revset(
&self,
expression: &ResolvedExpression,
Expand Down Expand Up @@ -382,6 +389,13 @@ impl Index for CompositeIndex<'_> {
ids
}

fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_> {
CompositeIndex::change_id_index(self, heads)
}

fn evaluate_revset<'index>(
&'index self,
expression: &ResolvedExpression,
Expand All @@ -392,8 +406,8 @@ impl Index for CompositeIndex<'_> {
}

pub(super) struct ChangeIdIndexImpl<I> {
pub index: I,
pub pos_by_change: IdIndex<ChangeId, IndexPosition, 4>,
index: I,
pos_by_change: IdIndex<ChangeId, IndexPosition, 4>,
}

impl<I: AsCompositeIndex> ChangeIdIndexImpl<I> {
Expand Down
9 changes: 8 additions & 1 deletion lib/src/default_index/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexSegment};
use crate::backend::{ChangeId, CommitId};
use crate::commit::Commit;
use crate::file_util::persist_content_addressed_temp_file;
use crate::index::{Index, MutableIndex, ReadonlyIndex};
use crate::index::{ChangeIdIndex, Index, MutableIndex, ReadonlyIndex};
use crate::object_id::{HexPrefix, ObjectId, PrefixResolution};
use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError};
use crate::store::Store;
Expand Down Expand Up @@ -442,6 +442,13 @@ impl Index for DefaultMutableIndex {
self.as_composite().topo_order(input)
}

fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_> {
self.as_composite().change_id_index(heads)
}

fn evaluate_revset<'index>(
&'index self,
expression: &ResolvedExpression,
Expand Down
7 changes: 7 additions & 0 deletions lib/src/default_index/readonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,13 @@ impl Index for DefaultReadonlyIndex {
self.as_composite().topo_order(input)
}

fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_> {
self.as_composite().change_id_index(heads)
}

fn evaluate_revset<'index>(
&'index self,
expression: &ResolvedExpression,
Expand Down
15 changes: 0 additions & 15 deletions lib/src/default_index/revset_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ use std::sync::Arc;

use itertools::Itertools;

use super::composite::ChangeIdIndexImpl;
use super::revset_graph_iterator::RevsetGraphIterator;
use crate::backend::{ChangeId, CommitId, MillisSinceEpoch};
use crate::default_index::{AsCompositeIndex, CompositeIndex, IndexEntry, IndexPosition};
use crate::id_prefix::IdIndex;
use crate::index::ChangeIdIndex;
use crate::matchers::{EverythingMatcher, Matcher, PrefixMatcher, Visit};
use crate::repo_path::RepoPath;
use crate::revset::{
Expand Down Expand Up @@ -133,18 +130,6 @@ where
Box::new(self.iter_graph_impl())
}

fn change_id_index(&self) -> Box<dyn ChangeIdIndex + 'index> {
// TODO: Create a persistent lookup from change id to commit ids.
let mut pos_by_change = IdIndex::builder();
for entry in self.entries() {
pos_by_change.insert(&entry.change_id(), entry.position());
}
Box::new(ChangeIdIndexImpl {
index: self.index.clone(),
pos_by_change: pos_by_change.build(),
})
}

fn is_empty(&self) -> bool {
self.entries().next().is_none()
}
Expand Down
5 changes: 5 additions & 0 deletions lib/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ pub trait Index: Send + Sync {
/// Parents before children
fn topo_order(&self, input: &mut dyn Iterator<Item = &CommitId>) -> Vec<CommitId>;

fn change_id_index(
&self,
heads: &mut dyn Iterator<Item = &CommitId>,
) -> Box<dyn ChangeIdIndex + '_>;

fn evaluate_revset<'index>(
&'index self,
expression: &ResolvedExpression,
Expand Down
11 changes: 6 additions & 5 deletions lib/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use crate::operation::Operation;
use crate::refs::{
diff_named_ref_targets, diff_named_remote_refs, merge_ref_targets, merge_remote_refs,
};
use crate::revset::RevsetExpression;
use crate::rewrite::{DescendantRebaser, RebaseOptions};
use crate::settings::{RepoSettings, UserSettings};
use crate::signing::{SignInitError, Signer};
Expand Down Expand Up @@ -1402,14 +1401,16 @@ impl Repo for MutableRepo {
}

fn resolve_change_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<Vec<CommitId>> {
let revset = RevsetExpression::all().evaluate_programmatic(self).unwrap();
let change_id_index = revset.change_id_index();
let change_id_index = self
.index()
.change_id_index(&mut self.view().heads().iter());
change_id_index.resolve_prefix(prefix)
}

fn shortest_unique_change_id_prefix_len(&self, target_id: &ChangeId) -> usize {
let revset = RevsetExpression::all().evaluate_programmatic(self).unwrap();
let change_id_index = revset.change_id_index();
let change_id_index = self
.index()
.change_id_index(&mut self.view().heads().iter());
change_id_index.shortest_unique_prefix_len(target_id)
}
}
Expand Down
3 changes: 0 additions & 3 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use crate::backend::{BackendError, BackendResult, ChangeId, CommitId};
use crate::commit::Commit;
use crate::git;
use crate::hex_util::to_forward_hex;
use crate::index::ChangeIdIndex;
use crate::object_id::{HexPrefix, PrefixResolution};
use crate::op_store::WorkspaceId;
use crate::repo::Repo;
Expand Down Expand Up @@ -2414,8 +2413,6 @@ pub trait Revset<'index>: fmt::Debug {

fn iter_graph(&self) -> Box<dyn Iterator<Item = (CommitId, Vec<RevsetGraphEdge>)> + '_>;

fn change_id_index(&self) -> Box<dyn ChangeIdIndex + 'index>;

fn is_empty(&self) -> bool;

fn count(&self) -> usize;
Expand Down
9 changes: 3 additions & 6 deletions lib/tests/test_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use jj_lib::default_index::{
use jj_lib::index::Index as _;
use jj_lib::object_id::{HexPrefix, ObjectId as _, PrefixResolution};
use jj_lib::repo::{MutableRepo, ReadonlyRepo, Repo};
use jj_lib::revset::RevsetExpression;
use jj_lib::settings::UserSettings;
use testutils::test_backend::TestBackend;
use testutils::{
Expand Down Expand Up @@ -674,11 +673,9 @@ fn test_change_id_index() {
let commit_5 = commit_with_change_id("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");

let index_for_heads = |commits: &[&Commit]| {
RevsetExpression::commits(commits.iter().map(|commit| commit.id().clone()).collect())
.ancestors()
.evaluate_programmatic(tx.repo())
.unwrap()
.change_id_index()
tx.repo()
.index()
.change_id_index(&mut commits.iter().map(|commit| commit.id()))
};
let change_id_index = index_for_heads(&[&commit_1, &commit_2, &commit_3, &commit_4, &commit_5]);
let prefix_len =
Expand Down

0 comments on commit 72c6155

Please sign in to comment.