Skip to content

ANTLR ast comparison#18

Open
orneryd wants to merge 4 commits intomainfrom
hybrid-ast
Open

ANTLR ast comparison#18
orneryd wants to merge 4 commits intomainfrom
hybrid-ast

Conversation

@orneryd
Copy link
Owner

@orneryd orneryd commented Dec 5, 2025

Cypher Executor Modes: Architecture Overview

Environment Variable: NORNICDB_EXECUTOR_MODE
Options: nornic | antlr | hybrid (default)

Architecture Diagram

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#1f6feb', 'primaryTextColor': '#c9d1d9', 'primaryBorderColor': '#30363d', 'lineColor': '#8b949e', 'secondaryColor': '#238636', 'tertiaryColor': '#21262d', 'background': '#0d1117', 'mainBkg': '#161b22', 'textColor': '#c9d1d9'}}}%%

flowchart TB
    subgraph ENV["🔧 Configuration"]
        direction LR
        E1["NORNICDB_EXECUTOR_MODE"]
        E2["nornic | antlr | hybrid"]
    end

    Q[/"Cypher Query"/]
    
    Q --> FACTORY["NewCypherExecutor()"]
    
    FACTORY --> |"mode=nornic"| NORNIC
    FACTORY --> |"mode=antlr"| ANTLR
    FACTORY --> |"mode=hybrid"| HYBRID
    
    subgraph NORNIC["⚡ Nornic Mode"]
        direction TB
        N1["String Parser"]
        N2["Regex + indexOf"]
        N3["Direct Execution"]
        N1 --> N2 --> N3
    end
    
    subgraph ANTLR["🌳 ANTLR Mode"]
        direction TB
        A1["ANTLR Lexer"]
        A2["ANTLR Parser"]
        A3["Full AST"]
        A4["AST Walker"]
        A1 --> A2 --> A3 --> A4
    end
    
    subgraph HYBRID["🔀 Hybrid Mode (Default)"]
        direction TB
        H1["Query Arrives"]
        H2["String Executor<br/>(Fast Path)"]
        H3["Background Worker"]
        H4["AST Cache"]
        
        H1 --> H2
        H1 -.-> |"async"| H3
        H3 --> H4
        
        style H2 fill:#238636,stroke:#3fb950
        style H3 fill:#1f6feb,stroke:#58a6ff
        style H4 fill:#6e40c9,stroke:#a371f7
    end
    
    NORNIC --> RESULT[("Result")]
    ANTLR --> RESULT
    HYBRID --> RESULT
    
    HYBRID -.-> |"cached AST for<br/>LLM features"| LLM["🤖 LLM Integration"]
    
    style ENV fill:#21262d,stroke:#30363d
    style NORNIC fill:#161b22,stroke:#f85149
    style ANTLR fill:#161b22,stroke:#a371f7
    style HYBRID fill:#161b22,stroke:#3fb950
    style RESULT fill:#238636,stroke:#3fb950
    style LLM fill:#1f6feb,stroke:#58a6ff
Loading

Query Flow Comparison

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#1f6feb', 'primaryTextColor': '#c9d1d9', 'primaryBorderColor': '#30363d', 'lineColor': '#8b949e', 'secondaryColor': '#238636', 'tertiaryColor': '#21262d'}}}%%

sequenceDiagram
    participant C as Client
    participant N as Nornic
    participant A as ANTLR
    participant H as Hybrid
    participant S as Storage
    participant Cache as AST Cache

    rect rgb(22, 27, 34)
        Note over C,S: Nornic Mode (fastest)
        C->>N: MATCH (n) RETURN n
        N->>N: String parse (~0.1µs)
        N->>S: Execute
        S-->>C: Results (~0.4µs total)
    end

    rect rgb(22, 27, 34)
        Note over C,S: ANTLR Mode (richest AST)
        C->>A: MATCH (n) RETURN n
        A->>A: Lexer + Parser (~15µs)
        A->>A: Build full AST
        A->>A: Walk AST (~50µs)
        A->>S: Execute
        S-->>C: Results (~70µs total)
    end

    rect rgb(22, 27, 34)
        Note over C,Cache: Hybrid Mode (best of both)
        C->>H: MATCH (n) RETURN n
        par Fast Path
            H->>H: String parse
            H->>S: Execute
            S-->>C: Results (~0.4µs)
        and Background
            H-->>Cache: Queue AST build
            Cache->>Cache: ANTLR parse (async)
        end
        Note over Cache: AST ready for LLM features
    end
