GithubHelp home page GithubHelp logo

Benchmarks and performance about deku HOT 5 OPEN

sharksforarms avatar sharksforarms commented on May 5, 2024
Benchmarks and performance

from deku.

Comments (5)

sharksforarms avatar sharksforarms commented on May 5, 2024

Added some simple benchmarks to catch regressions and measure optimisations on reading

from deku.

OliverGavin avatar OliverGavin commented on May 5, 2024

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.

sharksforarms avatar sharksforarms commented on May 5, 2024

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.

OliverGavin avatar OliverGavin commented on May 5, 2024

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.

sharksforarms avatar sharksforarms commented on May 5, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.