Skip to content

Commit

Permalink
Replace peak with more efficient peek (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt authored Dec 4, 2023
1 parent 36b2354 commit c7deb6d
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 273 deletions.
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

0 comments on commit c7deb6d

Please sign in to comment.