9 unstable releases (3 breaking)

0.4.2 Mar 15, 2024
0.4.1 Mar 10, 2024
0.4.0 Jan 25, 2024
0.3.0 Dec 2, 2023
0.1.0 Aug 30, 2023

#255 in Encoding

Download history 36/week @ 2023-12-22 4/week @ 2023-12-29 2/week @ 2024-01-05 5/week @ 2024-01-12 8/week @ 2024-01-19 1/week @ 2024-01-26 19/week @ 2024-02-02 15/week @ 2024-02-09 40/week @ 2024-02-16 49/week @ 2024-02-23 34/week @ 2024-03-01 281/week @ 2024-03-08 228/week @ 2024-03-15 40/week @ 2024-03-22 68/week @ 2024-03-29 29/week @ 2024-04-05

437 downloads per month
Used in 15 crates (9 directly)

MIT license

125KB
3.5K SLoC

simdnbt

Simdnbt is a very fast NBT serializer and deserializer.

It was originally made as a joke but it ended up being too good of a joke so it's actually a thing now.

Usage

cargo add simdnbt

Deserializing

For deserializing, you'll likely want either simdnbt::borrow::Nbt::read or simdnbt::owned::Nbt::read. The difference is that the "borrow" variant requires you to keep a reference to the original buffer, but is significantly faster.

use std::borrow::Cow;
use std::io::Cursor;
use simdnbt::borrow::Nbt;

fn example(item_bytes: &[u8]) {
    let nbt = Nbt::read(&mut Cursor::new(item_bytes))
        .unwrap()
        .unwrap();
    let skyblock_id: Cow<str> = nbt
        .list("i")
        .and_then(|i| i.compounds())
        .and_then(|i| i.get(0))
        .and_then(|i| i.compound("tag"))
        .and_then(|tag| tag.compound("ExtraAttributes"))
        .and_then(|ea| ea.string("id"))
        .map(|id| id.to_string_lossy())
        .unwrap_or_default();
}

Serializing

use simdnbt::owned::{BaseNbt, Nbt, NbtCompound, NbtTag};

let nbt = Nbt::Some(BaseNbt::new(
    "",
    NbtCompound::from_values(vec![
        ("key".into(), NbtTag::String("value".into())),
    ]),
));
let mut buffer = Vec::new();
nbt.write(&mut buffer);

Performance guide

Use the borrow variant of Nbt if possible, and avoid allocating unnecessarily (for example, keep strings as Cow<str> if you can).

The most significant and simple optimization you can do is switching to an allocator like mimalloc (it's ~20% faster on my machine). Setting RUSTFLAGS='-C target-cpu=native' when running your code may also help a little bit.

Implementation details

Simdnbt currently makes use of SIMD instructions for two things:

  • swapping the endianness of int arrays
  • checking if a string is plain ascii for faster mutf8 to utf8 conversion

Simdnbt cheats takes some shortcuts to be this fast:

  1. it requires a reference to the original data (to avoid cloning)
  2. it doesn't validate/decode the mutf-8 strings at decode-time

Benchmarks

Simdnbt is likely the fastest NBT decoder currently in existence.

Here's a benchmark comparing Simdnbt against a few of the other fastest NBT crates for decoding complex_player.dat:

Library Throughput
simdnbt::borrow 717.45 MiB/s
simdnbt::owned 329.10 MiB/s
shen_nbt5 306.58 MiB/s
azalea_nbt 297.28 MiB/s
valence_nbt 236.42 MiB/s
graphite_binary 210.51 MiB/s
fastnbt 115.54 MiB/s
hematite_nbt 108.91 MiB/s

And for writing complex_player.dat:

Library Throughput
simdnbt::borrow 2.5914 GiB/s
azalea_nbt 2.1096 GiB/s
simdnbt::owned 1.9508 GiB/s
graphite_binary 1.7745 GiB/s

The tables above were made from the compare benchmark in this repo. Note that the benchmark is somewhat unfair, since simdnbt::borrow doesn't fully decode some things like strings and integer arrays until they're used. Also keep in mind that if you run your own benchmark you'll get different numbers, but the speeds should be about the same relative to each other.

Dependencies

~0.9–1.4MB
~30K SLoC