Skip to content
Open
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
5 changes: 5 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
unstable_features = true

use_small_heuristics = "max"
imports_granularity = "Module"
group_imports = "StdExternalCrate"
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ readme = "README.md"
default-run = "ojc"

[dependencies]
packed_simd = { version = "0.3.3", optional = true }

[dev-dependencies]
snap = "1.0.0" # for the lib.rs example

[features]
default = []
nightly = ["packed_simd"]
nightly = []
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ sys 0m12.628s

### `ojc` with SIMD

How many times does it takes to `ojc` already? 56s, that can't be true, we are in 2020...
How many times does it takes to `ojc` already? 56s, that can't be true, we are in 2022...
What about enabling some SIMD optimizations? Compile the binary with the `nightly` feature and here we go!

```bash
Expand Down
1 change: 1 addition & 0 deletions src/bin/ojc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io;

use oxidized_json_checker::JsonChecker;

fn fmain() -> io::Result<()> {
Expand Down
4 changes: 3 additions & 1 deletion src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Class {
CPlus, // +
CMinus, // -
CPoint, // .
CZero , // 0
CZero, // 0
CDigit, // 123456789
CLowA, // a
CLowB, // b
Expand All @@ -43,6 +43,7 @@ pub enum Class {
/// This array maps the 128 ASCII characters into character classes.
/// The remaining Unicode characters should be mapped to C_ETC.
/// Non-whitespace control characters are errors.
#[rustfmt::skip]
pub const ASCII_CLASS: [Class; 128] = [
___, ___, ___, ___, ___, ___, ___, ___,
___, CWhite, CWhite, ___, ___, CWhite, ___, ___,
Expand Down Expand Up @@ -111,6 +112,7 @@ pub enum State {
}

// Number of states by number of classes
#[rustfmt::skip]
pub const STATE_TRANSITION_TABLE: [[State; 31]; 31] = [
/*
The state transition table takes the current state and the current symbol,
Expand Down
87 changes: 45 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@
//! ```
//!

#![cfg_attr(feature = "nightly", feature(portable_simd))]

use std::{fmt, io};
use crate::internals::{State, Class, Mode};
use crate::internals::{STATE_TRANSITION_TABLE, ASCII_CLASS};

use crate::internals::{Class, Mode, State, ASCII_CLASS, STATE_TRANSITION_TABLE};

mod internals;
#[cfg(test)]
mod tests;
mod internals;

