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

  • SIMD-accelerated decoding (AVX2/SSE4.2 on x86, NEON on ARM)
  • Ultra-low memory encoder (64 B per encode vs ~4 KB for OTP json/jason)
  • Parse-then-get API for selective field extraction via JSON Pointer (RFC 6901)
  • Batch field extraction (get_many/2) with single NIF call
  • Automatic dirty CPU scheduler dispatch for inputs larger than 20 KB
  • jiffy-compatible {proplist} encoding

Installation

Add to your mix.exs:

def deps do
  [
    {:torque, "~> 0.2.0"}
  ]
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)

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.decode(binary)Decode JSON to Elixir terms
Torque.decode!(binary)Decode JSON, raising on error
Torque.parse(binary, opts)Parse JSON into opaque document reference
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.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)

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).

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/2Document exceeds 128 nesting levels

parse/1 and decode/1 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:

Decode (1.2 KB OpenRTB)

Libraryipsmeanmedianp99memory
torque392.8K2.55 μs2.46 μs4.25 μs1.56 KB
glazer298.9K3.35 μs3.00 μs8.92 μs1.56 KB
jiffy208.8K4.79 μs4.33 μs10.13 μs1.55 KB
simdjsone181.6K5.51 μs5.29 μs9.88 μs1.59 KB
otp json125.2K7.99 μs7.71 μs13.08 μs7.73 KB
jason92.6K10.80 μs10.13 μs22.33 μs9.54 KB

Decode (750 KB Twitter)

Libraryipsmeanmedianp99memory
torque655.11.53 ms1.32 ms2.16 ms1.57 KB
glazer590.81.69 ms1.53 ms2.56 ms1.58 KB
simdjsone392.52.55 ms2.10 ms4.53 ms1.59 KB
jiffy286.33.49 ms3.59 ms3.92 ms2.30 MB
otp json165.26.05 ms5.42 ms16.57 ms2.48 MB
jason142.17.04 ms7.01 ms8.15 ms3.54 MB

Encode (1.2 KB OpenRTB)

Libraryipsmeanmedianp99memory
torque [proplist() :: iodata()]1280K0.78 μs0.71 μs0.88 μs64 B
torque [proplist() :: binary()]1190K0.84 μs0.75 μs1.63 μs88 B
glazer [map() :: binary()]1090K0.92 μs0.83 μs1.13 μs64 B
otp json [map() :: iodata()]1080K0.92 μs0.79 μs2.04 μs3928 B
torque [map() :: iodata()]1050K0.95 μs0.88 μs1.13 μs64 B
torque [map() :: binary()]1040K0.96 μs0.88 μs1.13 μs88 B
jiffy [proplist() :: iodata()]730K1.37 μs1.13 μs2.33 μs120 B
jiffy [map() :: iodata()]620K1.62 μs1.42 μs2.04 μs824 B
jason [map() :: iodata()]610K1.63 μs1.50 μs2.88 μs3848 B
simdjsone [proplist() :: iodata()]440K2.25 μs2.04 μs2.88 μs184 B
jason [map() :: binary()]380K2.66 μs2.38 μs6.29 μs3912 B
simdjsone [map() :: iodata()]370K2.69 μs2.42 μs4.79 μs888 B

Encode (750 KB Twitter)

Libraryipsmeanmedianp99memory
torque [proplist() :: iodata()]1228.70.81 ms0.80 ms1.00 ms64 B
torque [proplist() :: binary()]1226.60.82 ms0.80 ms0.99 ms88 B
torque [map() :: binary()]1098.90.91 ms0.89 ms1.17 ms88 B
torque [map() :: iodata()]1077.40.93 ms0.91 ms1.12 ms64 B
glazer [map() :: binary()]981.91.02 ms1.00 ms1.28 ms64 B
jiffy [proplist() :: iodata()]536.51.86 ms1.82 ms2.98 ms37.7 KB
jiffy [map() :: iodata()]439.82.27 ms2.26 ms2.48 ms1.06 MB
otp json [map() :: iodata()]267.33.74 ms4.08 ms6.51 ms5.40 MB
jason [map() :: iodata()]260.63.84 ms3.58 ms6.07 ms4.96 MB
simdjsone [proplist() :: iodata()]255.63.91 ms3.78 ms6.60 ms37.7 KB
simdjsone [map() :: iodata()]214.74.66 ms4.64 ms6.61 ms1.06 MB
jason [map() :: binary()]136.67.32 ms7.10 ms9.02 ms4.96 MB

Parse (1.2 KB OpenRTB)

Libraryipsmeanmedianp99
torque parse(unique_keys)572.1K1.75 μs1.33 μs5.75 μs
torque parse549.5K1.82 μs1.33 μs6.08 μs
simdjsone parse320.4K3.12 μs1.21 μs5.96 μ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(unique_keys) + get_many443.3K2.26 μs1.71 μs6.67 μs
torque parse + get_many425.3K2.35 μs1.79 μs6.04 μs
torque parse + get x5419.9K2.38 μs2.00 μs6.92 μs
simdjsone parse + get x5371.7K2.69 μs1.67 μs7.00 μs
glazer decode + find x5317.0K3.15 μs2.83 μs7.71 μs

Run benchmarks locally:

MIX_ENV=bench mix run bench/torque_bench.exs

Limitations

  • Nesting depth: JSON documents nested deeper than 128 levels return {:error, :nesting_too_deep} from decode/1, parse/1, get/2, get_many/2, and encode/1 rather than crashing the VM. Real-world documents are never this deep; the limit exists to prevent stack overflow in the NIF (the dirty CPU scheduler, used for inputs over 20 KB, has a small stack).

License

MIT