Skip to content

Commit

Permalink
Speed up rarity check while indexing (#702)
Browse files Browse the repository at this point in the history
Replace check for uncommon or better rarity by specialized, more efficient
call. This method call contributed for 70% of the time in index_transaction, so
it's worth adding a more specialized method.

There's more potential here, as self.epoch() can likely be sped up
significantly - the binary search still takes an inordinate amount of time. See
issue #711.
  • Loading branch information
VirtuallyThere authored Oct 25, 2022
1 parent e9a6659 commit c5227b3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ impl Index {
.pop_front()
.ok_or_else(|| anyhow!("insufficient inputs for transaction outputs"))?;

if Ordinal(range.0).rarity() > Rarity::Common {
if !Ordinal(range.0).is_common() {
ordinal_to_satpoint.insert(
&range.0,
&encode_satpoint(SatPoint {
Expand Down
27 changes: 27 additions & 0 deletions src/ordinal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ impl Ordinal {
self.into()
}

/// `Ordinal::rarity` is expensive and is called frequently when indexing.
/// Ordinal::is_common only checks if self is `Rarity::Common` but is
/// much faster.
pub(crate) fn is_common(self) -> bool {
let epoch = self.epoch();
(self.0 - epoch.starting_ordinal().0) % epoch.subsidy() != 0
}

pub(crate) fn name(self) -> String {
let mut x = Self::SUPPLY - self.0;
let mut name = String::new();
Expand Down Expand Up @@ -597,4 +605,23 @@ mod tests {
case(Ordinal::LAST.n() / (n + 1));
}
}

#[test]
fn is_common() {
fn case(n: u64) {
assert_eq!(
Ordinal(n).is_common(),
Ordinal(n).rarity() == Rarity::Common
);
}

case(0);
case(1);
case(50 * COIN_VALUE - 1);
case(50 * COIN_VALUE);
case(50 * COIN_VALUE + 1);
case(2067187500000000 - 1);
case(2067187500000000);
case(2067187500000000 + 1);
}
}

0 comments on commit c5227b3

Please sign in to comment.