Loading

Mode Comparison

Feature ⚡ Nornic 🌳 ANTLR 🔀 Hybrid
Performance ~420 ns/op ~70,000 ns/op ~430 ns/op
Speedup vs Neo4j 3-52x faster Similar 3-52x faster
Full AST Available ❌ No ✅ Yes ✅ Yes (async)
LLM Query Manipulation ❌ Limited ✅ Full support ✅ Full support
Memory Usage Lowest Highest Medium
Query Validation Basic Complete Complete (async)
Best For Production speed Development/Analysis Production + LLM

Detailed Pros & Cons

⚡ Nornic Mode (NORNICDB_EXECUTOR_MODE=nornic)

Pros:

  • 🚀 Fastest execution - 420ns/op average
  • 💾 Lowest memory - No AST allocation
  • 🔧 Battle-tested - Original implementation
  • Zero parsing overhead - Direct string manipulation

Cons:

  • 🤖 No LLM integration - Can't safely manipulate queries
  • 🔍 Limited introspection - No structured query analysis
  • 🐛 Harder to debug - No AST to inspect
  • 📊 No query optimization - Can't analyze query structure

Use When:

  • Maximum performance is critical
  • No LLM features needed
  • Simple query patterns

🌳 ANTLR Mode (NORNICDB_EXECUTOR_MODE=antlr)

Pros:

  • 🌳 Full AST - Complete parse tree for every query
  • 🤖 LLM-ready - Safe query manipulation/correction
  • 🔍 Rich introspection - Analyze any query structure
  • Strict validation - Grammar-enforced syntax checking
  • 🛠️ Extensible - Easy to add new Cypher features

Cons:

  • 🐢 Slowest execution - ~165x slower than Nornic
  • 💾 High memory - Full parse tree allocation
  • 🔄 Parse overhead - Every query fully parsed
  • ⏱️ Not for hot paths - Too slow for high-throughput

Use When:

  • Development and debugging
  • Query analysis tools
  • LLM features are the priority over speed
  • Building query optimization pipelines

🔀 Hybrid Mode (NORNICDB_EXECUTOR_MODE=hybrid) ← DEFAULT

Pros:

  • Fast execution - Same speed as Nornic (~3% overhead)
  • 🌳 AST available - Built asynchronously in background
  • 🤖 LLM-ready - Cached AST for manipulation features
  • 🎯 Best of both - Production speed + rich features
  • 📊 Stats tracking - Monitor cache hits/misses

Cons:

  • 💾 Medium memory - Caches grow over time
  • 🔄 Async complexity - AST not immediately available
  • ⏱️ Cold start - First query doesn't have cached AST
  • 🧹 Cache management - May need periodic cleanup

Use When:

  • Production deployments (recommended default)
  • Need both speed and LLM features
  • Can tolerate async AST availability
  • Want monitoring/stats capabilities

Performance Benchmarks (M3 Max)

BenchmarkNornic_Execute-16     2,832,133    420.6 ns/op    128 B/op    4 allocs/op
BenchmarkHybrid_Execute-16     2,711,396    428.4 ns/op    128 B/op    4 allocs/op
BenchmarkANTLR_Execute-16         16,851  70,234.0 ns/op  45312 B/op  892 allocs/op

Configuration Examples

# Production (default) - fast + LLM ready
export NORNICDB_EXECUTOR_MODE=hybrid

# Maximum speed - no LLM features
export NORNICDB_EXECUTOR_MODE=nornic

# Development/Analysis - full AST always
export NORNICDB_EXECUTOR_MODE=antlr

Startup Banner

When NornicDB starts, you'll see:

╔═══════════════════════════════════════════════════════════════════════╗
║  🔧 CYPHER EXECUTOR MODE: hybrid                                      ║
║     Hybrid executor - fast string execution + background AST building ║
║                                                                       ║
║  Set NORNICDB_EXECUTOR_MODE to: nornic | antlr | hybrid               ║
╚═══════════════════════════════════════════════════════════════════════╝

LLM Integration Architecture

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#1f6feb', 'primaryTextColor': '#c9d1d9', 'primaryBorderColor': '#30363d', 'lineColor': '#8b949e', 'secondaryColor': '#238636', 'tertiaryColor': '#21262d'}}}%%

