Balis 情報マガジン

gcp sajson

📅 October 29, 2026📖 Verified Publication
```html GCP SAJSON: The Complete Practical Playbook for Low-Memory JSON Parsing

GCP SAJSON: The Complete Practical Playbook for Low-Memory JSON Parsing

Key Takeaways

  • SAJSON is a header-only, ultra-low-memory JSON parser that builds a compact binary representation of JSON data, consuming up to 10× less RAM than conventional parsers.
  • On GCP, SAJSON shines in Cloud Functions, Cloud Run, and Dataflow where memory-optimized deployments directly reduce cold-start times and per-second billing costs.
  • No external dependencies. SAJSON is a single-header library — ideal for containerized GCP workloads where image bloat drives higher baseline memory pressure.
  • Read-heavy, write-light workloads benefit most. SAJSON parses once into its internal format; subsequent traversals are near-zero-cost pointer walks.
  • Production readiness requires discipline: validate inputs, handle parse errors explicitly, and benchmark against your real payload shapes before committing to SAJSON over rapidjson or nlohmann.

What Is SAJSON?

SAJSON (pronounced "sass-on") stands for Simple Angular JSON. It is a C++ header-only JSON parser designed around a single architectural insight: instead of building a full DOM tree, it constructs a compact binary representation where each JSON node occupies a fixed number of bytes regardless of string length. String data is deduplicated and stored in a single flat buffer, and the parser itself holds no per-node overhead.

The library was created by Simon Brand and is distributed under the MIT license. Its design goal is not feature completeness — it does not support streaming parsing or in-place mutation — but rather speed and minimal memory footprint for read-after-parse access patterns.

Core Architectural Advantages

  • Header-only: One include file. Zero build-step integration friction in GCP Cloud Build pipelines.
  • Fixed node size: Every node is exactly 24 bytes (64-bit), compared to 64–128+ bytes per node in libjson or nlohmann.
  • String interning: Duplicate strings share a single allocation, dramatically reducing memory for repetitive API payloads.
  • No allocator pressure: The entire document can be built into a single contiguous buffer after an initial parse pass.
  • Fast lookup: Object key lookups use linear scan with early-out, optimized for the small-object schemas common in GCP service responses.

SAJSON vs. Alternative JSON Libraries on GCP

Choosing the right JSON library for your GCP workload requires understanding trade-offs. The table below compares SAJSON against the most common alternatives you will encounter in production GCP environments.

JSON Library Comparison for GCP Workloads
Library Memory Model Parse Speed Header-Only Best GCP Use Case
SAJSON Fixed 24B nodes + flat string buffer Fast (single-pass) Yes High-throughput Cloud Run services, Dataflow DoFn transforms
nlohmann/json Full DOM tree, variable-sized nodes Slow (feature-rich) Yes Prototyping, config files, when serialization round-trip matters
RapidJSON DOM tree, compact but not fixed Very fast Yes When both parse and mutate are needed at scale
simdjson Zero-copy view over input buffer Fastest (SIMD-accelerated) Yes Read-only analytics pipelines where input buffer is long-lived
libjson-c Full DOM, C-style Slow No Legacy C projects, minimal C++ involvement

Key decision rule: If your GCP service parses JSON and reads keys repeatedly without modifying structure, SAJSON or simdjson will outperform nlohmann by 5–10× in memory and 2–4× in access latency. If you need to construct new JSON documents programmatically, RapidJSON remains the stronger choice.

Integrating SAJSON into GCP Workloads

SAJSON's header-only design makes it trivial to integrate into any GCP deployment model. Below are the three most common patterns.

Pattern 1: Cloud Functions (2nd Gen) with SAJSON

Cloud Functions allocate memory based on the memory flag. A smaller memory profile means lower cost and faster cold starts. SAJSON's low allocation count lets you run with --memory 256MB instead of 512MB for JSON-heavy HTTP functions.

Checklist: SAJSON in Cloud Functions

  • [ ] Clone SAJSON into your source tree: git clone https://github.com/nbsdx/sajson
  • [ ] Add the SAJSON include path to CMakeLists.txt or BUILD rules
  • [ ] Include only #include "sajson.h" — no other files required
  • [ ] Use sajson::parse() with a sajson::Allocator sized to your expected payload
  • [ ] Wrap all parse calls in try/catch(sajson::Error) for production error handling
  • [ ] Set MEMORY_LIMIT=256MB and benchmark before committing
  • [ ] Profile with Cloud Trace to confirm reduced GC pressure and faster invocation

Pattern 2: Cloud Run Container with SAJSON

Containerized Cloud Run services benefit from SAJSON's ability to keep resident memory flat. Since SAJSON does not fragment the heap across many small allocations, your container's memory profile is more predictable under load.

Checklist: SAJSON in Cloud Run

  • [ ] Build a slim base image (gcr.io/distroless/cc or alpine) with SAJSON headers included
  • [ ] Link your binary statically against SAJSON (no runtime dependency)
  • [ ] Set --memory=512Mi as starting point and reduce based on Prometheus metrics
  • [ ] Enable autoscaling.max-instances cautiously — SAJSON's low memory allows tighter autoscaling bands
  • [ ] Monitor container_memory_working_set_bytes in Cloud Monitoring; expect flat profile vs. sawtooth with nlohmann
  • [ ] Add ulimit -v or cgroup limits as a safety net for malformed超大 payloads

Pattern 3: Dataflow DoFn with SAJSON

Apache Dataflow on GCP processes JSON payloads in worker VMs. SAJSON reduces per-element memory during DoFn.process() calls, which directly impacts shuffle throughput and worker sizing.