Skip to content

Commit

Permalink
script, lint misc
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Dec 5, 2021
1 parent 74da2ff commit 8f72579
Show file tree
Hide file tree
Showing 21 changed files with 105 additions and 88 deletions.
10 changes: 7 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
-r bench/requirements.txt
-r integration/requirements.txt
-r test/requirements.txt
autoflake
black
isort
maturin
mypy
-r test/requirements.txt
-r bench/requirements.txt
-r integration/requirements.txt
types-python-dateutil
types-pytz
types-simplejson
types-ujson
File renamed without changes.
2 changes: 1 addition & 1 deletion graph → script/graph
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import json
import os
import sys

from tabulate import tabulate
import matplotlib.pyplot as plt
from tabulate import tabulate

LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json")

Expand Down
7 changes: 5 additions & 2 deletions lint → script/lint
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

set -eou pipefail

to_lint="./bench/*.py ./orjson.pyi ./test/*.py script/pydataclass script/pymem
script/pysort script/pynumpy script/pynonstr script/pycorrectness script/graph"

autoflake --in-place --recursive --remove-all-unused-imports --ignore-init-module-imports .
isort ./bench/*.py ./orjson.pyi ./test/*.py pydataclass pymem pysort pynumpy pynonstr pycorrectness
black ./bench/*.py ./orjson.pyi ./test/*.py pydataclass pymem pysort pynumpy pynonstr pycorrectness
isort ${to_lint}
black ${to_lint}
mypy --ignore-missing-imports ./bench/*.py ./orjson.pyi ./test/*.py
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 29 additions & 23 deletions pyindent → script/pyindent
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
import io
import json
import lzma
import sys
import os
import sys
from pathlib import Path
from timeit import timeit

import orjson
import rapidjson
import simplejson
import ujson
import rapidjson
from tabulate import tabulate

import orjson

os.sched_setaffinity(os.getpid(), {0, 1})


Expand All @@ -29,6 +30,7 @@ def read_fixture_obj(filename):
contents = path.read_bytes()
return orjson.loads(contents)


filename = sys.argv[1] if len(sys.argv) >= 1 else ""

data = read_fixture_obj(f"{filename}.json.xz")
Expand All @@ -43,61 +45,65 @@ output_in_kib_pretty = len(orjson.dumps(data, option=orjson.OPT_INDENT_2)) / 102
# minimum 2s runtime for orjson compact
ITERATIONS = int(2 / (timeit(lambda: orjson.dumps(data), number=20) / 20))

print(f"{output_in_kib_compact:,.0f}KiB compact, {output_in_kib_pretty:,.0f}KiB pretty, {ITERATIONS} iterations")
print(
f"{output_in_kib_compact:,.0f}KiB compact, {output_in_kib_pretty:,.0f}KiB pretty, {ITERATIONS} iterations"
)


def per_iter_latency(val):
if val is None:
return None
return (val * 1000) / ITERATIONS


def test_correctness(serialized):
return orjson.loads(serialized) == data


table = []
for lib_name in LIBRARIES:
print(f"{lib_name}...")
if lib_name == "json":
time_compact = timeit(
lambda: json.dumps(data).encode("utf-8"), number=ITERATIONS,
lambda: json.dumps(data).encode("utf-8"),
number=ITERATIONS,
)
time_pretty = timeit(
lambda: json.dumps(data, indent=2).encode("utf-8"), number=ITERATIONS,
)
correct = test_correctness(
json.dumps(data, indent=2).encode("utf-8")
lambda: json.dumps(data, indent=2).encode("utf-8"),
number=ITERATIONS,
)
correct = test_correctness(json.dumps(data, indent=2).encode("utf-8"))
elif lib_name == "simplejson":
time_compact = timeit(
lambda: simplejson.dumps(data).encode("utf-8"), number=ITERATIONS,
lambda: simplejson.dumps(data).encode("utf-8"),
number=ITERATIONS,
)
time_pretty = timeit(
lambda: simplejson.dumps(data, indent=2).encode("utf-8"), number=ITERATIONS,
)
correct = test_correctness(
simplejson.dumps(data, indent=2).encode("utf-8")
lambda: simplejson.dumps(data, indent=2).encode("utf-8"),
number=ITERATIONS,
)
correct = test_correctness(simplejson.dumps(data, indent=2).encode("utf-8"))
elif lib_name == "ujson":
time_compact = timeit(
lambda: ujson.dumps(data).encode("utf-8"), number=ITERATIONS,
lambda: ujson.dumps(data).encode("utf-8"),
number=ITERATIONS,
)
time_pretty = timeit(
lambda: ujson.dumps(data, indent=2).encode("utf-8"), number=ITERATIONS,
)
correct = test_correctness(
ujson.dumps(data, indent=2).encode("utf-8")
lambda: ujson.dumps(data, indent=2).encode("utf-8"),
number=ITERATIONS,
)
correct = test_correctness(ujson.dumps(data, indent=2).encode("utf-8"))
elif lib_name == "rapidjson":
time_compact = timeit(lambda: rapidjson.dumps(data), number=ITERATIONS)
time_pretty = None
correct = False
elif lib_name == "orjson":
time_compact = timeit(lambda: orjson.dumps(data), number=ITERATIONS)
time_pretty = timeit(
lambda: orjson.dumps(data, None, orjson.OPT_INDENT_2), number=ITERATIONS,
)
correct = test_correctness(
orjson.dumps(data, None, orjson.OPT_INDENT_2)
lambda: orjson.dumps(data, None, orjson.OPT_INDENT_2),
number=ITERATIONS,
)
correct = test_correctness(orjson.dumps(data, None, orjson.OPT_INDENT_2))
orjson_time_pretty = per_iter_latency(time_pretty)
else:
raise NotImplementedError
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/deserialize/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<'de> Visitor<'de> for JsonValue {
}
Ok(nonnull!(ptr))
}
Err(err) => std::result::Result::Err(err),
Err(err) => Result::Err(err),
}
}

Expand Down
82 changes: 41 additions & 41 deletions src/serialize/numpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl<'p> Serialize for NumpyArray {

#[repr(transparent)]
struct DataTypeF32 {
pub obj: f32,
obj: f32,
}

impl<'p> Serialize for DataTypeF32 {
Expand All @@ -331,7 +331,7 @@ impl<'p> Serialize for DataTypeF32 {

#[repr(transparent)]
pub struct DataTypeF64 {
pub obj: f64,
obj: f64,
}

impl<'p> Serialize for DataTypeF64 {
Expand All @@ -345,7 +345,7 @@ impl<'p> Serialize for DataTypeF64 {

#[repr(transparent)]
pub struct DataTypeI8 {
pub obj: i8,
obj: i8,
}

impl<'p> Serialize for DataTypeI8 {
Expand All @@ -359,7 +359,7 @@ impl<'p> Serialize for DataTypeI8 {

#[repr(transparent)]
pub struct DataTypeI32 {
pub obj: i32,
obj: i32,
}

impl<'p> Serialize for DataTypeI32 {
Expand All @@ -373,7 +373,7 @@ impl<'p> Serialize for DataTypeI32 {

#[repr(transparent)]
pub struct DataTypeI64 {
pub obj: i64,
obj: i64,
}

impl<'p> Serialize for DataTypeI64 {
Expand All @@ -387,7 +387,7 @@ impl<'p> Serialize for DataTypeI64 {

#[repr(transparent)]
pub struct DataTypeU8 {
pub obj: u8,
obj: u8,
}

impl<'p> Serialize for DataTypeU8 {
Expand All @@ -401,7 +401,7 @@ impl<'p> Serialize for DataTypeU8 {

#[repr(transparent)]
pub struct DataTypeU32 {
pub obj: u32,
obj: u32,
}

impl<'p> Serialize for DataTypeU32 {
Expand All @@ -415,7 +415,7 @@ impl<'p> Serialize for DataTypeU32 {

#[repr(transparent)]
pub struct DataTypeU64 {
pub obj: u64,
obj: u64,
}

impl<'p> Serialize for DataTypeU64 {
Expand All @@ -429,7 +429,7 @@ impl<'p> Serialize for DataTypeU64 {

#[repr(transparent)]
pub struct DataTypeBOOL {
pub obj: u8,
obj: u8,
}

impl<'p> Serialize for DataTypeBOOL {
Expand All @@ -442,8 +442,8 @@ impl<'p> Serialize for DataTypeBOOL {
}

pub struct NumpyScalar {
pub ptr: *mut pyo3::ffi::PyObject,
pub opts: Opt,
ptr: *mut pyo3::ffi::PyObject,
opts: Opt,
}

impl NumpyScalar {
Expand Down Expand Up @@ -495,9 +495,9 @@ impl<'p> Serialize for NumpyScalar {

#[repr(C)]
pub struct NumpyInt8 {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: i8,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: i8,
}

impl<'p> Serialize for NumpyInt8 {
Expand All @@ -511,9 +511,9 @@ impl<'p> Serialize for NumpyInt8 {

#[repr(C)]
pub struct NumpyInt32 {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: i32,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: i32,
}

impl<'p> Serialize for NumpyInt32 {
Expand All @@ -527,9 +527,9 @@ impl<'p> Serialize for NumpyInt32 {

#[repr(C)]
pub struct NumpyInt64 {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: i64,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: i64,
}

impl<'p> Serialize for NumpyInt64 {
Expand All @@ -543,9 +543,9 @@ impl<'p> Serialize for NumpyInt64 {

#[repr(C)]
pub struct NumpyUint8 {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: u8,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: u8,
}

impl<'p> Serialize for NumpyUint8 {
Expand All @@ -559,9 +559,9 @@ impl<'p> Serialize for NumpyUint8 {

#[repr(C)]
pub struct NumpyUint32 {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: u32,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: u32,
}

impl<'p> Serialize for NumpyUint32 {
Expand All @@ -575,9 +575,9 @@ impl<'p> Serialize for NumpyUint32 {

#[repr(C)]
pub struct NumpyUint64 {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: u64,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: u64,
}

impl<'p> Serialize for NumpyUint64 {
Expand All @@ -591,9 +591,9 @@ impl<'p> Serialize for NumpyUint64 {

#[repr(C)]
pub struct NumpyFloat32 {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: f32,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: f32,
}

impl<'p> Serialize for NumpyFloat32 {
Expand All @@ -607,9 +607,9 @@ impl<'p> Serialize for NumpyFloat32 {

#[repr(C)]
pub struct NumpyFloat64 {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: f64,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: f64,
}

impl<'p> Serialize for NumpyFloat64 {
Expand All @@ -623,9 +623,9 @@ impl<'p> Serialize for NumpyFloat64 {

#[repr(C)]
pub struct NumpyBool {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: bool,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: bool,
}

impl<'p> Serialize for NumpyBool {
Expand Down Expand Up @@ -794,9 +794,9 @@ impl NumpyDatetimeUnit {

#[repr(C)]
pub struct NumpyDatetime64 {
pub ob_refcnt: Py_ssize_t,
pub ob_type: *mut PyTypeObject,
pub value: i64,
ob_refcnt: Py_ssize_t,
ob_type: *mut PyTypeObject,
value: i64,
}

macro_rules! forward_inner {
Expand Down
Loading

0 comments on commit 8f72579

Please sign in to comment.