1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 07:26:04 +00:00

use mimalloc

This commit is contained in:
mat 2024-01-19 18:21:44 -06:00
parent 4e0002639b
commit ecb0c42f1f
4 changed files with 16 additions and 10 deletions

6
.gitignore vendored
View file

@ -2,6 +2,6 @@
/Cargo.lock
.vscode
/flamegraph.svg
/perf.data
/perf.data.old
flamegraph.svg
perf.data
perf.data.old

View file

@ -51,21 +51,27 @@ 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](https://docs.rs/mimalloc/latest/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 takes some shortcuts to be this fast, some people call it cheating but I think it's fair game:
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 in existence.
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 (though without actually accessing the data):
Here's a benchmark comparing Simdnbt against a few of the other fastest NBT crates (though without actually accessing the data):
![simdnbt is ~3x faster than the second fastest nbt crate](https://github.com/azalea-rs/simdnbt/assets/27899617/03a4f916-d162-4a23-aa1a-12f1b11dc903)

View file

@ -23,7 +23,7 @@ fastnbt = "2.4.4"
azalea-nbt = { git = "https://github.com/azalea-rs/azalea", rev = "84e036ce3752ecf57904b0f5aff1f33d43e95a32" }
hematite-nbt = { version = "0.5.2", default-features = false }
tikv-jemallocator = "0.5"
mimalloc = "0.1.39"
[features]
default = ["derive"]
@ -35,7 +35,7 @@ debug = false
[profile.bench]
lto = true
debug = false
debug = true
[[bench]]
harness = false

View file

@ -47,10 +47,10 @@ fn bench_file(filename: &str, c: &mut Criterion) {
}
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn bench(c: &mut Criterion) {
bench_file("bigtest.nbt", c);
// bench_file("bigtest.nbt", c);
// bench_file("simple_player.dat", c);
bench_file("complex_player.dat", c);
// bench_file("level.dat", c);