Skip to content
Permalink
Browse files

Some cleanup

  • Loading branch information
Licenser committed Jan 28, 2020
1 parent 9fe3106 commit cb6ee053745cad40fbe531459ea7352cb69fe5ee
Showing with 27 additions and 20 deletions.
  1. +0 −1 Cargo.toml
  2. +25 −4 src/lib.rs
  3. +2 −15 src/stage2.rs
@@ -10,7 +10,6 @@ repository = "https://github.com/simd-lite/simdjson-rs"

[dependencies]
halfbrown = "0.1"
page_size = "0.4"
itoa = "0.4"
ryu = "1"

@@ -378,11 +378,29 @@ impl<'de> Deserializer<'de> {
fn raw_error(idx: usize, c: char, error: ErrorType) -> Error {
Error::new(idx, c, error)
}

// By convention, `Deserializer` constructors are named like `from_xyz`.
// That way basic use cases are satisfied by something like
// `serde_json::from_str(...)` while advanced use cases that require a
// deserializer can make one with `serde_json::Deserializer::from_str(...)`.
pub fn from_slice(input: &'de mut [u8]) -> Result<Self> {
let len = input.len();

let mut string_buffer: Vec<u8> = Vec::with_capacity(len + SIMDJSON_PADDING);
unsafe {
string_buffer.set_len(len + SIMDJSON_PADDING);
};

Deserializer::from_slice_with_buffer(input, &mut string_buffer)
}

// By convention, `Deserializer` constructors are named like `from_xyz`.
// That way basic use cases are satisfied by something like
// `serde_json::from_str(...)` while advanced use cases that require a
// deserializer can make one with `serde_json::Deserializer::from_str(...)`.
// this takes an additional buffer to be (re) used for temporary string copying

pub fn from_slice_with_buffer(input: &'de mut [u8], string_buffer: &mut [u8]) -> Result<Self> {
// We have to pick an initial size of the structural indexes.
// 6 is a heuristic that seems to work well for the benchmark
// data and limit re-allocation frequency.
@@ -391,11 +409,13 @@ impl<'de> Deserializer<'de> {

// let buf_start: usize = input.as_ptr() as *const () as usize;
// let needs_relocation = (buf_start + input.len()) % page_size::get() < SIMDJSON_PADDING;

let mut buffer: Vec<u8> = Vec::with_capacity(len + SIMDJSON_PADDING * 2);
let align = buffer.as_slice().as_ptr().align_offset(SIMDJSON_PADDING);

let align = buffer
.as_slice()
.as_ptr()
.align_offset(SIMDJSON_PADDING / 2);
unsafe {
buffer.set_len(len + align + 1);
buffer
.as_mut_slice()
.get_unchecked_mut(align..align + len)
@@ -414,7 +434,8 @@ impl<'de> Deserializer<'de> {
}
};

let tape = Deserializer::build_tape(input, &buffer[align..], &structural_indexes)?;
let tape =
Deserializer::build_tape(input, &buffer[align..], string_buffer, &structural_indexes)?;

Ok(Deserializer { tape, idx: 0 })
}
@@ -1,14 +1,5 @@
#![allow(dead_code)]
#[cfg(target_feature = "avx2")]
use crate::avx2::stage1::SIMDJSON_PADDING;
use crate::charutils::*;
#[cfg(target_feature = "neon")]
use crate::neon::stage1::SIMDJSON_PADDING;
#[cfg(all(
any(target_arch = "x86", target_arch = "x86_64"),
not(target_feature = "avx2")
))]
use crate::sse42::stage1::SIMDJSON_PADDING;
use crate::value::tape::*;
use crate::{Deserializer, Error, ErrorType, Result};

@@ -122,17 +113,16 @@ impl<'de> Deserializer<'de> {
pub(crate) fn build_tape(
input: &'de mut [u8],
input2: &[u8],
buffer: &mut [u8],
structural_indexes: &[u32],
) -> Result<Vec<Node<'de>>> {
// While a valid json can have at max len/2 (`[[[]]]`)elements that are relevant
// a invalid json might exceed this `[[[[[[` and we need to pretect against that.
let mut res: Vec<Node<'de>> = Vec::with_capacity(structural_indexes.len());
let mut stack = Vec::with_capacity(structural_indexes.len());
let mut buffer: Vec<u8> = Vec::with_capacity(input.len() + SIMDJSON_PADDING * 2);
unsafe {
stack.set_len(structural_indexes.len());
res.set_len(structural_indexes.len());
buffer.set_len(input.len() + SIMDJSON_PADDING * 2);
}

let mut depth: usize = 0;
@@ -207,10 +197,7 @@ impl<'de> Deserializer<'de> {
macro_rules! insert_str {
() => {
insert_res!(Node::String(s2try!(Self::parse_str_(
input,
&input2,
&mut buffer,
idx
input, &input2, buffer, idx
))));
};
}

0 comments on commit cb6ee05

Please sign in to comment.
You can’t perform that action at this time.