| 1 | //! Cost of special-character name resolution (ADR-47/48): simple expressions |
| 2 | //! (which should pay nothing or next to nothing) against the critical shapes — |
| 3 | //! a 190-candidate name-only formula, a bound hyphen name, a nested hyphen |
| 4 | //! member, and one compiled program alternating between two scopes. |
| 5 | //! |
| 6 | //! Runs at `NameResolution::Symbols` (the spec mode; the crate default became |
| 7 | //! `Quoted` after the ADR-48 measurements, which used this file with the |
| 8 | //! then-default `compile`/`evaluate`). |
| 9 | //! |
| 10 | //! Run with: `cargo bench -p feel-core --bench names`. |
| 11 | |
| 12 | use std::collections::BTreeMap; |
| 13 | |
| 14 | use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 15 | use feel_core::{CompiledExpr, Context, FeelError, NameResolution, ParseOptions, Value}; |
| 16 | |
| 17 | fn symbols() -> ParseOptions { |
| 18 | ParseOptions::names(NameResolution::Symbols) |
| 19 | } |
| 20 | |
| 21 | fn compile(src: &str) -> Result<CompiledExpr, FeelError> { |
| 22 | feel_core::compile_with(src, &symbols()) |
| 23 | } |
| 24 | |
| 25 | fn evaluate(src: &str, ctx: &Context) -> Result<Value, FeelError> { |
| 26 | feel_core::evaluate_with(src, ctx, &symbols()) |
| 27 | } |
| 28 | |
| 29 | fn ctx_of(pairs: &[(&str, Value)]) -> Context { |
| 30 | let mut c = Context::empty(); |
| 31 | for (k, v) in pairs { |
| 32 | c.insert(*k, v.clone()); |
| 33 | } |
| 34 | c |
| 35 | } |
| 36 | |
| 37 | fn obj(pairs: &[(&str, Value)]) -> Value { |
| 38 | Value::Context(pairs.iter().map(|(k, v)| (k.to_string(), v.clone())).collect::<BTreeMap<_, _>>()) |
| 39 | } |
| 40 | |
| 41 | fn n(x: f64) -> Value { |
| 42 | Value::Number(x) |
| 43 | } |
| 44 | |
| 45 | /// `(label, expression, context)`. |
| 46 | fn workloads() -> Vec<(&'static str, String, Context)> { |
| 47 | let applicant = obj(&[("age", n(30.0)), ("net-income", n(2000.0))]); |
| 48 | let mut formula = String::new(); |
| 49 | let mut fctx = Context::empty(); |
| 50 | for i in 1..=10 { |
| 51 | if i > 1 { |
| 52 | formula.push_str(" + "); |
| 53 | } |
| 54 | formula.push_str(&format!("w{i}*x{i}")); |
| 55 | fctx.insert(format!("w{i}"), n(i as f64)); |
| 56 | fctx.insert(format!("x{i}"), n(2.0)); |
| 57 | } |
| 58 | vec![ |
| 59 | // Simple: no run at all / one root candidate (a plain path). |
| 60 | ("trivial", "1 + 1".into(), Context::empty()), |
| 61 | ( |
| 62 | "path", |
| 63 | "applicant.age >= 18".into(), |
| 64 | ctx_of(&[("applicant", applicant.clone())]), |
| 65 | ), |
| 66 | ( |
| 67 | "decision", |
| 68 | "if applicant.age >= 18 then \"adult\" else \"minor\"".into(), |
| 69 | ctx_of(&[("applicant", applicant.clone())]), |
| 70 | ), |
| 71 | // Critical: 190 root candidates, none bound. |
| 72 | ("formula190", formula, fctx), |
| 73 | // Critical: a bound hyphen name at the root. |
| 74 | ("bound_name", "a-b * 2".into(), ctx_of(&[("a-b", n(5.0))])), |
| 75 | // Critical: a nested hyphen member (ADR-48's path probe). |
| 76 | ( |
| 77 | "member", |
| 78 | "applicant.net-income > 1000".into(), |
| 79 | ctx_of(&[("applicant", applicant)]), |
| 80 | ), |
| 81 | ] |
| 82 | } |
| 83 | |
| 84 | fn bench_names(c: &mut Criterion) { |
| 85 | for (label, src, ctx) in workloads() { |
| 86 | let mut group = c.benchmark_group(label); |
| 87 | group.bench_function(BenchmarkId::new("parse", label), |b| { |
| 88 | b.iter(|| compile(black_box(&src)).unwrap()); |
| 89 | }); |
| 90 | let prog = compile(&src).unwrap(); |
| 91 | group.bench_function(BenchmarkId::new("eval", label), |b| { |
| 92 | b.iter(|| prog.evaluate(black_box(&ctx)).unwrap()); |
| 93 | }); |
| 94 | group.bench_function(BenchmarkId::new("evaluate", label), |b| { |
| 95 | b.iter(|| evaluate(black_box(&src), black_box(&ctx)).unwrap()); |
| 96 | }); |
| 97 | group.finish(); |
| 98 | } |
| 99 | |
| 100 | // One compiled program, rows alternating between the name reading and the |
| 101 | // arithmetic reading (feel-pg's mixed-rows case). Per iteration: both rows. |
| 102 | let named = ctx_of(&[("a-b", n(5.0))]); |
| 103 | let parts = ctx_of(&[("a", n(7.0)), ("b", n(3.0))]); |
| 104 | let prog = compile("a-b * 2").unwrap(); |
| 105 | let mut group = c.benchmark_group("alternating"); |
| 106 | group.bench_function(BenchmarkId::new("eval", "alternating"), |b| { |
| 107 | b.iter(|| { |
| 108 | prog.evaluate(black_box(&named)).unwrap(); |
| 109 | prog.evaluate(black_box(&parts)).unwrap() |
| 110 | }); |
| 111 | }); |
| 112 | group.bench_function(BenchmarkId::new("evaluate", "alternating"), |b| { |
| 113 | b.iter(|| { |
| 114 | evaluate(black_box("a-b * 2"), black_box(&named)).unwrap(); |
| 115 | evaluate(black_box("a-b * 2"), black_box(&parts)).unwrap() |
| 116 | }); |
| 117 | }); |
| 118 | group.finish(); |
| 119 | } |
| 120 | |
| 121 | criterion_group!(benches, bench_names); |
| 122 | criterion_main!(benches); |