Skip to content

Commit 9ccee8c

Browse files
authored
Fix large difference between Data enum variants (#2315)
1 parent 072fc49 commit 9ccee8c

File tree

12 files changed

+37
-17
lines changed

12 files changed

+37
-17
lines changed

crates/adapters/databento/src/decode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ pub fn decode_record(
841841
} else if let Some(msg) = record.get::<dbn::Mbp10Msg>() {
842842
let ts_init = determine_timestamp(ts_init, msg.ts_recv.into());
843843
let depth = decode_mbp10_msg(msg, instrument_id, price_precision, ts_init)?;
844-
(Some(Data::Depth10(depth)), None)
844+
(Some(Data::from(depth)), None)
845845
} else if let Some(msg) = record.get::<dbn::OhlcvMsg>() {
846846
let ts_init = determine_timestamp(ts_init, msg.hd.ts_event.into());
847847
let bar = decode_ohlcv_msg(msg, instrument_id, price_precision, ts_init)?;

crates/adapters/databento/src/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl DatabentoDataLoader {
315315
.filter_map(|result| match result {
316316
Ok((Some(item1), _)) => {
317317
if let Data::Depth10(depth) = item1 {
318-
Some(Ok(depth))
318+
Some(Ok(*depth))
319319
} else {
320320
None
321321
}

crates/adapters/tardis/src/replay.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub async fn run_tardis_machine_replay_from_config(config_filepath: &Path) -> an
182182
handle_deltas_msg(msg, &mut deltas_map, &mut deltas_cursors, &path);
183183
}
184184
Data::Depth10(msg) => {
185-
handle_depth10_msg(msg, &mut depths_map, &mut depths_cursors, &path);
185+
handle_depth10_msg(*msg, &mut depths_map, &mut depths_cursors, &path);
186186
}
187187
Data::Quote(msg) => handle_quote_msg(msg, &mut quotes_map, &mut quotes_cursors, &path),
188188
Data::Trade(msg) => handle_trade_msg(msg, &mut trades_map, &mut trades_cursors, &path),

crates/data/src/engine/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ impl DataEngine {
396396
match data {
397397
Data::Delta(delta) => self.handle_delta(delta),
398398
Data::Deltas(deltas) => self.handle_deltas(deltas.into_inner()),
399-
Data::Depth10(depth) => self.handle_depth10(depth),
399+
Data::Depth10(depth) => self.handle_depth10(*depth),
400400
Data::Quote(quote) => self.handle_quote(quote),
401401
Data::Trade(trade) => self.handle_trade(trade),
402402
Data::Bar(bar) => self.handle_bar(bar),

crates/data/src/engine/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ fn test_process_order_book_depth10(
742742
}
743743

744744
let mut data_engine = data_engine.borrow_mut();
745-
data_engine.process_data(Data::Depth10(depth));
745+
data_engine.process_data(Data::from(depth));
746746
let _cache = &data_engine.get_cache();
747747
let messages = get_saved_messages::<OrderBookDepth10>(handler);
748748

crates/model/src/data/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ use crate::{
6464
/// larger (10x) than the smallest.
6565
#[repr(C)]
6666
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
67-
#[allow(clippy::large_enum_variant)]
6867
pub enum Data {
6968
Delta(OrderBookDelta),
7069
Deltas(OrderBookDeltas_API),
71-
Depth10(OrderBookDepth10), // This variant is significantly larger
70+
Depth10(Box<OrderBookDepth10>), // This variant is significantly larger
7271
Quote(QuoteTick),
7372
Trade(TradeTick),
7473
Bar(Bar),
@@ -89,10 +88,20 @@ macro_rules! impl_try_from_data {
8988
};
9089
}
9190

91+
impl TryFrom<Data> for OrderBookDepth10 {
92+
type Error = ();
93+
94+
fn try_from(value: Data) -> Result<Self, Self::Error> {
95+
match value {
96+
Data::Depth10(x) => Ok(*x),
97+
_ => Err(()),
98+
}
99+
}
100+
}
101+
102+
impl_try_from_data!(Quote, QuoteTick);
92103
impl_try_from_data!(Delta, OrderBookDelta);
93104
impl_try_from_data!(Deltas, OrderBookDeltas_API);
94-
impl_try_from_data!(Depth10, OrderBookDepth10);
95-
impl_try_from_data!(Quote, QuoteTick);
96105
impl_try_from_data!(Trade, TradeTick);
97106
impl_try_from_data!(Bar, Bar);
98107

@@ -157,7 +166,7 @@ impl From<OrderBookDeltas_API> for Data {
157166

158167
impl From<OrderBookDepth10> for Data {
159168
fn from(value: OrderBookDepth10) -> Self {
160-
Self::Depth10(value)
169+
Self::Depth10(Box::new(value))
161170
}
162171
}
163172

crates/model/src/ffi/data/depth.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ pub unsafe extern "C" fn orderbook_depth10_new(
7575
)
7676
}
7777

78+
#[no_mangle]
79+
#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
80+
pub extern "C" fn orderbook_depth10_clone(depth: &OrderBookDepth10) -> OrderBookDepth10 {
81+
*depth
82+
}
83+
7884
#[no_mangle]
7985
pub extern "C" fn orderbook_depth10_eq(lhs: &OrderBookDepth10, rhs: &OrderBookDepth10) -> u8 {
8086
u8::from(lhs == rhs)

crates/model/src/python/data/depth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl OrderBookDepth10 {
276276
/// `Data::Depth10` object cannot be converted into a raw pointer.
277277
#[pyo3(name = "as_pycapsule")]
278278
fn py_as_pycapsule(&self, py: Python<'_>) -> PyObject {
279-
data_to_pycapsule(py, Data::Depth10(*self))
279+
data_to_pycapsule(py, Data::from(*self))
280280
}
281281

282282
/// Return a dictionary representation of the object.

crates/persistence/src/backend/catalog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl ParquetDataCatalog {
225225
delta.push(d);
226226
}
227227
Data::Depth10(d) => {
228-
depth10.push(d);
228+
depth10.push(*d);
229229
}
230230
Data::Quote(d) => {
231231
quote.push(d);

nautilus_trader/core/includes/model.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ typedef struct Data_t {
13071307
struct OrderBookDeltas_API deltas;
13081308
};
13091309
struct {
1310-
struct OrderBookDepth10_t depth10;
1310+
struct OrderBookDepth10_t *depth10;
13111311
};
13121312
struct {
13131313
struct QuoteTick_t quote;
@@ -1947,6 +1947,8 @@ struct OrderBookDepth10_t orderbook_depth10_new(struct InstrumentId_t instrument
19471947
uint64_t ts_event,
19481948
uint64_t ts_init);
19491949

1950+
struct OrderBookDepth10_t orderbook_depth10_clone(const struct OrderBookDepth10_t *depth);
1951+
19501952
uint8_t orderbook_depth10_eq(const struct OrderBookDepth10_t *lhs,
19511953
const struct OrderBookDepth10_t *rhs);
19521954

nautilus_trader/core/rust/model.pxd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ cdef extern from "../includes/model.h":
728728
Data_t_Tag tag;
729729
OrderBookDelta_t delta;
730730
OrderBookDeltas_API deltas;
731-
OrderBookDepth10_t depth10;
731+
OrderBookDepth10_t *depth10;
732732
QuoteTick_t quote;
733733
TradeTick_t trade;
734734
Bar_t bar;
@@ -1143,6 +1143,8 @@ cdef extern from "../includes/model.h":
11431143
uint64_t ts_event,
11441144
uint64_t ts_init);
11451145

1146+
OrderBookDepth10_t orderbook_depth10_clone(const OrderBookDepth10_t *depth);
1147+
11461148
uint8_t orderbook_depth10_eq(const OrderBookDepth10_t *lhs, const OrderBookDepth10_t *rhs);
11471149

11481150
uint64_t orderbook_depth10_hash(const OrderBookDepth10_t *delta);

nautilus_trader/model/data.pyx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ from nautilus_trader.core.rust.model cimport orderbook_depth10_ask_counts_array
108108
from nautilus_trader.core.rust.model cimport orderbook_depth10_asks_array
109109
from nautilus_trader.core.rust.model cimport orderbook_depth10_bid_counts_array
110110
from nautilus_trader.core.rust.model cimport orderbook_depth10_bids_array
111+
from nautilus_trader.core.rust.model cimport orderbook_depth10_clone
111112
from nautilus_trader.core.rust.model cimport orderbook_depth10_eq
112113
from nautilus_trader.core.rust.model cimport orderbook_depth10_hash
113114
from nautilus_trader.core.rust.model cimport orderbook_depth10_new
@@ -206,7 +207,7 @@ cpdef list capsule_to_list(capsule):
206207
elif ptr[i].tag == Data_t_Tag.DELTAS:
207208
objects.append(deltas_from_mem_c(ptr[i].deltas))
208209
elif ptr[i].tag == Data_t_Tag.DEPTH10:
209-
objects.append(depth10_from_mem_c(ptr[i].depth10))
210+
objects.append(depth10_from_mem_c(orderbook_depth10_clone(ptr[i].depth10)))
210211
elif ptr[i].tag == Data_t_Tag.QUOTE:
211212
objects.append(quote_from_mem_c(ptr[i].quote))
212213
elif ptr[i].tag == Data_t_Tag.TRADE:
@@ -226,7 +227,7 @@ cpdef Data capsule_to_data(capsule):
226227
elif ptr.tag == Data_t_Tag.DELTAS:
227228
return deltas_from_mem_c(ptr.deltas)
228229
elif ptr.tag == Data_t_Tag.DEPTH10:
229-
return depth10_from_mem_c(ptr.depth10)
230+
return depth10_from_mem_c(orderbook_depth10_clone(ptr.depth10))
230231
elif ptr.tag == Data_t_Tag.QUOTE:
231232
return quote_from_mem_c(ptr.quote)
232233
elif ptr.tag == Data_t_Tag.TRADE:
@@ -3108,7 +3109,7 @@ cdef class OrderBookDepth10(Data):
31083109
# It is supposed to be deallocated by the creator
31093110
capsule = pyo3_depth10.as_pycapsule()
31103111
cdef Data_t* ptr = <Data_t*>PyCapsule_GetPointer(capsule, NULL)
3111-
return depth10_from_mem_c(ptr.depth10)
3112+
return depth10_from_mem_c(orderbook_depth10_clone(ptr.depth10))
31123113

31133114
@staticmethod
31143115
cdef OrderBookDepth10 from_dict_c(dict values):

0 commit comments

Comments
 (0)