From f9dc00704d395e7c7d9d7845a3ccb79925193358 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 6 Jan 2024 18:00:48 -0800 Subject: [PATCH] index: specialize evaluate_revset_static() to change_id_index_static() I'd like to move `change_id_index()` from `Revset` to `Index` (and make it take the set of visible heads as argument). We currently use `evaluate_revset_static()` only to get a `ChangeIdIndex`, so a good place to start is to convert that into `change_id_index_static()`. --- lib/src/default_index/composite.rs | 17 +++++++++++++++++ lib/src/default_index/readonly.rs | 15 ++++++--------- lib/src/index.rs | 11 +++++------ lib/src/repo.rs | 8 ++------ 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/src/default_index/composite.rs b/lib/src/default_index/composite.rs index 80d6992f8..1d9c16288 100644 --- a/lib/src/default_index/composite.rs +++ b/lib/src/default_index/composite.rs @@ -396,6 +396,23 @@ pub(super) struct ChangeIdIndexImpl { pub pos_by_change: IdIndex, } +impl ChangeIdIndexImpl { + pub fn new(index: I, heads: &mut dyn Iterator) -> ChangeIdIndexImpl { + let mut pos_by_change = IdIndex::builder(); + let composite = index.as_composite(); + let head_positions = heads + .map(|id| composite.commit_id_to_pos(id).unwrap()) + .collect_vec(); + for entry in composite.walk_revs(&head_positions, &[]) { + pos_by_change.insert(&entry.change_id(), entry.position()); + } + Self { + index, + pos_by_change: pos_by_change.build(), + } + } +} + impl ChangeIdIndex for ChangeIdIndexImpl { fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution> { self.pos_by_change diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index ab8d01558..b95d6e320 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -26,12 +26,11 @@ use std::sync::Arc; use smallvec::SmallVec; use thiserror::Error; -use super::composite::{AsCompositeIndex, CompositeIndex, IndexSegment}; +use super::composite::{AsCompositeIndex, ChangeIdIndexImpl, CompositeIndex, IndexSegment}; use super::entry::{IndexPosition, LocalPosition, SmallIndexPositionsVec}; use super::mutable::DefaultMutableIndex; -use super::revset_engine; use crate::backend::{ChangeId, CommitId}; -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; @@ -528,13 +527,11 @@ impl ReadonlyIndex for DefaultReadonlyIndex { self } - fn evaluate_revset_static( + fn change_id_index_static( &self, - expression: &ResolvedExpression, - store: &Arc, - ) -> Result>, RevsetEvaluationError> { - let revset_impl = revset_engine::evaluate(expression, store, self.clone())?; - Ok(Box::new(revset_impl)) + heads: &mut dyn Iterator, + ) -> Box { + Box::new(ChangeIdIndexImpl::new(self.clone(), heads)) } fn start_modification(&self) -> Box { diff --git a/lib/src/index.rs b/lib/src/index.rs index 72fa7d7c0..bb554cc16 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -84,13 +84,12 @@ pub trait ReadonlyIndex: Send + Sync { fn as_index(&self) -> &dyn Index; - // TODO: might be better to split Index::evaluate_revset() to - // Readonly/MutableIndex::evaluate_static(). - fn evaluate_revset_static( + // TODO: might be better to split Index::change_id_index() to + // Readonly/MutableIndex::change_id_index_static(). + fn change_id_index_static( &self, - expression: &ResolvedExpression, - store: &Arc, - ) -> Result>, RevsetEvaluationError>; + heads: &mut dyn Iterator, + ) -> Box; fn start_modification(&self) -> Box; } diff --git a/lib/src/repo.rs b/lib/src/repo.rs index a7c962e62..de1ae748d 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -268,12 +268,8 @@ impl ReadonlyRepo { .get_or_init(|| { // TODO: maybe add abstraction over 'static/'index revset // evaluation, and use it. - let expression = RevsetExpression::all().resolve_programmatic(self); - let revset = self - .readonly_index() - .evaluate_revset_static(&expression, self.store()) - .unwrap(); - revset.change_id_index() + self.readonly_index() + .change_id_index_static(&mut self.view().heads().iter()) }) .as_ref() }