From 1fe5158762414b003767c89e000a7b67dd1ba69e Mon Sep 17 00:00:00 2001 From: VirtuallyThere Date: Thu, 27 Oct 2022 22:23:30 +0200 Subject: [PATCH 01/13] Replace binary search with linear search for epoch construction --- src/epoch.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/epoch.rs b/src/epoch.rs index 47a9b6822b..e2c980ac31 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -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), @@ -69,10 +69,12 @@ impl PartialEq for Epoch { impl From 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), + for (i, starting_ordinal) in Self::STARTING_ORDINALS.into_iter().enumerate() { + if starting_ordinal > ordinal { + return Epoch(i as u64 - 1); + } } + Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) } } @@ -112,7 +114,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); } @@ -144,6 +146,7 @@ mod tests { 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] From 5efb71ac47162f5b017e98e9e2f92e4b5c36a8c3 Mon Sep 17 00:00:00 2001 From: VirtuallyThere Date: Thu, 27 Oct 2022 23:35:04 +0200 Subject: [PATCH 02/13] More efficient iteration --- src/epoch.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/epoch.rs b/src/epoch.rs index e2c980ac31..e4908d90b4 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -4,7 +4,7 @@ use super::*; pub(crate) struct Epoch(pub(crate) u64); impl Epoch { - pub(crate) const STARTING_ORDINALS: [Ordinal;34] = [ + pub(crate) const STARTING_ORDINALS: [Ordinal; 34] = [ Ordinal(0), Ordinal(1050000000000000), Ordinal(1575000000000000), @@ -69,12 +69,14 @@ impl PartialEq for Epoch { impl From for Epoch { fn from(ordinal: Ordinal) -> Self { - for (i, starting_ordinal) in Self::STARTING_ORDINALS.into_iter().enumerate() { + let mut i = 0; + for starting_ordinal in Self::STARTING_ORDINALS { if starting_ordinal > ordinal { - return Epoch(i as u64 - 1); + break; } + i += 1; } - Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) + Epoch(i - 1) } } From 3746814a531de5b7c9e2fdcdbffedfdb6101c2b6 Mon Sep 17 00:00:00 2001 From: VirtuallyThere Date: Fri, 28 Oct 2022 00:04:24 +0200 Subject: [PATCH 03/13] 3rd iteration of epoch finding --- src/epoch.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/epoch.rs b/src/epoch.rs index e4908d90b4..993513db8f 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -69,14 +69,13 @@ impl PartialEq for Epoch { impl From for Epoch { fn from(ordinal: Ordinal) -> Self { - let mut i = 0; - for starting_ordinal in Self::STARTING_ORDINALS { - if starting_ordinal > ordinal { - break; + + for i in 0 .. Self::STARTING_ORDINALS.len() { + if Self::STARTING_ORDINALS[i] > ordinal { + return Epoch(i as u64 - 1) } - i += 1; } - Epoch(i - 1) + Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) } } From 41bd56f5bf0d7c0b2bc23f9e878f194e80519ac9 Mon Sep 17 00:00:00 2001 From: VirtuallyThere Date: Fri, 28 Oct 2022 17:47:10 +0200 Subject: [PATCH 04/13] Temporary code to get ordinal epoch distribution and to compare --- Cargo.toml | 4 ++++ src/epoch.rs | 39 +++++++++++++++++++++++++++++++++------ src/index/updater.rs | 2 ++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7408bf8e94..f6dd42ab14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ tokio-stream = "0.1.9" tokio-util = {version = "0.7.3", features = ["compat"] } tower = "0.4.13" tower-http = { version = "0.3.3", features = ["cors"] } +once_cell = "1.3.1" [dev-dependencies] executable-path = "1.0.0" @@ -63,3 +64,6 @@ path = "tests/lib.rs" [build-dependencies] pulldown-cmark = "0.9.2" + +[profile.release] +debug=true diff --git a/src/epoch.rs b/src/epoch.rs index 993513db8f..1bddfa0fb1 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -42,6 +42,8 @@ impl Epoch { ]; pub(crate) const FIRST_POST_SUBSIDY: Epoch = Self(33); + + pub(crate) fn subsidy(self) -> u64 { if self < Self::FIRST_POST_SUBSIDY { (50 * COIN_VALUE) >> self.0 @@ -59,6 +61,25 @@ impl Epoch { pub(crate) fn starting_height(self) -> Height { Height(self.0 * SUBSIDY_HALVING_INTERVAL) } + + #[inline(never)] + fn epoch_from_ordinal_binary_search(ordinal:Ordinal) -> Epoch { + match Self::STARTING_ORDINALS.binary_search(&ordinal) { + Ok(i) => Epoch(i as u64), + Err(i) => Epoch(i as u64 - 1), + } + } + + #[inline(never)] + fn epoch_from_ordinal_linear_search(ordinal:Ordinal) -> Epoch { + for i in 0 .. Self::STARTING_ORDINALS.len() { + if Self::STARTING_ORDINALS[i] > ordinal { + return Epoch(i as u64 - 1); + } + } + Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) + } + } impl PartialEq for Epoch { @@ -67,18 +88,24 @@ impl PartialEq for Epoch { } } +use once_cell::sync::Lazy; // 1.3.1 +use std::sync::Mutex; +pub static EPOCH_DISTRIBUTION: Lazy> = Lazy::new(|| Mutex::new([0; 34])); + impl From for Epoch { + #[inline(never)] fn from(ordinal: Ordinal) -> Self { +// let epoch_binary = Epoch::epoch_from_ordinal_binary_search(ordinal); + let epoch_linear = Epoch::epoch_from_ordinal_linear_search(ordinal); - for i in 0 .. Self::STARTING_ORDINALS.len() { - if Self::STARTING_ORDINALS[i] > ordinal { - return Epoch(i as u64 - 1) - } - } - Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) + EPOCH_DISTRIBUTION.lock().unwrap()[epoch_linear.0 as usize] += 1; + + // assert_eq!(epoch_binary, epoch_linear); + epoch_linear } } + impl From for Epoch { fn from(height: Height) -> Self { Self(height.0 / SUBSIDY_HALVING_INTERVAL) diff --git a/src/index/updater.rs b/src/index/updater.rs index c9b46e512d..e095c4a68b 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -270,6 +270,8 @@ impl Updater { self.outputs_cached ); + log::info!("Ordinals epoch distribution {:#?}", epoch::EPOCH_DISTRIBUTION.lock().unwrap()); + { log::info!( "Flushing {} entries ({:.1}% resulting from {} insertions) from memory to database", From fe6d16b901e04f0b51784a8e59788a248352fa98 Mon Sep 17 00:00:00 2001 From: VirtuallyThere Date: Fri, 28 Oct 2022 17:51:05 +0200 Subject: [PATCH 05/13] Remove temporary distribition tracking code again --- Cargo.toml | 1 - src/epoch.rs | 28 ++++++---------------------- src/index/updater.rs | 2 -- 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f6dd42ab14..fb4ca6b6ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,6 @@ tokio-stream = "0.1.9" tokio-util = {version = "0.7.3", features = ["compat"] } tower = "0.4.13" tower-http = { version = "0.3.3", features = ["cors"] } -once_cell = "1.3.1" [dev-dependencies] executable-path = "1.0.0" diff --git a/src/epoch.rs b/src/epoch.rs index 1bddfa0fb1..783bb0338d 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -42,8 +42,6 @@ impl Epoch { ]; pub(crate) const FIRST_POST_SUBSIDY: Epoch = Self(33); - - pub(crate) fn subsidy(self) -> u64 { if self < Self::FIRST_POST_SUBSIDY { (50 * COIN_VALUE) >> self.0 @@ -62,24 +60,21 @@ impl Epoch { Height(self.0 * SUBSIDY_HALVING_INTERVAL) } - #[inline(never)] - fn epoch_from_ordinal_binary_search(ordinal:Ordinal) -> Epoch { + fn epoch_from_ordinal_binary_search(ordinal: Ordinal) -> Epoch { match Self::STARTING_ORDINALS.binary_search(&ordinal) { Ok(i) => Epoch(i as u64), Err(i) => Epoch(i as u64 - 1), } } - - #[inline(never)] - fn epoch_from_ordinal_linear_search(ordinal:Ordinal) -> Epoch { - for i in 0 .. Self::STARTING_ORDINALS.len() { + + fn epoch_from_ordinal_linear_search(ordinal: Ordinal) -> Epoch { + for i in 0..Self::STARTING_ORDINALS.len() { if Self::STARTING_ORDINALS[i] > ordinal { return Epoch(i as u64 - 1); } } Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) } - } impl PartialEq for Epoch { @@ -88,24 +83,13 @@ impl PartialEq for Epoch { } } -use once_cell::sync::Lazy; // 1.3.1 -use std::sync::Mutex; -pub static EPOCH_DISTRIBUTION: Lazy> = Lazy::new(|| Mutex::new([0; 34])); - impl From for Epoch { - #[inline(never)] fn from(ordinal: Ordinal) -> Self { -// let epoch_binary = Epoch::epoch_from_ordinal_binary_search(ordinal); - let epoch_linear = Epoch::epoch_from_ordinal_linear_search(ordinal); - - EPOCH_DISTRIBUTION.lock().unwrap()[epoch_linear.0 as usize] += 1; - - // assert_eq!(epoch_binary, epoch_linear); - epoch_linear + // Epoch::epoch_from_ordinal_binary_search(ordinal) + Epoch::epoch_from_ordinal_linear_search(ordinal) } } - impl From for Epoch { fn from(height: Height) -> Self { Self(height.0 / SUBSIDY_HALVING_INTERVAL) diff --git a/src/index/updater.rs b/src/index/updater.rs index e095c4a68b..c9b46e512d 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -270,8 +270,6 @@ impl Updater { self.outputs_cached ); - log::info!("Ordinals epoch distribution {:#?}", epoch::EPOCH_DISTRIBUTION.lock().unwrap()); - { log::info!( "Flushing {} entries ({:.1}% resulting from {} insertions) from memory to database", From ae49fdf43cda17cb4e8f9681aaef0f96690798ea Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 28 Oct 2022 10:22:06 -0700 Subject: [PATCH 06/13] Clean up --- src/epoch.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/epoch.rs b/src/epoch.rs index 783bb0338d..54496a5e0d 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -59,22 +59,6 @@ impl Epoch { pub(crate) fn starting_height(self) -> Height { Height(self.0 * SUBSIDY_HALVING_INTERVAL) } - - fn epoch_from_ordinal_binary_search(ordinal: Ordinal) -> Epoch { - match Self::STARTING_ORDINALS.binary_search(&ordinal) { - Ok(i) => Epoch(i as u64), - Err(i) => Epoch(i as u64 - 1), - } - } - - fn epoch_from_ordinal_linear_search(ordinal: Ordinal) -> Epoch { - for i in 0..Self::STARTING_ORDINALS.len() { - if Self::STARTING_ORDINALS[i] > ordinal { - return Epoch(i as u64 - 1); - } - } - Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) - } } impl PartialEq for Epoch { @@ -85,8 +69,12 @@ impl PartialEq for Epoch { impl From for Epoch { fn from(ordinal: Ordinal) -> Self { - // Epoch::epoch_from_ordinal_binary_search(ordinal) - Epoch::epoch_from_ordinal_linear_search(ordinal) + for i in 0..Self::STARTING_ORDINALS.len() { + if Self::STARTING_ORDINALS[i] > ordinal { + return Epoch(i as u64 - 1); + } + } + Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) } } From 927d9380b01c8087014ead90cf5314671f8b8495 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 28 Oct 2022 10:23:14 -0700 Subject: [PATCH 07/13] Remove debug symbols from release build --- Cargo.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5e76359e19..fc1ea7ec29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,6 +62,3 @@ path = "tests/lib.rs" [build-dependencies] pulldown-cmark = "0.9.2" - -[profile.release] -debug=true From a7fdff2cf7efc387f948ff2f8c541e75b8bb963e Mon Sep 17 00:00:00 2001 From: VirtuallyThere Date: Fri, 28 Oct 2022 21:56:15 +0200 Subject: [PATCH 08/13] Unrolling first 4 epochs, fall back to binary search for higher epochs --- src/epoch.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/epoch.rs b/src/epoch.rs index 54496a5e0d..23a2c21340 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -69,12 +69,20 @@ impl PartialEq for Epoch { impl From for Epoch { fn from(ordinal: Ordinal) -> Self { - for i in 0..Self::STARTING_ORDINALS.len() { - if Self::STARTING_ORDINALS[i] > ordinal { - return Epoch(i as u64 - 1); + if ordinal.0 < Self::STARTING_ORDINALS[1].0 { + Epoch(0) + } else if ordinal.0 < Self::STARTING_ORDINALS[2].0 { + Epoch(1) + } else if ordinal.0 < Self::STARTING_ORDINALS[3].0 { + Epoch(2) + } else if ordinal.0 < Self::STARTING_ORDINALS[4].0 { + Epoch(3) + } else { + match Self::STARTING_ORDINALS.binary_search(&ordinal) { + Ok(i) => Epoch(i as u64), + Err(i) => Epoch(i as u64 - 1), } } - Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) } } From 8d33b4726475059a2cc5a699ad973375aaff93ff Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 29 Oct 2022 16:08:42 -0700 Subject: [PATCH 09/13] Try fully unrolled loop --- src/epoch.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/src/epoch.rs b/src/epoch.rs index 23a2c21340..92ebc8eb0a 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -69,19 +69,74 @@ impl PartialEq for Epoch { impl From for Epoch { fn from(ordinal: Ordinal) -> Self { - if ordinal.0 < Self::STARTING_ORDINALS[1].0 { + if ordinal < Self::STARTING_ORDINALS[1] { Epoch(0) - } else if ordinal.0 < Self::STARTING_ORDINALS[2].0 { + } else if ordinal < Self::STARTING_ORDINALS[2] { Epoch(1) - } else if ordinal.0 < Self::STARTING_ORDINALS[3].0 { + } else if ordinal < Self::STARTING_ORDINALS[3] { Epoch(2) - } else if ordinal.0 < Self::STARTING_ORDINALS[4].0 { + } 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 { - match Self::STARTING_ORDINALS.binary_search(&ordinal) { - Ok(i) => Epoch(i as u64), - Err(i) => Epoch(i as u64 - 1), - } + Epoch(33) } } } From 1b19b2b9736ffdb2c3773f09941a1df01d79928c Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 30 Oct 2022 04:38:17 -0700 Subject: [PATCH 10/13] Go back to loop --- src/epoch.rs | 73 ++++------------------------------------------------ 1 file changed, 5 insertions(+), 68 deletions(-) diff --git a/src/epoch.rs b/src/epoch.rs index 92ebc8eb0a..199afd9b36 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -69,75 +69,12 @@ impl PartialEq for Epoch { impl From for Epoch { fn from(ordinal: Ordinal) -> Self { - 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) + for (epoch, starting_ordinal) in Self::STARTING_ORDINALS.into_iter().enumerate().skip(1) { + if starting_ordinal > ordinal { + return Epoch(epoch as u64 - 1); + } } + Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) } } From 8c8c638db40aeb78c6a693bfde4ee8df2df81b84 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 30 Oct 2022 18:17:51 -0700 Subject: [PATCH 11/13] Go back to fully unrolled version, add a test --- src/epoch.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 78 insertions(+), 5 deletions(-) diff --git a/src/epoch.rs b/src/epoch.rs index 199afd9b36..6c1d252fc2 100644 --- a/src/epoch.rs +++ b/src/epoch.rs @@ -69,12 +69,75 @@ impl PartialEq for Epoch { impl From for Epoch { fn from(ordinal: Ordinal) -> Self { - for (epoch, starting_ordinal) in Self::STARTING_ORDINALS.into_iter().enumerate().skip(1) { - if starting_ordinal > ordinal { - return Epoch(epoch 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) } - Epoch(Self::STARTING_ORDINALS.len() as u64 - 1) } } @@ -142,6 +205,16 @@ 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); From e988b5d91d285c1d24e7136de5d856fd05cb9cd4 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 30 Oct 2022 18:20:06 -0700 Subject: [PATCH 12/13] Add start-dev-server-benchmark --- justfile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/justfile b/justfile index a464af9430..7b574454ab 100644 --- a/justfile +++ b/justfile @@ -59,6 +59,13 @@ doc: update-dev-server: ./bin/update-dev-server +start-dev-server-benchmark: + 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 From b034070586a01a4034e1387208fbcb417db08cc4 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 30 Oct 2022 18:21:27 -0700 Subject: [PATCH 13/13] Use post-condition --- justfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/justfile b/justfile index 7b574454ab..6cb2fe64ed 100644 --- a/justfile +++ b/justfile @@ -59,11 +59,11 @@ doc: update-dev-server: ./bin/update-dev-server -start-dev-server-benchmark: +start-dev-server-benchmark: && update-dev-server systemctl stop ord-dev rm /var/lib/ord-dev/index.redb journalctl --rotate - journalctl --vacuum-time=1s + journalctl --vacuum-time 1s ./bin/update-dev-server # publish current GitHub master branch