Skip to content

Commit

Permalink
Documentation update, compare against stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Nov 23, 2024
1 parent f53ec9f commit ded52a7
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 474 deletions.
359 changes: 107 additions & 252 deletions README.md

Large diffs are not rendered by default.

22 changes: 0 additions & 22 deletions bench/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,17 @@
from json import dumps as _json_dumps
from json import loads as json_loads

from rapidjson import dumps as _rapidjson_dumps
from rapidjson import loads as rapidjson_loads
from simplejson import dumps as _simplejson_dumps
from simplejson import loads as simplejson_loads
from ujson import dumps as _ujson_dumps
from ujson import loads as ujson_loads

from orjson import dumps as orjson_dumps
from orjson import loads as orjson_loads


def ujson_dumps(obj):
return _ujson_dumps(obj).encode("utf-8")


def rapidjson_dumps(obj):
return _rapidjson_dumps(obj).encode("utf-8")


def json_dumps(obj):
return _json_dumps(obj).encode("utf-8")


def simplejson_dumps(obj):
return _simplejson_dumps(obj).encode("utf-8")


libraries = {
"orjson": (orjson_dumps, orjson_loads),
"ujson": (ujson_dumps, ujson_loads),
"json": (json_dumps, json_loads),
"rapidjson": (rapidjson_dumps, rapidjson_loads),
"simplejson": (simplejson_dumps, simplejson_loads),
}


Expand Down
5 changes: 1 addition & 4 deletions bench/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
memory-profiler
memory-profiler; python_version<"3.13"
pandas; python_version<"3.13"
pytest-benchmark
pytest-random-order
python-rapidjson
seaborn; python_version<"3.13"
simplejson
tabulate
ujson
6 changes: 0 additions & 6 deletions bench/run_mem
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ if lib_name == "json":
from json import dumps, loads
elif lib_name == "orjson":
from orjson import dumps, loads
elif lib_name == "rapidjson":
from rapidjson import dumps, loads
elif lib_name == "simplejson":
from simplejson import dumps, loads
elif lib_name == "ujson":
from ujson import dumps, loads
else:
raise NotImplementedError

Expand Down
Binary file modified doc/deserialization.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/serialization.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion integration/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
flask;sys_platform!="win"
gunicorn;sys_platform!="win"
httpx==0.24.1;sys_platform!="win"
httpx==0.27.2;sys_platform!="win"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ known-first-party = ["orjson"]
python_version = "3.8"

[[tool.mypy.overrides]]
module = ["dateutil", "pytz", "simplejson", "ujson"]
module = ["dateutil", "pytz"]
ignore_missing_imports = true
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
-r test/requirements.txt
maturin
mypy==1.13.0
ruff==0.7.1
ruff==0.8.0
2 changes: 1 addition & 1 deletion script/graph
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from tabulate import tabulate

import orjson

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


def aggregate():
Expand Down
2 changes: 1 addition & 1 deletion script/lint
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -eou pipefail

to_lint="./bench/*.py ./pysrc/orjson/__init__.pyi ./test/*.py script/pydataclass script/pymem
to_lint="./bench/*.py ./pysrc/orjson/__init__.pyi ./test/*.py script/pydataclass
script/pysort script/pynumpy script/pynonstr script/pycorrectness script/graph integration/init
integration/wsgi.py integration/typestubs.py integration/thread"

Expand Down
8 changes: 1 addition & 7 deletions script/pycorrectness
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@ import lzma
import os
from pathlib import Path

import rapidjson
import simplejson
import ujson
from tabulate import tabulate

import orjson

dirname = os.path.join(os.path.dirname(__file__), "..", "data")

LIBRARIES = ["orjson", "ujson", "rapidjson", "simplejson", "json"]
LIBRARIES = ["orjson", "json"]


LIBRARY_FUNC_MAP = {
"orjson": orjson.loads,
"ujson": ujson.loads,
"rapidjson": rapidjson.loads,
"simplejson": simplejson.loads,
"json": json.loads,
}

Expand Down
33 changes: 1 addition & 32 deletions script/pydataclass
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import os
from timeit import timeit
from typing import List

import rapidjson
import simplejson
import ujson
from tabulate import tabulate

import orjson
Expand Down Expand Up @@ -50,7 +47,7 @@ def default(__obj):

headers = ("Library", "dict (ms)", "dataclass (ms)", "vs. orjson")

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

ITERATIONS = 100

