Skip to content

Commit

Permalink
feat: improve backtesting performance through alignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaz001 committed Mar 22, 2024
1 parent e215522 commit eaf181a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions rust/src/backtest/evs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[derive(Clone, Copy)]
#[repr(C, align(32))]
pub struct Event {
pub timestamp: i64,
pub asset_no: usize,
Expand Down
3 changes: 2 additions & 1 deletion rust/src/backtest/models/latencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ impl LatencyModel for ConstantLatency {
}
}

#[repr(C)]
#[derive(Clone, Debug)]
#[repr(C)]
pub struct OrderLatencyRow {
pub req_timestamp: i64,
pub exch_timestamp: i64,
pub resp_timestamp: i64,
pub reserved: i64
}

#[derive(Clone)]
Expand Down
34 changes: 25 additions & 9 deletions rust/src/backtest/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
ops::Index,
rc::Rc,
};
use std::mem::forget;

use crate::{
backtest::Error,
Expand Down Expand Up @@ -46,7 +47,7 @@ pub const UNTIL_END_OF_DATA: i64 = i64::MAX;

#[derive(Clone, Debug)]
pub struct Data<D> {
buf: Rc<Vec<u8>>,
buf: Rc<Box<[u8]>>,
header_len: usize,
_d_marker: PhantomData<D>,
}
Expand Down Expand Up @@ -186,15 +187,33 @@ where
}
}

#[repr(C, align(64))]
struct Align64([u8; 64]);

fn aligned_vec(size: usize) -> Box<[u8]> {
let capacity = (size / size_of::<Align64>()) + 1;
let mut aligned: Vec<Align64> = Vec::with_capacity(capacity);

let ptr = aligned.as_mut_ptr();
let cap = aligned.capacity();

forget(aligned);

unsafe {
Vec::from_raw_parts(
ptr as *mut u8,
size,
cap * size_of::<Align64>(),
).into_boxed_slice()
}
}

pub fn read_npy<D: Sized>(filepath: &str) -> Result<Data<D>, IoError> {
let mut file = File::open(filepath)?;

file.sync_all()?;
let size = file.metadata()?.len() as usize;
let mut buf = Vec::with_capacity(size);
unsafe {
buf.set_len(size);
}
let mut buf = aligned_vec(size);

let mut read_size = 0;
while read_size < size {
Expand All @@ -217,10 +236,7 @@ pub fn read_npz<D: Sized>(filepath: &str) -> Result<Data<D>, IoError> {
let mut file = archive.by_index(0)?;

let size = file.size() as usize;
let mut buf = Vec::with_capacity(size);
unsafe {
buf.set_len(size);
}
let mut buf = aligned_vec(size);

let mut read_size = 0;
while read_size < size {
Expand Down

0 comments on commit eaf181a

Please sign in to comment.