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

Replace peak with more efficient peek #48

Merged
merged 6 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Object(
To use [Jiter], you need to know what schema you're expecting:

```rust
use jiter::{Jiter, NumberInt, Peak};
use jiter::{Jiter, NumberInt, Peek};

fn main() {
let json_data = r#"
Expand All @@ -75,10 +75,10 @@ fn main() {
assert_eq!(jiter.next_key().unwrap(), Some("age"));
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(43));
assert_eq!(jiter.next_key().unwrap(), Some("phones"));
assert_eq!(jiter.next_array().unwrap(), Some(Peak::String));
assert_eq!(jiter.next_array().unwrap(), Some(Peek::String));
// we know the next value is a string as we just asserted so
assert_eq!(jiter.known_str().unwrap(), "+44 1234567");
assert_eq!(jiter.array_step().unwrap(), Some(Peak::String));
assert_eq!(jiter.array_step().unwrap(), Some(Peek::String));
// same again
assert_eq!(jiter.known_str().unwrap(), "+44 2345678");
// next we'll get `None` from `array_step` as the array is finished
Expand Down
42 changes: 21 additions & 21 deletions benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hint::black_box;
use std::fs::File;
use std::io::Read;

use jiter::{Jiter, JsonValue, Peak};
use jiter::{Jiter, JsonValue, Peek};
use serde_json::Value;

fn read_file(path: &str) -> String {
Expand All @@ -31,11 +31,11 @@ fn jiter_iter_big(path: &str, bench: &mut Bencher) {
jiter.next_array().unwrap();

loop {
if let Some(peak) = jiter.next_array().unwrap() {
let i = jiter.known_float(peak).unwrap();
if let Some(peek) = jiter.next_array().unwrap() {
let i = jiter.known_float(peek).unwrap();
black_box(i);
while let Some(peak) = jiter.array_step().unwrap() {
let i = jiter.known_float(peak).unwrap();
while let Some(peek) = jiter.array_step().unwrap() {
let i = jiter.known_float(peek).unwrap();
black_box(i);
}
}
Expand All @@ -47,10 +47,10 @@ fn jiter_iter_big(path: &str, bench: &mut Bencher) {
}

fn find_string(jiter: &mut Jiter) -> String {
let peak = jiter.peak().unwrap();
match peak {
Peak::String => jiter.known_str().unwrap().to_string(),
Peak::Array => {
let peek = jiter.peek().unwrap();
match peek {
Peek::String => jiter.known_str().unwrap().to_string(),
Peek::Array => {
assert!(jiter.known_array().unwrap().is_some());
let s = find_string(jiter).to_string();
assert!(jiter.array_step().unwrap().is_none());
Expand Down Expand Up @@ -93,11 +93,11 @@ fn jiter_iter_true_array(path: &str, bench: &mut Bencher) {
let json_data = black_box(json.as_bytes());
bench.iter(|| {
let mut jiter = Jiter::new(json_data, false);
let first_peak = jiter.next_array().unwrap().unwrap();
let i = jiter.known_bool(first_peak).unwrap();
let first_peek = jiter.next_array().unwrap().unwrap();
let i = jiter.known_bool(first_peek).unwrap();
black_box(i);
while let Some(peak) = jiter.array_step().unwrap() {
let i = jiter.known_bool(peak).unwrap();
while let Some(peek) = jiter.array_step().unwrap() {
let i = jiter.known_bool(peek).unwrap();
black_box(i);
}
})
Expand Down Expand Up @@ -126,11 +126,11 @@ fn jiter_iter_ints_array(path: &str, bench: &mut Bencher) {
let json_data = black_box(json.as_bytes());
bench.iter(|| {
let mut jiter = Jiter::new(json_data, false);
let first_peak = jiter.next_array().unwrap().unwrap();
let i = jiter.known_int(first_peak).unwrap();
let first_peek = jiter.next_array().unwrap().unwrap();
let i = jiter.known_int(first_peek).unwrap();
black_box(i);
while let Some(peak) = jiter.array_step().unwrap() {
let i = jiter.known_int(peak).unwrap();
while let Some(peek) = jiter.array_step().unwrap() {
let i = jiter.known_int(peek).unwrap();
black_box(i);
}
})
Expand All @@ -141,11 +141,11 @@ fn jiter_iter_floats_array(path: &str, bench: &mut Bencher) {
let json_data = black_box(json.as_bytes());
bench.iter(|| {
let mut jiter = Jiter::new(json_data, false);
let first_peak = jiter.next_array().unwrap().unwrap();
let i = jiter.known_float(first_peak).unwrap();
let first_peek = jiter.next_array().unwrap().unwrap();
let i = jiter.known_float(first_peek).unwrap();
black_box(i);
while let Some(peak) = jiter.array_step().unwrap() {
let i = jiter.known_float(peak).unwrap();
while let Some(peek) = jiter.array_step().unwrap() {
let i = jiter.known_float(peek).unwrap();
black_box(i);
}
})
Expand Down
Loading
Loading