Torque

High-performance JSON library for Elixir via Rustler NIFs, powered by sonic-rs (SIMD-accelerated).

Torque provides the fastest JSON encoding and decoding available in the BEAM ecosystem, with a selective field extraction API for workloads that only need a subset of fields from each document.

Features

Installation

Add to your mix.exs:

def deps do
[
{:torque, "~> 0.2.3"}
]
end

Precompiled binaries are available for common targets. To compile from source, install a stable Rust toolchain and set TORQUE_BUILD=true.

CPU-optimized variants

On x86_64, precompiled binaries are available for three CPU feature levels:

VariantCPU featurestarget-cpu
baselineSSE2x86-64
v2SSE4.2, SSSE3, POPCNTx86-64-v2
v3AVX2, AVX, BMI1, BMI2, FMAx86-64-v3

At compile time, Torque auto-detects the host CPU and downloads the best matching variant. To override detection (e.g., when cross-compiling for a different target):

TORQUE_CPU_VARIANT=v2 mix compile # force SSE4.2 variant
TORQUE_CPU_VARIANT=v3 mix compile # force AVX2 variant
TORQUE_CPU_VARIANT=base mix compile # force baseline

Usage

Decoding

{:ok, data} = Torque.decode(~s({"name":"Alice","age":30}))
# %{"name" => "Alice", "age" => 30}
data = Torque.decode!(json)

Selective Field Extraction

Parse once, extract many fields without building the full Elixir term tree:

{:ok, doc} = Torque.parse(json)
{:ok, "example.com"} = Torque.get(doc, "/site/domain")
nil = Torque.get(doc, "/missing/field", nil)
# Batch extraction (single NIF call, fastest path)
results = Torque.get_many(doc, ["/id", "/site/domain", "/device/ip"])
# [{:ok, "req-1"}, {:ok, "example.com"}, {:ok, "1.2.3.4"}]

When your JSON is known to have no duplicate object keys, pass unique_keys: true for faster field lookups (uses sonic-rs internal indexing instead of linear scan):

{:ok, doc} = Torque.parse(json, unique_keys: true)

Compiled Pointers

When the same fixed set of paths is extracted from every document, compile the pointers once and reuse the handle. parse_get_many_nil/2 then fuses the parse and extraction into a single NIF call, skipping all per-request path parsing — roughly 1.5× faster end-to-end than parse/2 + get_many_nil/2.

# Once, at startup (e.g. a module attribute or :persistent_term):
pointers = Torque.compile_pointers(["/id", "/site/domain", "/imp/0/banner/w"], unique_keys: true)
# Per document — parse + extract in one call:
{:ok, ["req-1", "example.com", 300]} = Torque.parse_get_many_nil(json, pointers)

Missing fields and JSON null both become nil. The handle also works with an already-parsed document via Torque.get_many_nil(doc, pointers).

Encoding

# Maps with atom or binary keys
{:ok, json} = Torque.encode(%{id: "abc", price: 1.5})
# "{\"id\":\"abc\",\"price\":1.5}"
# Bang variant
json = Torque.encode!(%{id: "abc"})
# iodata variant (fastest, no {:ok, ...} tuple wrapping)
json = Torque.encode_to_iodata(%{id: "abc"})
# jiffy-compatible proplist format
{:ok, json} = Torque.encode({[{:id, "abc"}, {:price, 1.5}]})

API

FunctionDescription
Torque.compile_pointers(paths, opts)Pre-compile a fixed path set into a reusable handle
Torque.decode(binary)Decode JSON to Elixir terms
Torque.decode!(binary)Decode JSON, raising on error
Torque.encode(term)Encode term to JSON binary
Torque.encode!(term)Encode term, raising on error
Torque.encode_to_iodata(term)Encode term, returns binary directly (fastest)
Torque.get(doc, path)Extract field by JSON Pointer path
Torque.get(doc, path, default)Extract field with default for missing paths
Torque.get_many(doc, paths)Extract multiple fields in one NIF call
Torque.get_many_nil(doc, paths)Extract multiple fields, nil for missing
Torque.length(doc, path)Return length of array at path
Torque.parse(binary, opts)Parse JSON into opaque document reference
Torque.parse_get_many_nil(binary, pointers)Fused parse + extract of compiled pointers in one NIF call

Type Conversion

JSON to Elixir

JSONElixir
objectmap (binary keys)
arraylist
stringbinary
integerinteger
floatfloat
true, falsetrue, false
nullnil

For objects with duplicate keys, the last value wins (unless unique_keys: true is passed to parse/2).

Integers outside the signed/unsigned 64-bit range decode as exact arbitrary-precision integers (Erlang bignums) via decode/1, rather than degrading to lossy floats. The parse/2 + get/2 path returns them as floats, since the parsed document cannot hold a bignum.

Elixir to JSON

ElixirJSON
map (atom/binary keys)object
listarray
binarystring
integernumber
floatnumber
true, falsetrue, false
nilnull
atomstring
{keyword_list}object

Errors

Functions return {:error, reason} tuples (or raise ArgumentError for bang/iodata variants). Possible reason atoms:

Decode / Parse

AtomReturned byMeaning
:nesting_too_deepdecode/1, parse/1, get/2, get_many/2, parse_get_many_nil/2Document exceeds 128 nesting levels

parse/1, decode/1, and parse_get_many_nil/2 also return {:error, binary} with a message from sonic-rs for malformed JSON.

Encode

