Comments (5)
Added some simple benchmarks to catch regressions and measure optimisations on reading
from deku.
I was playing with the magic attribute and benchmarks and I realized it is very slow so I thought I would share:
#![feature(test)]
use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "big", magic = b"\x00\x00\x00\x05")]
struct WithMagicV5 {
value: u32
}
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "big")]
struct WithoutMagicV5 {
#[deku(assert = "*version == 5")]
version: u32, // users ideally should not be able to set this as it is always 5
value: u32
}
#[cfg_attr(test, macro_use)]
extern crate hex_literal;
#[cfg(test)]
mod magic_benches {
use super::*;
extern crate test;
use test::Bencher;
#[bench]
fn bench_with_magic(b: &mut Bencher) {
let data = hex!("00 00 00 05 00 00 00 01");
b.iter(|| {
let (_, _) = WithMagicV5::from_bytes((&data, 0)).unwrap();
});
}
#[bench]
fn bench_without_magic(b: &mut Bencher) {
let data = hex!("00 00 00 05 00 00 00 01");
b.iter(|| {
let (_, _) = WithoutMagicV5::from_bytes((&data, 0)).unwrap();
});
}
}
And the results:
running 2 tests
test magic_benches::bench_with_magic ... bench: 48 ns/iter (+/- 4)
test magic_benches::bench_without_magic ... bench: 21 ns/iter (+/- 6)
from deku.
I was playing with the magic attribute and benchmarks and I realized it is very slow so I thought I would share:
Nice one!
There are some optimizations which could be done. This is a great example.
For example, magic
attribute handling could convert the byte string to 0x00000005u32
(at compile time?) and do a single u32
read and equality check. Currently, it would do 4 u8
reads and 4 equality checks (iterating the byte string).
To add flexibility, magic
could also accept a number or a bytestring!
from deku.
Oh yeah, actually the first problem I faced with magic
was that I couldn't pass a number (and it wasn't clear from the docs as it was a string example).
I'd be happy to give this a shot if you could give me a rough idea where to look? Seems relatively easy.
from deku.
@OliverGavin Yes! That would be awesome. Happy to show you where to start.
I'd take a look at how the id
attribute works, this attribute accepts both b"bytes"
and tokens
: https://github.com/sharksforarms/deku/blob/master/deku-derive/src/lib.rs#L17
The magic attribute is declared here: https://github.com/sharksforarms/deku/blob/master/deku-derive/src/lib.rs#L121 and as you can see only accepts a LitByteStr
. This would involve changing the type to an enum to accept either b""
and other desired variants (such as LitInt)
I'll create an issue for this!
Edit: #204
from deku.
Related Issues (20)
- How to read repeatedly? HOT 2
- `DekuWrite`: multiple `write` found HOT 7
- Right way to imply a checksum HOT 12
- Deku v0.13.0/v0.12.6 - cannot download HOT 1
- Implement zero-copy read for Cow<[u8]> just like &[u8]
- `id_pat` with external `id` HOT 2
- Support `arbitrary_enum_discriminant`
- Add trait to get binary size of messages
- Add "bitfield" to Cargo.toml keywords HOT 4
- Type mismatch when using anyhow HOT 2
- Can't use newtype-structs together with deku-attributes HOT 5
- Context cannot contain a reference HOT 1
- Make endianness dynamic/conditional. HOT 6
- `deku::input` is unusable
- Reader and Writer should modify value of Option. HOT 3
- Enum discriminant re/mapping HOT 6
- Can't use enum inside structs HOT 4
- i get endian in the binary, how to set endian in the root? HOT 3
- Using Traits within `ctx` and `endian` fails for some cases HOT 3
- Cannot specify struct endianness when a nested struct field is present HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deku.