Skip to content

Commit

Permalink
Adds ScanOrder enum to ScanConfig (#4955)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Feb 12, 2025
1 parent e9e0e40 commit 627a586
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions accounts-db/src/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,29 @@ pub struct ScanConfig {
/// checked by the scan. When true, abort scan.
pub abort: Option<Arc<AtomicBool>>,

/// true to allow return of all matching items and allow them to be unsorted.
/// This is more efficient.
pub collect_all_unsorted: bool,
/// In what order should items be scanned?
pub scan_order: ScanOrder,
}

impl Default for ScanConfig {
fn default() -> Self {
Self {
abort: None,
collect_all_unsorted: true,
scan_order: ScanOrder::Unsorted,
}
}
}

impl ScanConfig {
pub fn new(collect_all_unsorted: bool) -> Self {
let scan_order = if collect_all_unsorted {
ScanOrder::Unsorted
} else {
ScanOrder::Sorted
};

Self {
collect_all_unsorted,
scan_order,
..Default::default()
}
}
Expand All @@ -145,7 +150,7 @@ impl ScanConfig {
pub fn recreate_with_abort(&self) -> Self {
ScanConfig {
abort: Some(self.abort.clone().unwrap_or_default()),
collect_all_unsorted: self.collect_all_unsorted,
scan_order: self.scan_order,
}
}

Expand All @@ -159,6 +164,18 @@ impl ScanConfig {
}
}

/// In what order should items be scanned?
///
/// Users should prefer `Unsorted`, unless required otherwise,
/// as sorting incurs additional runtime cost.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ScanOrder {
/// Scan items in any order
Unsorted,
/// Scan items in sorted order
Sorted,
}

pub(crate) type AccountMapEntry<T> = Arc<AccountMapEntryInner<T>>;

pub trait IsCached {
Expand Down Expand Up @@ -1053,10 +1070,9 @@ impl<T: IndexValue, U: DiskIndexValue + From<T> + Into<T>> AccountsIndex<T, U> {
F: FnMut(&Pubkey, (&T, Slot)),
R: RangeBounds<Pubkey> + std::fmt::Debug,
{
let returns_items = if config.collect_all_unsorted {
AccountsIndexIteratorReturnsItems::Unsorted
} else {
AccountsIndexIteratorReturnsItems::Sorted
let returns_items = match config.scan_order {
ScanOrder::Unsorted => AccountsIndexIteratorReturnsItems::Unsorted,
ScanOrder::Sorted => AccountsIndexIteratorReturnsItems::Sorted,
};

// TODO: expand to use mint index to find the `pubkey_list` below more efficiently
Expand Down Expand Up @@ -4241,21 +4257,22 @@ pub mod tests {

#[test]
fn test_scan_config() {
for collect_all_unsorted in [false, true] {
for scan_order in [ScanOrder::Sorted, ScanOrder::Unsorted] {
let collect_all_unsorted = scan_order == ScanOrder::Unsorted;
let config = ScanConfig::new(collect_all_unsorted);
assert_eq!(config.collect_all_unsorted, collect_all_unsorted);
assert_eq!(config.scan_order, scan_order);
assert!(config.abort.is_none()); // not allocated
assert!(!config.is_aborted());
config.abort(); // has no effect
assert!(!config.is_aborted());
}

let config = ScanConfig::new(false);
assert!(!config.collect_all_unsorted);
assert_eq!(config.scan_order, ScanOrder::Sorted);
assert!(config.abort.is_none());

let config = ScanConfig::default();
assert!(config.collect_all_unsorted);
assert_eq!(config.scan_order, ScanOrder::Unsorted);
assert!(config.abort.is_none());

let config = config.recreate_with_abort();
Expand Down

0 comments on commit 627a586

Please sign in to comment.