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.1.9"}
  ]
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, get/2, get_many/2Document exceeds 512 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 512 nesting levels

Benchmarks

Apple M2 Pro, OTP 28, Elixir 1.19:

Decode (1.2 KB OpenRTB)

Libraryipsmeanmedianp99memory
torque262.5K3.81 μs3.63 μs7.83 μs1.56 KB
simdjsone182.7K5.47 μs5.13 μs11.88 μs1.59 KB
jiffy144.6K6.92 μs6.21 μs17.17 μs1.56 KB
otp json129.6K7.72 μs7.21 μs16.50 μs7.73 KB
jason103.6K9.65 μs8.71 μs22.75 μs9.54 KB

Decode (750 KB Twitter)

Libraryipsmeanmedianp99memory
torque476.02.10 ms1.87 ms4.73 ms1.56 KB
simdjsone459.42.18 ms1.85 ms3.20 ms1.56 KB
otp json195.15.13 ms5.12 ms6.16 ms2.49 MB
jason142.07.04 ms6.91 ms11.47 ms3.55 MB
jiffy115.98.63 ms8.72 ms9.94 ms5.53 MB

Encode (1.2 KB OpenRTB)

Libraryipsmeanmedianp99memory
otp json [map() :: iodata()]1091.6K0.92 μs0.83 μs1.46 μs3928 B
torque [proplist() :: binary()]1073.6K0.93 μs0.88 μs1.13 μs88 B
torque [proplist() :: iodata()]1069.3K0.94 μs0.88 μs1.17 μs64 B
torque [map() :: binary()]917.5K1.09 μs1.00 μs1.33 μs88 B
torque [map() :: iodata()]914.6K1.09 μs1.00 μs1.42 μs64 B
jason [map() :: iodata()]571.8K1.75 μs1.54 μs3.75 μs3848 B
jiffy [proplist() :: iodata()]518.4K1.93 μs1.67 μs2.75 μs120 B
jiffy [map() :: iodata()]427.6K2.34 μs2.08 μs4.33 μs824 B
simdjsone [proplist() :: iodata()]415.4K2.41 μs2.21 μs3.96 μs184 B
jason [map() :: binary()]385.1K2.60 μs2.38 μs5.00 μs3912 B
simdjsone [map() :: iodata()]346.8K2.88 μs2.67 μs4.33 μs888 B

Encode (750 KB Twitter)

Libraryipsmeanmedianp99memory
torque [proplist() :: iodata()]1026.40.97 ms0.96 ms1.18 ms64 B
torque [proplist() :: binary()]983.51.02 ms0.98 ms1.69 ms88 B
torque [map() :: binary()]918.51.09 ms1.08 ms1.31 ms88 B
torque [map() :: iodata()]905.41.10 ms1.09 ms1.35 ms64 B
jiffy [proplist() :: iodata()]342.62.92 ms2.86 ms4.35 ms37.7 KB
jiffy [map() :: iodata()]270.83.69 ms3.53 ms5.94 ms1.06 MB
jason [map() :: iodata()]254.93.92 ms3.70 ms6.50 ms4.96 MB
simdjsone [proplist() :: iodata()]247.44.04 ms3.98 ms5.63 ms37.7 KB
otp json [map() :: iodata()]246.94.05 ms4.13 ms5.64 ms5.40 MB
simdjsone [map() :: iodata()]210.54.75 ms4.78 ms5.41 ms1.06 MB
jason [map() :: binary()]141.17.09 ms7.02 ms8.40 ms4.96 MB

Parse (1.2 KB OpenRTB)

Libraryipsmeanmedianp99
torque parse(unique_keys)596.6K1.68 μs1.33 μs3.13 μs
torque parse579.2K1.73 μs1.33 μs3.88 μs
simdjsone parse364.9K2.74 μs1.17 μs4.92 μs

Get (5 fields) (1.2 KB OpenRTB)

Libraryipsmeanmedianp99memory
torque get_many_nil (unique_keys)2.49M402 ns375 ns500 ns240 B
torque get_many (unique_keys)2.37M422 ns375 ns500 ns360 B
torque get_many_nil2.16M463 ns458 ns583 ns240 B
torque get_many2.07M483 ns458 ns584 ns360 B
simdjsone get1.77M564 ns458 ns1083 ns384 B
torque get (unique_keys)1.67M601 ns583 ns709 ns384 B
torque get1.50M669 ns625 ns792 ns384 B

Run benchmarks locally:

MIX_ENV=bench mix run bench/torque_bench.exs

Limitations

  • Nesting depth: JSON documents nested deeper than 512 levels return {:error, :nesting_too_deep} from decode/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.

License

MIT