From d802aab59839e3a4d85c5f4746562e7c39f10c78 Mon Sep 17 00:00:00 2001 From: Jonathan Strong Date: Fri, 17 Apr 2020 17:00:56 -0400 Subject: [PATCH] use integer masking to speed up row filtering a lot --- src/binary-serialization.rs | 49 ++++++++++++++++++++++++++----------- src/encoding.rs | 21 ++++++++++------ 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/binary-serialization.rs b/src/binary-serialization.rs index 6de8c03..b1b8100 100644 --- a/src/binary-serialization.rs +++ b/src/binary-serialization.rs @@ -100,24 +100,45 @@ fn easy_query( let mut gdax_amount = 0.0; let mut n_gdax = 0; + const MASK : i32 = i32::from_le_bytes([ 255, 255, 255, 0]); + const BMEX_BTC_USD : i32 = i32::from_le_bytes([ e!(bmex) as u8, c!(btc) as u8, c!(usd) as u8, 0 ]); + const GDAX_BTC_USD : i32 = i32::from_le_bytes([ e!(gdax) as u8, c!(btc) as u8, c!(usd) as u8, 0 ]); + macro_rules! update { // in macro to avoid repeating code once outside loop, and again in loop body ($trade:ident) => {{ - match ($trade.exch(), $trade.base(), $trade.quote()) { - (Ok(e!(bmex)), Ok(c!(btc)), Ok(c!(usd))) => { - bmex_total += $trade.price() * $trade.amount(); - bmex_amount += $trade.amount(); - n_bmex += 1; - } - (Ok(e!(gdax)), Ok(c!(btc)), Ok(c!(usd))) => { - gdax_total += $trade.price() * $trade.amount(); - gdax_amount += $trade.amount(); - n_gdax += 1; + let meta_sans_side: i32 = $trade.meta_i32() & MASK; - } - - _ => {} - } + let is_bmex_btc_usd: f64 = (meta_sans_side == BMEX_BTC_USD) as u8 as f64; + + let is_gdax_btc_usd: f64 = (meta_sans_side == GDAX_BTC_USD) as u8 as f64; + + let amount = $trade.amount(); + let total = $trade.price() * amount; + + bmex_total += is_bmex_btc_usd * total; + bmex_amount += is_bmex_btc_usd * amount; + n_bmex += is_bmex_btc_usd as usize * 1; + + gdax_total += is_gdax_btc_usd * total; + gdax_amount += is_gdax_btc_usd * amount; + n_gdax += is_gdax_btc_usd as usize * 1; + + + //match ($trade.exch(), $trade.base(), $trade.quote()) { + // (Ok(e!(bmex)), Ok(c!(btc)), Ok(c!(usd))) => { + // n_bmex += 1; + // } + + // (Ok(e!(gdax)), Ok(c!(btc)), Ok(c!(usd))) => { + // gdax_total += $trade.price() * $trade.amount(); + // gdax_amount += $trade.amount(); + // n_gdax += 1; + + // } + // + // _ => {} + //} }} } diff --git a/src/encoding.rs b/src/encoding.rs index ddaa748..809823a 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -183,13 +183,6 @@ impl<'a> PackedTradeData<'a> { pub fn base(&self) -> Result { Currency::try_from(self.0[BASE_OFFSET]) } - #[inline] - pub fn ticker(&self) -> Ticker { - Ticker { - base: self.base().unwrap(), - quote: self.quote().unwrap(), - } - } #[inline] pub fn quote(&self) -> Result { @@ -204,6 +197,20 @@ impl<'a> PackedTradeData<'a> { } } + #[inline] + pub fn ticker(&self) -> Ticker { + Ticker { + base: self.base().unwrap(), + quote: self.quote().unwrap(), + } + } + + #[inline] + pub unsafe fn meta_i32(&self) -> i32 { + i32::from_le_bytes((&self.0[..4]).try_into().unwrap()) + } + + #[inline] pub fn time(&self) -> u64 { u64::from_le_bytes(