flowchart LR
    subgraph USER["User Input"]
        Q1["Malformed Query"]
        Q2["Natural Language"]
    end
    
    subgraph LLM["🤖 LLM Processing"]
        direction TB
        L1["Query Correction"]
        L2["AST Analysis"]
        L3["Safe Manipulation"]
    end
    
    subgraph HYBRID["🔀 Hybrid Executor"]
        direction TB
        AST["Cached AST"]
        EXEC["Fast Execution"]
    end
    
    Q1 --> L1
    Q2 --> L1
    L1 --> L2
    AST --> L2
    L2 --> L3
    L3 --> EXEC
    EXEC --> R[("Results")]
    
    style USER fill:#21262d,stroke:#f85149
    style LLM fill:#1f6feb,stroke:#58a6ff
    style HYBRID fill:#238636,stroke:#3fb950
    style R fill:#238636,stroke:#3fb950
Loading

Related Files

  • pkg/config/executor_mode.go - Configuration
  • pkg/cypher/executor_factory.go - Factory function
  • pkg/cypher/hybrid_executor.go - Hybrid implementation
  • pkg/cypher/ast_executor.go - ANTLR implementation
  • pkg/cypher/executor.go - Nornic (string) implementation

Questions? Open an issue or check the test files for usage examples:

  • pkg/cypher/executor_mode_test.go - Comprehensive mode tests
  • pkg/cypher/hybrid_executor_test.go - Hybrid-specific tests

ANTLR

Cleaning up...


 ✓ testing/benchmarks/nornicdb-vs-neo4j-northwind.bench.ts > NornicDB Benchmarks (Northwind) 35282ms
     name                                                    hz       min       max      mean       p75       p99      p995      p999      rme  samples
   · Count all nodes                                    44.9561   19.0484   37.3070   22.2439   20.4747   37.3070   37.3070   37.3070  ±10.66%       24
   · Count all relationships                            49.9588   19.2844   21.3269   20.0165   20.2548   21.3269   21.3269   21.3269   ±1.00%       25
   · Get all products                                    946.87    0.7721    3.0533    1.0561    1.0767    2.8253    3.0101    3.0533   ±2.27%      474
   · Get all categories                                  981.25    0.6891    4.1243    1.0191    1.0250    3.1400    3.1749    4.1243   ±3.59%      491
   · Get all customers                                 1,431.57    0.4142    6.6114    0.6985    0.6497    2.7327    3.3201    6.6114   ±5.17%      716
   · Find product by name                              1,220.44    0.6003    3.5219    0.8194    0.8158    2.5997    2.7393    3.5219   ±3.05%      611
   · Find category by name                             1,264.64    0.5870    2.4809    0.7907    0.8455    1.0900    1.1860    2.4809   ±1.26%      633
   · Find customer by ID                               2,152.65    0.3276    3.5240    0.4645    0.4722    0.9985    2.4955    3.4701   ±2.85%     1077
   · Products in Beverages category                    1,281.70    0.6198    1.8499    0.7802    0.8184    1.0823    1.7607    1.8499   ±1.16%      641
   · Products supplied by Exotic Liquids                52.9629   17.2185   33.2915   18.8811   17.8586   33.2915   33.2915   33.2915   ±8.06%       27
   · Orders by customer ALFKI                          2,098.91    0.3374    2.6480    0.4764    0.4907    1.4444    1.6619    2.6103   ±2.40%     1050
   · Products in order 10643                           1,591.67    0.5077    1.0755    0.6283    0.6637    0.8325    0.8911    1.0755   ±0.70%      796
   · Supplier to category through products              21.8117   33.3222   50.3124   45.8469   50.2180   50.3124   50.3124   50.3124  ±10.07%       12
   · Customer orders to products                       2,120.71    0.3482    3.2225    0.4715    0.4857    0.7495    1.3814    3.1896   ±2.30%     1061
   · Customer to category through orders and products  1,530.25    0.5094    2.6262    0.6535    0.6877    0.8950    1.0967    2.6262   ±1.34%      766
   · Customer to supplier through orders and products    401.21    0.5208   32.5868    2.4925    0.8203   21.5932   32.3954   32.5868  ±31.07%      202
   · Products per category                               860.91    0.9035    2.8943    1.1616    1.2211    1.8314    2.0843    2.8943   ±1.39%      431
   · Average price per category                          816.26    0.9317    3.3285    1.2251    1.2400    2.5040    3.0308    3.3285   ±2.40%      409
   · Total quantity ordered per product                  578.61    1.2382    5.1275    1.7283    1.7355    4.0075    4.2389    5.1275   ±3.36%      290
   · Orders per customer                               1,464.99    0.4455    4.5932    0.6826    0.6613    2.7118    3.3583    4.5932   ±3.96%      733
   · Products per supplier                              51.1669   17.4443   33.1423   19.5439   18.4680   33.1423   33.1423   33.1423   ±9.11%       26
   · Top products by revenue (price * quantity)          506.24    1.6340    3.9633    1.9754    2.0790    2.5073    2.5133    3.9633   ±1.30%      254
   · Products out of stock                               584.82    1.3859    3.8014    1.7099    1.7482    3.4776    3.7051    3.8014   ±2.42%      293
   · Expensive products (price > 30)                     530.83    1.3549    4.8779    1.8838    1.8900    4.2935    4.6703    4.8779   ±3.73%      266
   · Categories with product lists                       707.15    1.0371    4.0228    1.4141    1.4104    3.2512    3.7594    4.0228   ±3.29%      354
   · Customers with order lists                        1,622.98    0.4381    2.7570    0.6161    0.6404    0.8562    1.0255    2.7570   ±1.30%      812
   · Products with or without orders                     0.8164  1,127.77  1,314.41  1,224.93  1,283.54  1,314.41  1,314.41  1,314.41   ±3.45%       10
   · Create and delete product node                    3,509.71    0.1756    3.2210    0.2849    0.2928    0.7837    1.1475    3.1941   ±2.71%     1757
   · Create and delete relationship                     61.6443   12.2937   29.6450   16.2221   17.0863   29.6450   29.6450   29.6450  ±11.20%       31
