From 2494a028e42fdc24aa3471f02e61c1a0c87f1b4d Mon Sep 17 00:00:00 2001 From: junderw Date: Sun, 7 Aug 2022 11:45:34 +0900 Subject: [PATCH] Fix: Avoid expensive from_hex if possible. --- src/rest.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/rest.rs b/src/rest.rs index 6914c87ad..cbf946862 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -615,16 +615,22 @@ impl FromStr for AddressPaginator { type Err = String; fn from_str(s: &str) -> Result { - Txid::from_hex(s) - .ok() - .and_then(|txid| Some(Self::Txid(txid))) - .or_else(|| { - s.parse::() - .ok() - .filter(|&skip| skip != 0) // Don't allow 0 for Skip - .and_then(|skip| Some(Self::Skip(skip))) - }) - .ok_or("Invalid AddressPaginator".to_string()) + // 1) Deal with Options in if else statement + // to utilize filter for usize. + // 2) 64 length usize doesn't exist, + // and from_hex is expensive. + if s.len() == 64 { + Txid::from_hex(s) + .ok() + .and_then(|txid| Some(Self::Txid(txid))) + } else { + s.parse::() + .ok() + .filter(|&skip| skip != 0) // Don't allow 0 for Skip + .and_then(|skip| Some(Self::Skip(skip))) + } + // 3) Convert the return value of the if else statement into a Result. + .ok_or("Invalid AddressPaginator".to_string()) } }