/// The error type returned by the `JsonChecker` type.
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -234,15 +236,15 @@ impl<R> JsonChecker<R> {
#[inline]
#[cfg(feature = "nightly")]
fn next_bytes(&mut self, bytes: &[u8]) -> Result<(), Error> {
use packed_simd::u8x8;
use core::simd::u8x8;

// TODO use chunks_exact instead?
// By using u8x8 instead of u8x16 we lost 2s on 16s but
// we are less prone to find state change requirements.
for chunk in bytes.chunks(u8x8::lanes()) {
if chunk.len() == u8x8::lanes() && self.state == State::St {
for chunk in bytes.chunks(u8x8::LANES) {
if chunk.len() == u8x8::LANES && self.state == State::St {
// Load the bytes into a SIMD type
let bytes = u8x8::from_slice_unaligned(chunk);
let bytes = u8x8::from_slice(chunk);

// According to the state STATE_TRANSITION_TABLE we are in the `St` state
// and *none of those bytes* are in the `CWhite`, `CQuote` or `CBacks` ascci class
Expand All @@ -258,18 +260,17 @@ impl<R> JsonChecker<R> {
// We first compare with quotes because this is the most
// common character we can encounter in valid JSON strings
// and this way we are able to skip other comparisons faster
if bytes.eq(cquotes).any() ||
bytes.eq(cbacks).any() ||
bytes.eq(cwhites1).any() ||
bytes.eq(cwhites2).any() ||
bytes.eq(cwhites3).any()
if bytes.lanes_eq(cquotes).any()
|| bytes.lanes_eq(cbacks).any()
|| bytes.lanes_eq(cwhites1).any()
|| bytes.lanes_eq(cwhites2).any()
|| bytes.lanes_eq(cwhites3).any()
{
chunk.iter().try_for_each(|b| self.next_byte(*b))?;
}

// Now that we checked that these bytes will not change
// the state we can continue to the next chunk and ignore them

} else {
chunk.iter().try_for_each(|b| self.next_byte(*b))?;
}
Expand All @@ -293,11 +294,8 @@ impl<R> JsonChecker<R> {
// We can potentially use try_blocks in the future.
fn internal_next_byte<R>(jc: &mut JsonChecker<R>, next_byte: u8) -> Result<(), Error> {
// Determine the character's class.
let next_class = if next_byte >= 128 {
Class::CEtc
} else {
ASCII_CLASS[next_byte as usize]
};
let next_class =
if next_byte >= 128 { Class::CEtc } else { ASCII_CLASS[next_byte as usize] };

if next_class == Class::Invalid {
return Err(Error::InvalidCharacter);
Expand All @@ -321,55 +319,61 @@ impl<R> JsonChecker<R> {
}

match next_state {
State::Wec => { // Empty }
State::Wec => {
// Empty }
if !jc.pop(Mode::Key) {
return Err(Error::EmptyCurlyBraces);
}
jc.state = State::Ok;
},
State::Wcu => { // }
}
State::Wcu => {
// }
if !jc.pop(Mode::Object) {
return Err(Error::OrphanCurlyBrace);
}
jc.state = State::Ok;
},
State::Ws => { // ]
}
State::Ws => {
// ]
if !jc.pop(Mode::Array) {
return Err(Error::OrphanSquareBrace);
}
jc.state = State::Ok;
},
State::Woc => { // {
}
State::Woc => {
// {
if !jc.push(Mode::Key) {
return Err(Error::MaxDepthReached);
}
jc.state = State::Ob;
},
State::Wos => { // [
}
State::Wos => {
// [
if !jc.push(Mode::Array) {
return Err(Error::MaxDepthReached);
}
jc.state = State::Ar;
}
State::Wq => { // "
State::Wq => {
// "
match jc.stack.last() {
Some(Mode::Done) => {
if !jc.push(Mode::String) {
return Err(Error::MaxDepthReached);
}
jc.state = State::St;
},
}
Some(Mode::String) => {
jc.pop(Mode::String);
jc.state = State::Ok;
},
}
Some(Mode::Key) => jc.state = State::Co,
Some(Mode::Array) |
Some(Mode::Object) => jc.state = State::Ok,
Some(Mode::Array) | Some(Mode::Object) => jc.state = State::Ok,
_ => return Err(Error::InvalidQuote),
}
},
State::Wcm => { // ,
}
State::Wcm => {
// ,
match jc.stack.last() {
Some(Mode::Object) => {
// A comma causes a flip from object mode to key mode.
Expand All @@ -381,17 +385,16 @@ impl<R> JsonChecker<R> {
Some(Mode::Array) => jc.state = State::Va,
_ => return Err(Error::InvalidComma),
}
},
State::Wcl => { // :
}
State::Wcl => {
// :
// A colon causes a flip from key mode to object mode.
if !jc.pop(Mode::Key) || !jc.push(Mode::Object) {
return Err(Error::InvalidColon);
}
jc.state = State::Va;
},
State::Invalid => {
return Err(Error::InvalidState)
},
}
State::Invalid => return Err(Error::InvalidState),

// Or change the state.
state => jc.state = state,
Expand Down Expand Up @@ -429,7 +432,7 @@ impl<R> JsonChecker<R> {

if is_state_valid && self.pop(Mode::Done) {
let outer_type = self.outer_type.expect("BUG: the outer type must have been guessed");
return Ok((self.reader, outer_type))
return Ok((self.reader, outer_type));
}

// We do not need to catch this error to *fuse* the checker because this method
Expand Down Expand Up @@ -467,7 +470,7 @@ impl<R: io::Read> io::Read for JsonChecker<R> {
// type instead we use the IncompleteElement error.
self.error = Some(Error::IncompleteElement);
return Err(error);
},
}
Ok(len) => len,
};

Expand Down
15 changes: 11 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Read;

use crate::*;

fn parse(text: &str) -> io::Result<JsonType> {
Expand Down Expand Up @@ -246,7 +247,8 @@ fn pass_single_null() {

#[test]
fn pass_1() {
let outer_type = parse(r##"
let outer_type = parse(
r##"

[
"JSON Test Pattern pass1",
Expand Down Expand Up @@ -307,7 +309,9 @@ fn pass_1() {
1e00,2e+00,2e-00
,"rosebud"]

"##).unwrap();
"##,
)
.unwrap();

assert_eq!(outer_type, JsonType::Array);
}
Expand All @@ -321,7 +325,8 @@ fn pass_2() {

#[test]
fn pass_3() {
let outer_type = parse(r#"
let outer_type = parse(
r#"

{
"JSON Test Pattern pass3": {
Expand All @@ -330,7 +335,9 @@ fn pass_3() {
}
}

"#).unwrap();
"#,
)
.unwrap();

assert_eq!(outer_type, JsonType::Object);
}