Repository navigation
🐇🕳️ Compile fee rules to closures instead of running CEL - #5326
Draft
jmg-duarte wants to merge 2 commits into
Draft
jmg-duarte wants to merge 2 commits into
jmg-duarte wants to merge 2 commits into
Conversation
The `cel` crate now only parses. Each rule compiles once to typed closures that read the order through a field table, so no CEL map is built per order. Numbers stay exact (U256, I256, Decimal). Type errors, unknown names and constant fee arguments off the grid are now load errors. The sample-order check stays for run-time failures. Macros (`exists`, `all`, `map`, ...) are not supported: test a list with `in`. Infra must replace `wrapped_native.exists(t, t in L)` before this ships. On mainnet auction 14281257 with the prod rules, the fees are the same for all 6,363 orders and evaluation goes from 4.4 to 0.12 µs/order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The interpreter spent most of its time on moving values in and out of CEL (the order as a CEL map for each order, name lookups, fees as maps), not on the rule logic. On mainnet the engine took about 11 µs per order, about 70 ms per auction (Prometheus, 2026-11-19).
On a real mainnet auction (14281257, 6,363 orders) with the prod rules:
The rule language is now a fixed subset of CEL: the operators,
has, the fee functions and the lists. Anything else is an error when the rules load. The order fields are the same as before.After adding the CEL-based fee engine:

After replacing it with the compiler-based fee engine:

Important
Infra must change before this is deployed. Every rendered
[fee-engine]section haswrapped_native.exists(t, t in correlated_N), and macros no longer compile, so the services would not start. See Rollout.Behavior changes:
order.sellAmount == "x") or a constant fee argument off the grid (volume(0.001)) in any branch is a load error. Before, it was a skipped rule on each order, or found only if a sample order reached it.rust_decimal). Before, amounts were doubles: a real order with a sell amount of 999999999999999984204664 was equal to1e24. Literals are read from the rule text, so1e24is exactly 10^24. All arithmetic is checked; an overflow skips the rule for that order.order.creationDateis a number, in Unix seconds likevalidToandvalidFrom. Before, it was an RFC 3339 string.0xaddress matches in any letter case. Any other member is compared as written.inis the only operation on a list.appCodematches one spelling only, becauselowerAsciiis gone. Put every spelling in a list.feethat named no variable was.The sample-order check (
RuleSet::validate_on_sample_order) stays for the failures that depend on the order: an optional field read withouthas, arithmetic, a computed fee argument.Changes
fee-engine:compilemodule.compile::expressionwalks the parsed tree once and gives aNode: a type and aVal, which is a constant or a boxed closure over theOrderView.numberholds the exact number types and their promotion rules.order_viewis a static table of fields with Rust accessors. The engine builds no CEL map per order.RuleSet::evaluate(view)replacesRuleSet::evaluator(now).evaluate(view).Evaluatoris removed.RuleErroris nowMissing,Arithmetic,RangeandFee.FeeErroris removed.alloy_primitives::map::AddressSet. A list of at most 4 addresses is scanned.tests/readme.rsnow compiles everywhen:andfee:example line.tests/numbers.rsfor the exact-number cases.const-hexandserde_jsonare removed.alloy-primitivesgets themapfeature.autopilot,orderbook: callRuleSet::evaluate. The engine side no longer takesnow; the legacy fees still do.How to test
cargo nextest run -p fee-engine -p autopilot -p orderbook -p shared -p configs[fee-engine], the existing e2e tests run the legacy path.I've also ran this on BNB production as the pictures above showed