How bytedance/sonic works
Sonic is a high-performance Go JSON library. The principal workflow is a caller submitting a Go value or JSON input to the public API, which serializes or deserializes it; callers can also parse, inspect, search, and manipulate JSON values through the AST APIs. The implementation includes runtime encoder compilation, multiple decoder paths, and architecture-specific native acceleration. Edges are limited to relationships visible in the excerpts, SOURCE INDEX references, or README; several implementation links remain uncertain because their source was not sampled.
Main entry points: encoder_native.go Encoder API · decoder_native.go Decoder API · compiler.go Encode Compiler · assembler_regabi_amd64.go JIT Decoder
Components 18
Public APIs
- JSON API
api.go - Decoder API
decoder/decoder_native.go - Encoder API
encoder/encoder_native.go - Compatibility Fallbacks
decoder/decoder_compat.go - Encoding Options
option/option.go
JSON Value APIs
- JSON Nodes
ast/node.go - JSON Parser
ast/parser.go - Path Search
ast/search.go
Runtime Binding
- Encode Compiler
internal/encoder/compiler.go - Encode Assembler
internal/encoder/x86/assembler_regabi_amd64.go - Field Resolver
internal/resolver/fields.go - JIT Decoder
internal/decoder/jitdec/assembler_regabi_amd64.go - Optimized Decoder
internal/decoder/optdec/node.go - JIT Runtime
internal/jit/assembler_amd64.go - Code Loader
loader/loader.go
Native Acceleration
- Native Dispatch
internal/native/dispatch_amd64.go - SIMD Primitives
native/scanning.h
Other components
- Go caller
Connections 19
- Go caller → JSON API: uses JSON
README.md - JSON API → JSON Nodes: uses AST
api.go - Go caller → Encoder API: encodes values
README.md - Go caller → Decoder API: decodes JSON
README.md - Go caller → JSON Nodes: manipulates values
README.md - JSON Parser → JSON Nodes: parses values
- Path Search → JSON Parser: searches input
- Encoder API → Encode Compiler: compiles encoding
- Encode Compiler → Field Resolver: resolves fields
internal/encoder/compiler.go - Encode Compiler → Encode Assembler: builds program
- Encode Assembler → JIT Runtime: uses JIT
internal/encoder/x86/assembler_regabi_amd64.go - JIT Decoder → JIT Runtime: uses JIT
internal/decoder/jitdec/assembler_regabi_amd64.go - JIT Decoder → Native Dispatch: calls native
internal/decoder/jitdec/assembler_regabi_amd64.go - Native Dispatch → SIMD Primitives: dispatches primitives
- JIT Runtime → Code Loader: assembles code
internal/jit/assembler_amd64.go - Decoder API → JIT Decoder: binds at runtime
- Decoder API → Optimized Decoder: uses optimized path
- Encoder API → Compatibility Fallbacks: fallbacks available
- Encoder API → Encoding Options: accepts options