How TikTok’s Sonic Library Supercharges Go JSON Performance
This article explains how TikTok engineers built Sonic, a high‑performance Go JSON library that leverages JIT compilation, SIMD instructions, smart memory handling, and optional features to dramatically reduce latency and memory usage compared with the standard encoding/json package, offering real‑world cost and speed benefits.
Every Go developer knows the JSON problem
If you use Go’s standard encoding/json package it works, but it isn’t the fastest, and at TikTok scale—billions of requests per day—tiny improvements translate into huge cost and latency savings.
Speed champion: Meet Sonic
Benchmarks on a typical 13 KB JSON file show:
// Processing user profile with posts and metadata
Standard JSON: 106,322 ns/op
Sonic: 32,393 ns/op
Memory Usage:
- Standard: 49,136 bytes with 789 allocations
- Sonic: 11,965 bytes with just 4 allocationsAdditional benchmarks on small (400 B) and large (635 KB) payloads confirm similar gains.
1. JIT – instant compilation
Instead of using generic code for every JSON structure, Sonic generates specialized code at runtime, similar to a chef creating a custom recipe for a frequently cooked dish.
Standard Go JSON: one generic code path for all JSON.
Sonic: a dedicated code path per schema.
2. SIMD – doing more work per cycle
Sonic employs SIMD instructions to process multiple bytes simultaneously, akin to sorting many cards at once instead of one by one.
3. Smart memory usage
When Sonic encounters a plain string without special characters, it references the original string instead of copying it, saving allocations.
// Example JSON
{"name": "John", "city": "New York"}
// Standard library copies strings
// Sonic just references them if they are simple4. Optional features for extra speed
Map key sorting disabled by default (≈10% faster).
HTML escaping disabled by default (≈15% faster).
These can be enabled when needed, keeping the common path fast.
Design highlights
JIT assembles opcode for each schema at runtime and caches it in off‑heap memory.
Combines SIMD with size‑based heuristics to handle both large and small data efficiently.
Uses a custom asm2asm tool to translate optimized x86 assembly into Plan 9 assembly for Go runtime.
AST parser employs lazy‑load mechanisms to reduce multi‑key lookup overhead.
Further optimizations include a lightweight function‑call mechanism (global‑function‑table + static offset) and a high‑performance open‑addressing hash cache with RCU for read‑heavy workloads.
Real‑world benefits
For medium‑size JSON, Sonic reduces memory usage from ~49 KB with 789 allocations to ~12 KB with only 4 allocations, dramatically lowering server cost and improving API response times.
Should you switch to Sonic?
Consider Sonic if you process large volumes of JSON, performance is critical, and you run on AMD64 or ARM64 with Go 1.17+ (Linux, macOS, Windows supported). Some features like HTML escaping must be explicitly enabled.
Quick start example
import "github.com/bytedance/sonic"
// Encoding
data := map[string]string{"hello": "world"}
bytes, err := sonic.Marshal(data)
// Decoding
var result map[string]string
err = sonic.Unmarshal(bytes, &result)Conclusion
Sonic shows that even a ubiquitous task like JSON handling can be dramatically improved with modern CPU features (SIMD), just‑in‑time compilation, and thoughtful design choices, delivering faster APIs, lower costs, and better user experiences.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