Expand All @@ -72,34 +69,6 @@ for lib_name in LIBRARIES:
lambda: json.dumps(objects_as_dataclass, default=default).encode("utf-8"),
number=ITERATIONS,
)
elif lib_name == "simplejson":
as_dict = timeit(
lambda: simplejson.dumps(objects_as_dict).encode("utf-8"),
number=ITERATIONS,
)
as_dataclass = timeit(
lambda: simplejson.dumps(objects_as_dataclass, default=default).encode(
"utf-8"
),
number=ITERATIONS,
)
elif lib_name == "ujson":
as_dict = timeit(
lambda: ujson.dumps(objects_as_dict).encode("utf-8"),
number=ITERATIONS,
)
as_dataclass = None
elif lib_name == "rapidjson":
as_dict = timeit(
lambda: rapidjson.dumps(objects_as_dict).encode("utf-8"),
number=ITERATIONS,
)
as_dataclass = timeit(
lambda: rapidjson.dumps(objects_as_dataclass, default=default).encode(
"utf-8"
),
number=ITERATIONS,
)
elif lib_name == "orjson":
as_dict = timeit(lambda: orjson.dumps(objects_as_dict), number=ITERATIONS)
as_dataclass = timeit(
Expand Down
29 changes: 1 addition & 28 deletions script/pyindent
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import sys
from pathlib import Path
from timeit import timeit

import rapidjson
import simplejson
import ujson
from tabulate import tabulate

import orjson
Expand All @@ -37,7 +34,7 @@ data = read_fixture_obj(f"{filename}.json.xz")

headers = ("Library", "compact (ms)", "pretty (ms)", "vs. orjson")

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

output_in_kib_compact = len(orjson.dumps(data)) / 1024
output_in_kib_pretty = len(orjson.dumps(data, option=orjson.OPT_INDENT_2)) / 1024
Expand Down Expand Up @@ -73,30 +70,6 @@ for lib_name in LIBRARIES:
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,
)
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"))
elif lib_name == "ujson":
time_compact = timeit(
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"))
elif lib_name == "rapidjson":
time_compact = timeit(lambda: rapidjson.dumps(data), number=ITERATIONS)
time_pretty = timeit(lambda: rapidjson.dumps(data, indent=2), number=ITERATIONS)
correct = test_correctness(rapidjson.dumps(data, indent=2))
elif lib_name == "orjson":
time_compact = timeit(lambda: orjson.dumps(data), number=ITERATIONS)
time_pretty = timeit(
Expand Down
34 changes: 0 additions & 34 deletions script/pymem

This file was deleted.

40 changes: 1 addition & 39 deletions script/pynonstr
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import random
from time import mktime
from timeit import timeit

import rapidjson
import simplejson
import ujson
from tabulate import tabulate

import orjson
Expand All @@ -33,7 +30,7 @@ data_as_str = orjson.loads(orjson.dumps(data_as_obj, option=orjson.OPT_NON_STR_K

headers = ("Library", "str keys (ms)", "int keys (ms)", "int keys sorted (ms)")

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

ITERATIONS = 500

Expand Down Expand Up @@ -69,41 +66,6 @@ for lib_name in LIBRARIES:
None # TypeError: '<' not supported between instances of 'str' and 'int'
)
correct = False
elif lib_name == "simplejson":
time_as_str = timeit(
lambda: simplejson.dumps(data_as_str).encode("utf-8"),
number=ITERATIONS,
)
time_as_obj = timeit(
lambda: simplejson.dumps(data_as_obj).encode("utf-8"),
number=ITERATIONS,
)
time_as_obj_sorted = timeit(
lambda: simplejson.dumps(data_as_obj, sort_keys=True).encode("utf-8"),
number=ITERATIONS,
)
correct = test_correctness(
simplejson.dumps(data_as_obj, sort_keys=True).encode("utf-8")
)
elif lib_name == "ujson":
time_as_str = timeit(
lambda: ujson.dumps(data_as_str).encode("utf-8"),
number=ITERATIONS,
)
time_as_obj = timeit(
lambda: ujson.dumps(data_as_obj).encode("utf-8"),
number=ITERATIONS,
)
time_as_obj_sorted = None # segfault
correct = False
elif lib_name == "rapidjson":
time_as_str = timeit(
lambda: rapidjson.dumps(data_as_str).encode("utf-8"),
number=ITERATIONS,
)
time_as_obj = None
time_as_obj_sorted = None
correct = False
elif lib_name == "orjson":
time_as_str = timeit(
lambda: orjson.dumps(data_as_str, None, orjson.OPT_NON_STR_KEYS),
Expand Down
15 changes: 1 addition & 14 deletions script/pynumpy
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ from timeit import timeit

import numpy
import psutil
import rapidjson
import simplejson
from memory_profiler import memory_usage
from tabulate import tabulate

Expand Down Expand Up @@ -71,7 +69,7 @@ def default(__obj):

headers = ("Library", "Latency (ms)", "RSS diff (MiB)", "vs. orjson")

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

ITERATIONS = 10

Expand All @@ -80,17 +78,6 @@ def orjson_dumps():
return orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY)


ujson_dumps = None


def rapidjson_dumps():
return rapidjson.dumps(array, default=default).encode("utf-8")


def simplejson_dumps():
return simplejson.dumps(array, default=default).encode("utf-8")


def json_dumps():
return json.dumps(array, default=default).encode("utf-8")

Expand Down
Loading

0 comments on commit ded52a7

Please sign in to comment.