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
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/json-canon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
crossbeam-queue = "0.3.8"
lazy_static = "1.4.0"
ryu-js = { version = "0.2.2", default-features = false }
serde = { version = "1.0.162", default-features = false }
serde_json = { version = "1.0.96", default-features = false, features = ["std", "float_roundtrip"] }
Expand Down
1 change: 1 addition & 0 deletions rust/json-canon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
//!

mod object;
mod pool;
mod ser;

pub use self::ser::{to_string, to_vec, to_writer};
41 changes: 32 additions & 9 deletions rust/json-canon/src/object.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use std::{
io::{self, sink, Error, ErrorKind, Write},
str::from_utf8_unchecked,
sync::Arc,
};

use lazy_static::lazy_static;
use serde_json::ser::{CompactFormatter, Formatter};

use crate::pool::{Clear, Pool, PoolObjectContainer};

lazy_static! {
static ref POOL: Arc<Pool<ObjectEntry>> = Arc::new(Pool::with_capacity(256));
}

#[derive(Clone, Debug)]
pub(crate) struct ObjectEntry {
key: Vec<u8>,
Expand All @@ -13,15 +21,30 @@ pub(crate) struct ObjectEntry {
is_key_done: bool,
}

impl ObjectEntry {
pub(crate) fn new() -> Self {
impl Clear for ObjectEntry {
fn clear(&mut self) {
self.key.clear();
self.key_bytes.clear();
self.value.clear();
self.is_key_done = false;
}
}

impl Default for ObjectEntry {
fn default() -> Self {
Self {
key: Vec::new(),
key_bytes: Vec::new(),
value: Vec::new(),
key: Vec::with_capacity(8),
key_bytes: Vec::with_capacity(8),
value: Vec::with_capacity(16),
is_key_done: false,
}
}
}

impl ObjectEntry {
pub(crate) fn new() -> PoolObjectContainer<ObjectEntry> {
Pool::create(POOL.clone())
}

#[inline]
pub(crate) fn end_key(&mut self) {
Expand Down Expand Up @@ -84,9 +107,9 @@ impl ObjectEntry {
}
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub(crate) struct Object {
entries: Vec<ObjectEntry>,
entries: Vec<PoolObjectContainer<ObjectEntry>>,
}

impl Object {
Expand All @@ -96,7 +119,7 @@ impl Object {
}
}

pub(crate) fn current_entry(&mut self) -> io::Result<&mut ObjectEntry> {
pub(crate) fn current_entry(&mut self) -> io::Result<&mut PoolObjectContainer<ObjectEntry>> {
self.entries.last_mut().ok_or_else(|| {
Error::new(
ErrorKind::InvalidData,
Expand Down Expand Up @@ -164,7 +187,7 @@ impl Object {
}
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub(crate) struct ObjectStack {
objects: Vec<Object>,
}
Expand Down
105 changes: 105 additions & 0 deletions rust/json-canon/src/pool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use std::{
fmt::Debug,
mem::ManuallyDrop,
ops::{Deref, DerefMut},
ptr,
sync::Arc,
};

use crossbeam_queue::ArrayQueue;

pub trait Clear {
fn clear(&mut self);
}

#[derive(Debug)]
pub struct Pool<T> {
pub(crate) values: ArrayQueue<T>,
pub(crate) max_size: usize,
}

impl<T> Pool<T>
where
T: Clear,
{
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Self {
values: ArrayQueue::new(capacity),
max_size: capacity,
}
}

#[inline]
pub fn create(self: Arc<Self>) -> PoolObjectContainer<T>
where
T: Clear + Default,
{
let val = self.values.pop().unwrap_or_else(|| Default::default());
PoolObjectContainer::new(val, self)
}

#[inline]
pub fn max_size(&self) -> usize {
self.max_size
}

#[inline]
pub fn len(&self) -> usize {
self.values.len()
}

#[inline]
pub fn push(&self, value: T) -> Result<(), T> {
self.values.push(value)
}
}

#[derive(Debug)]
pub struct PoolObjectContainer<T: Clear> {
pool: Arc<Pool<T>>,
inner: ManuallyDrop<T>,
}

impl<T: Clear> PoolObjectContainer<T> {
#[inline]
fn new(val: T, pool: Arc<Pool<T>>) -> Self {
Self {
pool,
inner: ManuallyDrop::new(val),
}
}
}

impl<T: Clear> Deref for PoolObjectContainer<T> {
type Target = T;

#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl<T: Clear> DerefMut for PoolObjectContainer<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}

impl<T: Clear> Drop for PoolObjectContainer<T> {
fn drop(&mut self) {
let val = unsafe { ptr::read(&self.inner) };
let mut val = ManuallyDrop::into_inner(val);

let pool = &self.pool;
if pool.len() >= pool.max_size() {
drop(val);
} else {
val.clear();
if let Err(val) = pool.push(val) {
drop(val);
}
}
}
}
2 changes: 1 addition & 1 deletion rust/json-canon/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static MAX_SAFE_INTEGER_I64: i64 = 9_007_199_254_740_991;
static MAX_SAFE_INTEGER_U128: u128 = 9_007_199_254_740_991;
static MAX_SAFE_INTEGER_I128: i128 = 9_007_199_254_740_991;

#[derive(Clone, Debug)]
#[derive(Debug)]
#[repr(transparent)]
pub struct CanonicalFormatter {
stack: ObjectStack,
Expand Down