-
Notifications
You must be signed in to change notification settings - Fork 543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Change value type backing to 128-bit integers #2084
Comments
Quick question on the fact that existing catalogs need to be upgraded. Will there be a convenience tool to convert an existing catalog from 64-bit to 128-bit? |
#2072 feature flags the type of the implementation backing the struct. It is also refactoring necessary functions to make them compatible with both i128 and i64. It will allow switching between the implementations by switching on/off the feature flag and recompiling.
However, any nautilus data models persisted to the catalog or some file with one precision mode will not work with another precision mode (because the backing types will be different, de-serialization will fail). To make it easy to switch the catalog from one implementation to the other we will use a json/csv file to store the data temporarily. So the steps will become
On the other hand, this can be avoided by loading the catalog from the original source after turning high-precision on. |
I have some numbers from comparison tests on i128 and i64 backed values
Higher precision values need more storage and are slightly slower on backtesting. However, storage difference is relatively small and at 5M ticks per second it's likely that CPU processing will be the bottleneck for backtesting. Using higher precision values can be made the default. |
Thanks for the extensive benchmarks! Another question is which changes would be needed on the adapter side. The instrument provider needs to be aware of the precision I suppose. Do we also need to use pyo3 objects in the adapter? Or will this change be merged only when the rust core is "feature complete"? |
Hey @davidsblom This change should actually be fairly transparent. Up in the Python layer, the only changes are maximum precision increases to 18, and there is a greater value range available. |
Yeah agreed. how I read it, is that both low precision and high precision are supported. Was thinking how an InstrumentProvider knows that. |
The Nautilus value types
Price
,Money
, andQuantity
are currently backed by 64-bit integer raw values, with a maximum precision of 9 decimals enforced through validation (see table below). This design provides memory efficiency and high performance, using simple fixed-point arithmetic to represent actual values. (Integers are more CPU-native than string or decimal types, and are highly efficient for comparisons.)Limitations of the current 64-bit specification:
Quantity
, larger time frame bar volumes cannot be adequately represented.To address these issues, increasing the raw integer width from 64 bits to 128 bits will significantly expand the precision and allowable range.
Current specification (64-bit integer backing):
i64
i64
u64
Proposed specification (128-bit integer backing):
i128
i128
u128
Pros of 128-bit backing:
Cons of 128-bit backing:
QuoteTick
).Other solutions, such as introducing an additional scaling factor field and using arithmetic for more flexible precisions, were considered. However, these approaches significantly increased complexity and the likelihood of bugs, as raw values are directly accessed in many parts of the codebase.
Mitigations
For the in-memory cache, the number of 128-bit fields across all objects is relatively small, and for data is further constrained by deques with a defined
maxlen
.For the in-memory footprint of data during large backtests, this is mostly alleviated by streaming from the Parquet data catalog where only a limited amount of data is held in memory at a time. Users who are using the
BacktestEngine
directly will be most affected.The breaking change to catalogs is a temporary inconvenience. Users can decide whether to upgrade to
nautilus_trader
versions with 128-bit backing or choose the timing of their upgrade to align with their operational needs. Alternatively, they can compile from source with the existing 64-bit backing.Implementation
To provide flexibility, @twitu is implementing a solution where a
high-precision
feature flag will control whether these values are backed by 64-bit or 128-bit integers. This allows users to:#2072
Final comments
While the increased precision and range may be unnecessary or even excessive for traditional financial assets, the limitations are far more severe for crypto users. Traditional users may appreciate the lower memory footprint as a "nice to have," but crypto users are constrained by the 9-decimal precision cap and a more restricted range.
It's expected that
high-precision
will quickly become the default, as the advantages outweigh the trade-offs for most users.We're open to feedback and suggestions on the above.
The text was updated successfully, but these errors were encountered: