Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace binary search with linear search for epoch construction #723

Merged
merged 15 commits into from
Oct 31, 2022
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
7 changes: 7 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ doc:
update-dev-server:
./bin/update-dev-server

start-dev-server-benchmark: && update-dev-server
systemctl stop ord-dev
rm /var/lib/ord-dev/index.redb
journalctl --rotate
journalctl --vacuum-time 1s
./bin/update-dev-server

# publish current GitHub master branch
publish:
#!/usr/bin/env bash
Expand Down
86 changes: 81 additions & 5 deletions src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;
pub(crate) struct Epoch(pub(crate) u64);

impl Epoch {
pub(crate) const STARTING_ORDINALS: &'static [Ordinal] = &[
pub(crate) const STARTING_ORDINALS: [Ordinal; 34] = [
Ordinal(0),
Ordinal(1050000000000000),
Ordinal(1575000000000000),
Expand Down Expand Up @@ -69,9 +69,74 @@ impl PartialEq<u64> for Epoch {

impl From<Ordinal> for Epoch {
fn from(ordinal: Ordinal) -> Self {
match Self::STARTING_ORDINALS.binary_search(&ordinal) {
Ok(i) => Epoch(i as u64),
Err(i) => Epoch(i as u64 - 1),
if ordinal < Self::STARTING_ORDINALS[1] {
Epoch(0)
} else if ordinal < Self::STARTING_ORDINALS[2] {
Epoch(1)
} else if ordinal < Self::STARTING_ORDINALS[3] {
Epoch(2)
} else if ordinal < Self::STARTING_ORDINALS[4] {
Epoch(3)
} else if ordinal < Self::STARTING_ORDINALS[5] {
Epoch(4)
} else if ordinal < Self::STARTING_ORDINALS[6] {
Epoch(5)
} else if ordinal < Self::STARTING_ORDINALS[7] {
Epoch(6)
} else if ordinal < Self::STARTING_ORDINALS[8] {
Epoch(7)
} else if ordinal < Self::STARTING_ORDINALS[9] {
Epoch(8)
} else if ordinal < Self::STARTING_ORDINALS[10] {
Epoch(9)
} else if ordinal < Self::STARTING_ORDINALS[11] {
Epoch(10)
} else if ordinal < Self::STARTING_ORDINALS[12] {
Epoch(11)
} else if ordinal < Self::STARTING_ORDINALS[13] {
Epoch(12)
} else if ordinal < Self::STARTING_ORDINALS[14] {
Epoch(13)
} else if ordinal < Self::STARTING_ORDINALS[15] {
Epoch(14)
} else if ordinal < Self::STARTING_ORDINALS[16] {
Epoch(15)
} else if ordinal < Self::STARTING_ORDINALS[17] {
Epoch(16)
} else if ordinal < Self::STARTING_ORDINALS[18] {
Epoch(17)
} else if ordinal < Self::STARTING_ORDINALS[19] {
Epoch(18)
} else if ordinal < Self::STARTING_ORDINALS[20] {
Epoch(19)
} else if ordinal < Self::STARTING_ORDINALS[21] {
Epoch(20)
} else if ordinal < Self::STARTING_ORDINALS[22] {
Epoch(21)
} else if ordinal < Self::STARTING_ORDINALS[23] {
Epoch(22)
} else if ordinal < Self::STARTING_ORDINALS[24] {
Epoch(23)
} else if ordinal < Self::STARTING_ORDINALS[25] {
Epoch(24)
} else if ordinal < Self::STARTING_ORDINALS[26] {
Epoch(25)
} else if ordinal < Self::STARTING_ORDINALS[27] {
Epoch(26)
} else if ordinal < Self::STARTING_ORDINALS[28] {
Epoch(27)
} else if ordinal < Self::STARTING_ORDINALS[29] {
Epoch(28)
} else if ordinal < Self::STARTING_ORDINALS[30] {
Epoch(29)
} else if ordinal < Self::STARTING_ORDINALS[31] {
Epoch(30)
} else if ordinal < Self::STARTING_ORDINALS[32] {
Epoch(31)
} else if ordinal < Self::STARTING_ORDINALS[33] {
Epoch(32)
} else {
Epoch(33)
}
}
}
Expand Down Expand Up @@ -112,7 +177,7 @@ mod tests {
ordinal += SUBSIDY_HALVING_INTERVAL * Epoch(epoch).subsidy();
}

assert_eq!(Epoch::STARTING_ORDINALS, epoch_ordinals);
assert_eq!(Epoch::STARTING_ORDINALS.as_slice(), epoch_ordinals);
assert_eq!(Epoch::STARTING_ORDINALS.len(), 34);
}

Expand Down Expand Up @@ -140,10 +205,21 @@ mod tests {

#[test]
fn from_ordinal() {
for (epoch, starting_ordinal) in Epoch::STARTING_ORDINALS.into_iter().enumerate() {
if epoch > 0 {
assert_eq!(
Epoch::from(Ordinal(starting_ordinal.n() - 1)),
Epoch(epoch as u64 - 1)
);
}
assert_eq!(Epoch::from(starting_ordinal), Epoch(epoch as u64));
assert_eq!(Epoch::from(starting_ordinal + 1), Epoch(epoch as u64));
}
assert_eq!(Epoch::from(Ordinal(0)), 0);
assert_eq!(Epoch::from(Ordinal(1)), 0);
assert_eq!(Epoch::from(Epoch(1).starting_ordinal()), 1);
assert_eq!(Epoch::from(Epoch(1).starting_ordinal() + 1), 1);
assert_eq!(Epoch::from(Ordinal(u64::max_value())), 33);
}

#[test]
Expand Down