fast_json/README.md
2026-01-25 23:39:19 -03:00

2.2 KiB

fast_json - MQL5 High-Performance JSON Library (The "Bit-Banger" Edition V2)

"If your JSON parser needs recursion, you're doing it wrong."

Overview

fast_json is a production-grade, high-performance JSON library for MQL5, designed for high-frequency trading (HFT) environments where latency and memory fragmentation are critical.

Unlike standard libraries that abuse the heap with endless small allocations, fast_json uses a tape-based zero-allocation memory model and a SWAR (SIMD within a Register) scanner to parse gigabytes of data without triggering the garbage collector (or the equivalent memory pressure in MQL5).

Key Features

  • 🚀 Zero-Alloc Tape Architecture: Parses the entire JSON into a single contiguous integer array (the "tape"). No objects are created during parsing.
  • SWAR Acceleration: Uses bit-manipulation hacks (Simulated 64-bit SWAR) to scan 8 bytes at a time for structural characters, drastically reducing branch misprediction.
  • 🛡️ Stack-Safe Iterative Parser: Written as a finite state machine. Zero recursion. Immune to stack overflow attacks from deeply nested JSONs.
  • 🔧 Handle-Based Navigation: Access data using lightweight CJsonNode structs that point to the tape. Copying nodes is free.
  • 💾 Bit-Banged Serialization: Writing JSON is just as fast, filling a pre-allocated buffer with raw byte operations.

Performance

Metric fast_json Standard Libs
Allocation Strategy 1 big block (Tape) N objects per node
Recursion Depth Unlimited (Heap stack) Limited (Call stack)
Parsing Speed ~300MB/s (Est.) Snail pace
Memory Frag Near Zero High

Usage

Parsing

#include "fast_json.mqh"

CJsonContext ctx;
if (ctx.Parse(json_string)) {
    CJsonNode root = ctx.GetRoot();
    Print(root["price"].ToDouble());
} else {
    Print("Error: ", ctx.error_msg);
}

Building & Serializing

// Implicit serialization is supported via direct buffer writing
// (See implementation for details - this is for pros)

Why MQL5?

Because we trade here. And we don't tolerate latency.

License

MIT. Use it, profit, but don't blame me if your strategy fails.