stdout | testing/benchmarks/nornicdb-vs-neo4j-northwind.bench.ts:426:11
✓ Cleanup complete

NORNIC

stdout | testing/benchmarks/nornicdb-vs-neo4j-northwind.bench.ts:412:11
Cleaning up...


 ✓ testing/benchmarks/nornicdb-vs-neo4j-northwind.bench.ts > NornicDB Benchmarks (Northwind) 17475ms
     name                                                    hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · Count all nodes                                   3,271.94  0.1870  3.0944  0.3056  0.3197  0.7232  1.1851  2.3027  ±2.20%     1636
   · Count all relationships                           3,692.92  0.1861  2.5003  0.2708  0.2780  0.5274  0.6210  2.2587  ±1.73%     1849
   · Get all products                                  2,505.10  0.2640  6.6701  0.3992  0.4212  0.6411  0.7119  1.8052  ±2.69%     1253
   · Get all categories                                3,201.06  0.2220  1.3729  0.3124  0.3297  0.5528  0.6218  1.3108  ±1.03%     1601
   · Get all customers                                 3,185.68  0.2437  3.0793  0.3139  0.3255  0.5813  0.7440  2.3061  ±1.66%     1593
   · Find product by name                              4,020.03  0.1829  2.3412  0.2488  0.2564  0.4934  0.5563  0.6815  ±1.11%     2011
   · Find category by name                             4,037.75  0.1740  3.1155  0.2477  0.2537  0.5307  1.0953  2.0423  ±2.14%     2019
   · Find customer by ID                               4,212.86  0.1794  1.0003  0.2374  0.2448  0.4396  0.5058  0.5983  ±0.70%     2107
   · Products in Beverages category                    4,175.60  0.1754  0.6027  0.2395  0.2448  0.4485  0.4985  0.5776  ±0.61%     2088
   · Products supplied by Exotic Liquids               4,022.71  0.1802  3.1426  0.2486  0.2542  0.4873  0.5774  1.1666  ±1.68%     2012
   · Orders by customer ALFKI                          4,149.85  0.1704  0.7425  0.2410  0.2500  0.4828  0.5355  0.6721  ±0.77%     2076
   · Products in order 10643                           3,779.97  0.1772  2.1190  0.2646  0.2635  0.7910  0.9153  1.2374  ±1.84%     1890
   · Supplier to category through products             3,225.03  0.2045  2.2721  0.3101  0.3181  0.6416  1.1019  2.1833  ±1.82%     1613
   · Customer orders to products                       3,577.07  0.1707  5.8842  0.2796  0.2549  1.1114  1.4784  3.2441  ±3.73%     1789
   · Customer to category through orders and products  3,121.82  0.2160  2.0652  0.3203  0.3270  0.8066  1.2657  1.7240  ±1.77%     1561
   · Customer to supplier through orders and products  3,208.77  0.2131  1.9235  0.3116  0.3221  0.7182  0.8822  1.7244  ±1.39%     1605
   · Products per category                             3,328.56  0.2066  0.8345  0.3004  0.3114  0.5900  0.7323  0.8295  ±0.84%     1665
   · Average price per category                        3,379.08  0.2024  2.5847  0.2959  0.3069  0.6420  0.7538  2.5507  ±1.84%     1690
   · Total quantity ordered per product                3,021.62  0.2177  2.9685  0.3309  0.3430  0.7079  1.1475  2.1621  ±1.92%     1511
   · Orders per customer                               3,177.99  0.2071  3.2252  0.3147  0.3210  0.8537  1.2052  2.7592  ±2.43%     1589
   · Products per supplier                             3,133.26  0.2180  2.2741  0.3192  0.3253  0.9146  1.3105  2.1133  ±2.04%     1567
   · Top products by revenue (price * quantity)        3,090.34  0.2190  1.3061  0.3236  0.3402  0.6784  0.7783  1.3043  ±1.08%     1546
   · Products out of stock                             4,018.84  0.1740  0.9014  0.2488  0.2591  0.5492  0.6700  0.7636  ±0.92%     2010
   · Expensive products (price > 30)                   4,008.66  0.1956  0.9510  0.2495  0.2532  0.5477  0.6335  0.7826  ±0.85%     2005
   · Categories with product lists                     3,348.57  0.2346  0.9864  0.2986  0.3105  0.6736  0.7466  0.9429  ±0.97%     1675
   · Customers with order lists                        2,805.05  0.2328  2.4520  0.3565  0.3629  1.0937  1.2534  2.2407  ±2.01%     1403
   · Products with or without orders                   3,881.17  0.1761  1.4444  0.2577  0.2513  0.7879  0.8959  1.3499  ±1.86%     1941
   · Create and delete product node                    3,801.48  0.1827  3.2097  0.2631  0.2590  0.9181  1.2330  2.2110  ±2.52%     1901
   · Create and delete relationship                    3,973.51  0.1804  1.4978  0.2517  0.2610  0.6365  0.8318  1.0484  ±1.23%     1987
