Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object! performance could be improved #159

Closed
pmnoxx opened this issue Jun 11, 2019 · 0 comments · Fixed by #164
Closed

object! performance could be improved #159

pmnoxx opened this issue Jun 11, 2019 · 0 comments · Fixed by #164

Comments

@pmnoxx
Copy link

pmnoxx commented Jun 11, 2019

I started using object! macro, but I noticed that it's performing poorly, due to unnecessary memory allocations. We can optimize the code, because we know the number of arguments we are going to put into the array.
Here are the results:

test object ... bench: 52,500,999 ns/iter (+/- 1,924,204)
test object_new ... bench: 33,950,158 ns/iter (+/- 685,775)

`
#![feature(test)]

extern crate test;
#[macro_use]
extern crate json;

use json::JsonValue;
use test::Bencher;


macro_rules! count {
() => (0usize);
( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
}

macro_rules! obj {
{} => ($crate::JsonValue::new_object());

{ $( $key:expr => $value:expr ),* } => {
    obj!( $(
        $key => $value,
    )* )
};

{ $( $key:expr => $value:expr, )* } => ({
    use json::object::Object;

    let cnt = count!( $( $key )* );
    let mut object = Object::with_capacity(cnt);
    $(
        object.insert($key, $value.into());
    )*

    JsonValue::Object(object)
})

}

#[bench]
fn object_new(b: &mut Bencher) {
b.iter(|| {
for it in 0..100_000 {
let mut val: JsonValue = obj! {
"data"=> obj!{
"arg1" => 23,
"arg2" => 44,
},
"test"=> 880032432,
"anki"=> JsonValue::Null,
};
}
});
}

#[bench]
fn object(b: &mut Bencher) {
b.iter(|| {
for it in 0..100_000 {
let mut val: JsonValue = object! {
"data"=> obj!{
"arg1" => 23,
"arg2" => 44,
},
"test"=> 880032432,
"anki"=> JsonValue::Null,
};
}
});
}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant