Skip to content

Performance and Benchmarks

github-actions[bot] edited this page Nov 29, 2026 · 2 revisions

Performance & Benchmarks

Celly.Json is engineered for hot-path rule evaluation where expressions are compiled once and evaluated thousands to millions of times per second.


BenchmarkDotNet Results

Measured on .NET 10.0.14 (X64 RyuJIT x86-64-v3), BenchmarkDotNet v0.15.8 with [MemoryDiagnoser], against a ~15 KB JSON payload containing a nested user object, 40 metadata fields, and a 100-item items array:

Benchmark Mean Allocated vs. Naive Eager
Policy_NaiveEagerDictionary (baseline) 19,375.4 ns 85,416 B 1×
Policy_CellyJson_EvalJson (JsonDocument) 273.1 ns 504 B 71× faster, 169× less alloc
Policy_CellyJson_ReusableActivation 268.6 ns 384 B 72× faster, 222× less alloc
Policy_CellyJson_JsonObject 187.2 ns 296 B 103× faster, 288× less alloc
Comprehension100_NaiveEagerDictionary 55,508.1 ns 214,680 B 1×
Comprehension100_CellyJson_ReusableActivation 8,503.5 ns 14,992 B 6.5× faster, 14.3× less alloc

Celly.Json.AspNetCore Baselines

Benchmark Mean Allocated Notes
HttpContext_HeaderAndRolePolicy 162.3 ns 632 B Lazy HttpContextActivation + zero-list 'admin' in user.roles
HttpContext_WithJsonBodyPolicy 602.0 ns 656 B Includes ArrayPool<byte> body read, JsonDocument.Parse, rewind, and CEL eval
CelJsonHttpResult_StreamToBodyWriter 515.4 ns 1,880 B Direct Utf8JsonWriter streaming (includes new DefaultHttpContext())

Celly.Json.Query Baselines

Translating user.age >= 18 && user.active && 'admin' in user.roles && items.exists(i, i.price >= 50 && i.qty > 0):

Benchmark Mean Allocated
Translate_MongoFilterJson 610.7 ns 1.47 KB
Translate_CosmosSql 990.8 ns 4.13 KB
Translate_SqliteJson 1,227.1 ns 5.63 KB
Translate_SqlServerJson 1,239.7 ns 6.29 KB
Translate_PostgresJsonb 1,330.0 ns 6.11 KB
EndToEnd_ParseAndCosmosSql 3,858.1 ns 16.84 KB

Why It's Fast

  1. Zero eager materialization — JsonElementObjectValue and JsonNodeObjectValue implement Celly's IStructValue, IIndexerValue, ISizedValue, IContainsTester, and IIterableValue directly. Unreferenced fields in a JSON payload are never decoded or allocated.
  2. Zero-allocation in and == scans — JsonElementListValue implements IContainsTester.Contains and EqualTo over JsonElement.EnumerateArray() using JsonElement.ValueEquals(string) and direct primitive comparisons — avoiding UTF-8→UTF-16 string allocations, CelValue boxing, and $O(N^2)$ JsonElement[index] scans.
  3. Inline 4-slot activation cache — JsonElementActivation and JsonNodeActivation cache up to 4 root variable lookups inline on the activation object (no heap array allocation), so repeated accesses like user.age, user.active, and user.roles share a single wrapper.
  4. Native ListValue integration — JsonElementListValue and JsonNodeListValue inherit from ListValue with lazy IReadOnlyList<CelValue> backing, giving full compatibility with Celly's $O(1)$ rope ConcatListValue ([0] + arr).
  5. Direct Utf8JsonWriter pass-through — CelJsonWriter calls JsonElement.WriteTo(writer) directly for unmodified JSON subtrees and formats durations on a stack-allocated Span<char>.

Hot-Path Best Practices

  • Compile CelProgram once and cache it (it is immutable and thread-safe).
  • Reuse JsonElementActivation per thread with .Reset(doc.RootElement) in tight loops (or use FilterJsonArray / FilterJsonLines, which do this for you).
  • Avoid EvalJson(string) inside hot loops if you already have a JsonDocument or JsonObject — parsing JSON dominates the ~270 ns evaluation time.
  • Keep PropertyNameComparison = StringComparison.Ordinal (default) so JsonElement.TryGetProperty uses its fast direct lookup path.
  • Leave JsonSchemaOptions.MapFormats = false unless you actually need automatic date-time/duration/byte decoding.

Clone this wiki locally