stdout | testing/benchmarks/nornicdb-vs-neo4j-northwind.bench.ts:426:11
✓ Cleanup complete

HYBRID


 ✓ testing/benchmarks/nornicdb-vs-neo4j-northwind.bench.ts > NornicDB Benchmarks (Northwind) 17474ms
     name                                                    hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · Count all nodes                                   3,311.96  0.2106  1.6345  0.3019  0.3210  0.5937  0.7535  1.5335  ±1.39%     1656
   · Count all relationships                           3,750.47  0.1904  2.2623  0.2666  0.2758  0.5013  0.6150  1.2654  ±1.38%     1876
   · Get all products                                  2,590.61  0.2538  2.0142  0.3860  0.4049  0.6171  0.7460  1.9224  ±1.21%     1296
   · Get all categories                                3,201.68  0.2132  3.0393  0.3123  0.3194  0.5431  0.7086  2.3060  ±2.01%     1601
   · Get all customers                                 3,211.91  0.2314  3.8804  0.3113  0.3126  0.9338  1.2737  3.0905  ±2.42%     1606
   · Find product by name                              3,745.53  0.1810  3.1055  0.2670  0.2588  0.8789  1.0440  3.0048  ±2.85%     1873
   · Find category by name                             3,885.60  0.1955  1.2540  0.2574  0.2607  0.5526  0.6310  1.2189  ±1.10%     1943
   · Find customer by ID                               4,008.81  0.1787  1.8260  0.2495  0.2574  0.5081  0.5587  0.6287  ±1.01%     2005
   · Products in Beverages category                    4,033.86  0.1730  2.6605  0.2479  0.2573  0.4993  0.5887  0.9729  ±1.38%     2017
   · Products supplied by Exotic Liquids               4,132.65  0.1736  1.3247  0.2420  0.2536  0.4917  0.5478  1.2548  ±1.07%     2067
   · Orders by customer ALFKI                          4,258.70  0.1728  0.6321  0.2348  0.2397  0.4068  0.4753  0.5610  ±0.59%     2130
   · Products in order 10643                           3,896.27  0.1760  0.7257  0.2567  0.2641  0.4441  0.5356  0.6702  ±0.66%     1949
   · Supplier to category through products             3,341.56  0.2397  0.7954  0.2993  0.3119  0.5450  0.5810  0.7701  ±0.74%     1672
   · Customer orders to products                       4,152.80  0.1749  0.8133  0.2408  0.2492  0.4891  0.5383  0.6184  ±0.74%     2077
   · Customer to category through orders and products  3,275.40  0.2391  0.8507  0.3053  0.3195  0.5324  0.5864  0.7295  ±0.76%     1638
   · Customer to supplier through orders and products  3,224.24  0.2463  1.2089  0.3102  0.3255  0.5975  0.7226  0.8776  ±0.94%     1613
   · Products per category                             3,150.61  0.2255  3.0143  0.3174  0.3118  0.9272  1.2682  2.2958  ±2.50%     1576
   · Average price per category                        3,287.54  0.2070  2.6128  0.3042  0.3157  0.7106  0.7968  2.2353  ±1.74%     1644
   · Total quantity ordered per product                2,804.21  0.2267  1.1885  0.3566  0.3726  0.8312  0.9742  1.1659  ±1.18%     1403
   · Orders per customer                               2,890.57  0.2118  3.1530  0.3460  0.3492  0.9805  1.1921  2.3067  ±2.16%     1446
   · Products per supplier                             3,269.05  0.2110  2.6712  0.3059  0.3161  0.6612  0.9013  1.3065  ±1.59%     1635
   · Top products by revenue (price * quantity)        3,020.19  0.2298  2.9543  0.3311  0.3334  0.9988  1.2971  2.2892  ±2.19%     1511
   · Products out of stock                             3,697.29  0.1808  2.2517  0.2705  0.2740  0.6466  0.9607  1.7198  ±1.54%     1849
   · Expensive products (price > 30)                   3,733.16  0.1809  2.3111  0.2679  0.2665  1.0119  1.2254  2.2258  ±2.16%     1867
   · Categories with product lists                     3,118.83  0.2183  2.6859  0.3206  0.3263  0.9832  1.2515  2.3304  ±2.17%     1560
   · Customers with order lists                        2,614.83  0.2549  4.1475  0.3824  0.3956  1.1423  1.8952  2.7055  ±2.79%     1308
   · Products with or without orders                   3,966.73  0.1749  0.9896  0.2521  0.2558  0.6848  0.8203  0.9752  ±1.18%     1984
   · Create and delete product node                    3,453.63  0.1888  3.3375  0.2895  0.2677  1.0057  1.1961  3.3308  ±2.86%     1727
   · Create and delete relationship                    3,955.82  0.1751  1.6440  0.2528  0.2607  0.7319  0.8725  1.0676  ±1.36%     1978
