Crates.io | fee |
lib.rs | fee |
version | 0.2.2 |
created_at | 2025-09-02 02:37:52.378288+00 |
updated_at | 2025-09-13 01:04:44.495258+00 |
description | Fast expression evaluator and parser supporting numeric, logical and bitwise operators |
homepage | |
repository | https://github.com/ZocoLini/fee |
max_upload_size | |
id | 1820459 |
size | 326,585 |
fee
is a fast and flexible library for evaluating mathematical
expressions from strings. It focuses on runtime performance while
keeping the parsing layer simple and efficient.
This crate was originally designed to power scientific and engineering software where expressions cannot be hardcoded:
Over time, fee
has grown into a more general-purpose expression engine.
It already supports:
f64
arithmeticThis makes it suitable not only for numerical/scientific use cases, but also for building DSLs, config evaluators, and high-performance runtimes.
First step is add the dependency to your Cargo.toml.
[dependencies]
fee = { version = "0.2.2" }
The following code shows the default use case
use fee::{prelude::*, DefaultResolver};
fn main()
{
let expr = "max((2 + 4) * 6 / (p1 + 2), sqrt(p0^2 + p1^2)) + abs(-2)";
let mut var_resolver = DefaultResolver::empty();
var_resolver.insert("p0", 10.0);
var_resolver.insert("p1", 4.0);
let mut fn_resolver = DefaultResolver::empty();
fn_resolver.insert("abs", ExprFn::new(abs));
let context = Context::new(var_resolver, fn_resolver);
let mut stack = Vec::with_capacity(10);
let expr = Expr::compile(expr, &context).unwrap();
let result = expr.eval(&context, &mut stack).unwrap();
assert_eq!(result, 8.0);
}
fn abs(x: &[f64]) -> f64 {
x[0].abs()
}
The resolvers are the objects that give meaning to the variables and
functions used in an expression. They can be freely combined in a
Context
depending on your needs.
The current available resolvers are:
DefaultResolver
— No size or naming restrictions, but slower than specialized resolvers.IndexedResolver
— No size restrictions, but requires specific naming patterns. Very fast.SmallResolver
— Restricted size, but allows arbitrary names with good performance.ConstantResolver
— Always resolves to the same value; offers the best performance.EmptyResolver
— Always resolves to None
; useful for expressions without variables or functions.To learn more about their pros and cons read each struct's documentation.
Operator | Priority | Description |
---|---|---|
^ | 8 | Power (exponentiation) |
-, ! | 7 | Unary negation / logical NOT |
*, /, % | 6 | Multiplication, division, remainder |
+, - | 5 | Addition, subtraction |
<<, >> | 4 | Bitwise shift left, bitwise shift right |
&, |, ^^ | 3 | Bitwise AND, OR, XOR |
==, !=, <=, =>, <, > | 2 | Equality and comparison operators |
&& | 1 | Logical AND |
|| | 0 | Logical OR |
true
: Boolean literal, equivalent to 1.0
(Values greater than 0.0 are also considered true).false
: Boolean literal, equivalent to 0.0
.Example of a valid expression:
((x + 3 * y) << 2 ) & 255 | ((10 ^ 2) % 7 )
^^ ((true && (z > 5 || false)) ? 1 : 0 )
+ max(a, b, c)
- min(1, 2, 3)
* abs(-42)
Note: short-circuit not yet implemented.
One of the goals of this library is provide a simple but powerful API. To archive
this, the Expr::compile()
method, depending on the context provided and thanks to
Rust’s type system, automatically picks the best Expr<T>
for
your Context
. You only need to choose the right resolvers, and fee
will compile
to the most efficient form at compile time.
The different types of Expr
are:
RPN (Expr<Rpn>)
: Default and the slowest expression variant.Indexed Var RPN (Expr<IVRpn>)
: Compiled expression with indexed variable resolver.Indexed Fn RPN (Expr<IFRpn>)
: Compiled expression with indexed function resolver.Indexed RPN (Expr<IRpn>)
: Compiled expression with indexed variable and function resolver.Locked RPN (Expr<LRpn>)
: Compiled expression with locked context.The context has a method called lock()
that allows the context to lock
the resolvers it holds. Locking implies that the resolvers will no
longer be able to grow in size, avoiding any further reallocations. This enables
the use of pointers to get and set the resolver's items values. Using a locked
context, the expression can be compiled to a more optimized form, reducing the
number of operations required to evaluate the expression in exchange of not being
able to add new items to the resolvers.
When building this type of expression, the context should be able to resolve the expression's variables and functions at compile time instead of eval time.
let context = Context::new(var_resolver, fn_resolver).lock();
let expr = Expr::compile("abs(2 / p1) + abs(-2)", &context).unwrap();
To get accurate and reproducible results, it is recommended to run benchmarks on isolated CPU cores with a fixed frequency and Turbo Boost disabled.
Feature | Value |
---|---|
Architecture | x86_64 |
CPU Model | 12th Gen Intel® Core™ i9-12900HK |
CPU Cores / Threads | 14 / 2 per core |
Online CPUs | 0-19 |
Isolated Cores | 0-3 |
CPU Frequency | 400–2500 MHz |
Turbo Boost | Disabled |
The following script executes the lib benches.
CORES=0
taskset -c $CORES cargo bench internal
The following script executes the benches related to comparations with other similar libraries available in crates.io.
CORES=0
taskset -c $CORES cargo bench cmp