AtomReturned byMeaning
:unsupported_typeencode/1Term has no JSON representation (PID, reference, port, …)
:invalid_utf8encode/1Binary string or map key is not valid UTF-8
:invalid_keyencode/1Map key is not an atom or binary (e.g. integer key)
:malformed_proplistencode/1{proplist} contains a non-{key, value} element
:non_finite_floatencode/1Float is infinity or NaN (unreachable from normal BEAM code)
:nesting_too_deepencode/1Term exceeds 128 nesting levels

Benchmarks

Apple M2 Pro, OTP 29, Elixir 1.20. Both libraries are profile-guided optimised (PGO) builds: Torque PGO (via scripts/pgo-build.sh) and Glazer PGO (via OPTIMIZE=1).

Decode (1.2 KB OpenRTB)

Libraryipsmeanmedianp99memory
torque413.3K2.42 μs2.29 μs4.04 μs1.56 KB
glazer401.8K2.49 μs2.38 μs4.58 μs1.56 KB
jiffy202.5K4.94 μs4.50 μs9.75 μs1.55 KB
simdjsone178.2K5.61 μs5.25 μs12.63 μs1.59 KB
otp json147.4K6.78 μs6.58 μs10.58 μs7.73 KB
jason113.2K8.83 μs8.46 μs13.96 μs9.54 KB

Decode (750 KB Twitter)

Libraryipsmeanmedianp99memory
torque660.71.51 ms1.33 ms2.21 ms1.57 KB
glazer659.61.52 ms1.43 ms1.94 ms1.58 KB
simdjsone413.62.42 ms1.93 ms3.75 ms1.59 KB
jiffy301.43.32 ms3.39 ms3.96 ms2.30 MB
otp json205.34.87 ms4.80 ms7.52 ms2.48 MB
jason151.76.59 ms6.58 ms6.97 ms3.52 MB

Encode (1.2 KB OpenRTB)

Libraryipsmeanmedianp99memory
otp json [map() :: iodata()]1180K0.85 μs0.79 μs1.08 μs3928 B
torque [proplist() :: binary()]1110K0.90 μs0.83 μs1.08 μs88 B
torque [proplist() :: iodata()]1080K0.92 μs0.83 μs1.83 μs64 B
torque [map() :: iodata()]1000K1.00 μs0.96 μs1.13 μs64 B
glazer [map() :: binary()]990K1.01 μs0.92 μs1.29 μs64 B
torque [map() :: binary()]980K1.02 μs0.96 μs1.17 μs88 B
jiffy [proplist() :: iodata()]640K1.57 μs1.33 μs1.83 μs120 B
jason [map() :: iodata()]620K1.62 μs1.54 μs2.63 μs3848 B
jiffy [map() :: iodata()]520K1.91 μs1.75 μs2.25 μs824 B
simdjsone [proplist() :: iodata()]410K2.42 μs2.29 μs3.00 μs184 B
jason [map() :: binary()]380K2.63 μs2.38 μs6.42 μs3912 B
simdjsone [map() :: iodata()]340K2.94 μs2.75 μs4.38 μs888 B

Encode (750 KB Twitter)

Libraryipsmeanmedianp99memory
torque [proplist() :: iodata()]1156.00.87 ms0.85 ms1.06 ms64 B
torque [proplist() :: binary()]1154.10.87 ms0.85 ms1.06 ms88 B
torque [map() :: binary()]1045.40.96 ms0.95 ms1.13 ms88 B
torque [map() :: iodata()]1040.40.96 ms0.96 ms1.13 ms64 B
glazer [map() :: binary()]1033.30.97 ms0.96 ms1.08 ms64 B
jiffy [proplist() :: iodata()]469.52.13 ms2.10 ms2.51 ms37.7 KB
jiffy [map() :: iodata()]347.62.88 ms2.96 ms3.90 ms1.06 MB
simdjsone [proplist() :: iodata()]254.73.93 ms3.87 ms5.44 ms37.7 KB
otp json [map() :: iodata()]251.83.97 ms4.13 ms6.44 ms5.40 MB
jason [map() :: iodata()]233.54.28 ms4.03 ms6.50 ms4.96 MB
simdjsone [map() :: iodata()]215.14.65 ms4.76 ms5.22 ms1.06 MB
jason [map() :: binary()]127.97.82 ms7.84 ms8.43 ms4.96 MB

Parse (1.2 KB OpenRTB)

Libraryipsmeanmedianp99
torque parse(unique_keys)542.0K1.84 μs1.46 μs5.63 μs
torque parse522.8K1.91 μs1.46 μs6.00 μs
simdjsone parse306.3K3.27 μs1.21 μs5.83 μs

Extract 5 fields from raw JSON (1.2 KB OpenRTB)

End-to-end cost of pulling 5 fields out of a JSON blob: parse + get (torque, simdjsone) vs decode + find (glazer has no lazy handle, so it must fully decode first). This is the apples-to-apples version of "get" — torque's selective extraction skips materializing the whole document.

Libraryipsmeanmedianp99
torque parse + get_many436.6K2.29 μs1.79 μs6.21 μs
torque parse(unique_keys) + get_many429.5K2.33 μs1.79 μs6.67 μs
torque parse + get x5409.8K2.44 μs2.00 μs6.67 μs
simdjsone parse + get x5374.9K2.67 μs1.79 μs7.21 μs
glazer decode + find x5344.9K2.90 μs2.75 μs6.71 μs

Run benchmarks locally:

MIX_ENV=bench mix run bench/torque_bench.exs

Limitations

License

MIT