stdout | testing/benchmarks/nornicdb-vs-neo4j-northwind.bench.ts:426:11
✓ Cleanup complete

These are great real-world benchmarks! Let me update the documentation with the actual numbers:

[3 tools called]

Here's your updated GitHub Issue with real benchmark data:


Title: feat: Configurable Cypher Executor Modes (nornic/antlr/hybrid)


NornicDB now supports three Cypher executor modes via NORNICDB_EXECUTOR_MODE.

Architecture

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#1f6feb', 'primaryTextColor': '#c9d1d9', 'primaryBorderColor': '#30363d', 'lineColor': '#8b949e', 'secondaryColor': '#238636', 'tertiaryColor': '#21262d', 'background': '#0d1117', 'mainBkg': '#161b22'}}}%%

flowchart TB
    Q[/"Cypher Query"/] --> FACTORY["NewCypherExecutor()"]
    
    FACTORY --> |"mode=nornic"| NORNIC
    FACTORY --> |"mode=antlr"| ANTLR  
    FACTORY --> |"mode=hybrid"| HYBRID
    
    subgraph NORNIC["⚡ Nornic Mode"]
        N1["String Parser"] --> N2["Direct Execution"]
    end
    
    subgraph ANTLR["🌳 ANTLR Mode"]
        A1["ANTLR Lexer"] --> A2["Full AST"] --> A3["AST Walker"]
    end
    
    subgraph HYBRID["🔀 Hybrid Mode (Default)"]
        H1["Query"] --> H2["String Executor<br/>(Fast Path)"]
        H1 -.-> |"async"| H3["Background AST Builder"]
        H3 --> H4["AST Cache"]
        style H2 fill:#238636,stroke:#3fb950
        style H3 fill:#1f6feb,stroke:#58a6ff
        style H4 fill:#6e40c9,stroke:#a371f7
    end
    
    NORNIC & ANTLR & HYBRID --> RESULT[("Result")]
    HYBRID -.-> |"cached AST"| LLM["🤖 LLM Features"]
    
    style NORNIC fill:#161b22,stroke:#f85149
    style ANTLR fill:#161b22,stroke:#a371f7
    style HYBRID fill:#161b22,stroke:#3fb950
Loading

Real-World Benchmarks (Northwind Database)

Query ⚡ Nornic 🔀 Hybrid 🌳 ANTLR Slowdown
Count all nodes 3,272 hz 3,312 hz 45 hz 73x
Count all relationships 3,693 hz 3,750 hz 50 hz 74x
Find customer by ID 4,213 hz 4,009 hz 2,153 hz 2x
Products supplied by supplier 4,023 hz 4,133 hz 53 hz 76x
Supplier→Category traversal 3,225 hz 3,342 hz 22 hz 147x
Products with/without orders 3,881 hz 3,967 hz 0.82 hz 4,753x
Create/delete relationship 3,974 hz 3,956 hz 62 hz 64x

Total test suite:

Mode Time Winner
⚡ Nornic 17.5s 🥇
🔀 Hybrid 17.5s 🥇
🌳 ANTLR 35.3s ❌ 2x slower

Mode Comparison

Feature ⚡ Nornic 🌳 ANTLR 🔀 Hybrid
Throughput 3,000-4,200 hz 0.8-2,100 hz 3,000-4,200 hz
Worst Case - 4,753x slower -
Full AST ✅ (async)
LLM Integration
Memory Lowest Highest Medium
Best For Max speed Dev/Analysis Production

Key Findings

  1. Hybrid = Nornic performance — Zero overhead in real workloads
  2. ⚠️ ANTLR is 50-5000x slower — Catastrophic on complex queries
  3. 🏆 Hybrid is the clear winner — Production speed + LLM-ready AST

Configuration

export NORNICDB_EXECUTOR_MODE=hybrid  # default - recommended
export NORNICDB_EXECUTOR_MODE=nornic  # fastest, no AST
export NORNICDB_EXECUTOR_MODE=antlr   # full AST, slow

Startup Banner

╔═══════════════════════════════════════════════════════════════════════╗
║  🔧 CYPHER EXECUTOR MODE: hybrid                                      ║
║     Hybrid executor - fast string execution + background AST building ║
║                                                                       ║
║  Set NORNICDB_EXECUTOR_MODE to: nornic | antlr | hybrid               ║
╚═══════════════════════════════════════════════════════════════════════╝

TL;DR: Use hybrid (default). You get Nornic speed with async AST for LLM features.

@orneryd orneryd changed the title Hybrid ast ANTLR ast comparison Dec 5, 2025
@orneryd
Copy link
Owner Author

orneryd commented Dec 5, 2025

I'm keeping the hybrid-ast branch up to date with main so we can compare differences between parsing strategies.

If you have concerns about performance or security with our stream-parse-execute model, we have documentation covering these topics:

📊 Performance

🔒 Security

Our stream-parse-execute model provides both performance benefits (no upfront AST overhead) and security guarantees (identifier validation, parameterization) documented in detail above.

TJ Sweet added 4 commits December 5, 2025 14:40
- Add NORNICDB_EXECUTOR_MODE env var (default: hybrid)
- nornic: Fast string-based parser (original)
- antlr: Full ANTLR AST-based parser (for LLM features)
- hybrid: Fast execution + background AST building (best of both)

New files:
- pkg/config/executor_mode.go: Config for executor mode
- pkg/cypher/executor_factory.go: Factory to create executors
- pkg/cypher/hybrid_executor.go: Hybrid executor implementation
- pkg/cypher/executor_interface.go: CypherExecutor interface + ABExecutor
- pkg/cypher/antlr/*.go: ANTLR grammar and parser files

Test coverage:
- executor_factory_test.go: Factory tests
- executor_mode_test.go: Comprehensive mode comparison tests
- hybrid_executor_test.go: Hybrid-specific tests
- ab_test.go: A/B performance comparison tests

Performance (M3 Max):
- nornic: ~422 ns/op
- hybrid: ~433 ns/op (~3% overhead)
- All modes pass identical query tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments