1
0
Fork 0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-05-25 11:06:19 +02:00

Add simd libraries

This commit is contained in:
joncrall 2022-05-29 18:56:24 -04:00
parent 2b2aedb89f
commit b0bc25ab3c
No known key found for this signature in database
GPG Key ID: BE04D092BDD81C0D
3 changed files with 47 additions and 16 deletions

View File

@ -72,9 +72,9 @@ def main(cmdline=True, **kwargs):
print(f"result_fpath = {result_fpath!r}")
result_fpaths = [result_fpath]
agg = config["mode"] not in {"single"}
if agg:
result_fpaths = list(dpath.glob("benchmarks*.json"))
# agg = config["mode"] not in {"single"}
# if agg:
# result_fpaths = list(dpath.glob("benchmarks*.json"))
analyze = config["mode"] in {"all", "single", "analyze"}
if analyze:

View File

@ -9,6 +9,8 @@ KNOWN_LIBRARIES = [
{"modname": "simplejson", "distname": "simplejson"},
{"modname": "json", "distname": "<stdlib>"},
{"modname": "simdjson", "distname": "pysimdjson"},
{"modname": "cysimdjson", "distname": "cysimdjson"},
{"modname": "libpy_simdjson", "distname": "libpy-simdjson"},
]
KNOWN_MODNAMES = [info["modname"] for info in KNOWN_LIBRARIES]
@ -29,6 +31,29 @@ KNOWN_MODNAMES = [info["modname"] for info in KNOWN_LIBRARIES]
# # pkg_resources.get_distribution('pysimdjson')
class Compatability:
"""
Expose a common API for all tested implmentations
"""
@staticmethod
def lut_dumps(module):
if module.__name__ == 'cysimdjson':
return None
elif module.__name__ == 'pysimdjson':
return None
else:
return getattr(module, 'dumps', None)
@staticmethod
def lut_loads(module):
if module.__name__ == 'cysimdjson':
parser = module.JSONParser()
return parser.loads
else:
return getattr(module, 'loads', None)
def available_json_impls():
"""
Return a dictionary of information about each json implementation
@ -39,7 +64,7 @@ def available_json_impls():
>>> print('json_impls = {}'.format(ub.repr2(json_impls, nl=1)))
"""
import importlib
import pkg_resources
known_libinfo = KNOWN_LIBRARIES
json_impls = {}
for libinfo in known_libinfo:
@ -50,8 +75,6 @@ def available_json_impls():
except ImportError:
pass
else:
import pkg_resources
mod_version = getattr(module, "__version__", None)
if distname == "<stdlib>":
pkg_version = mod_version
@ -60,10 +83,15 @@ def available_json_impls():
if mod_version is not None:
assert mod_version == pkg_version
version = pkg_version
json_impls[modname] = {
dumps = Compatability.lut_dumps(module)
loads = Compatability.lut_loads(module)
impl_info = {
"module": module,
"modname": modname,
"distname": distname,
"version": version,
"dumps": dumps,
"loads": loads,
}
json_impls[modname] = impl_info
return json_impls

View File

@ -69,7 +69,7 @@ def benchmark_json():
# 'Dict of List[Dict[str, int]]',
# 'Complex object'
],
"size": [1, 2, 4, 8, 16, 32, 128, 256, 512, 1024, 2048, 4096, 8192, 12288],
"size": [1, 2, 4, 8, 16, 32, 128, 256, 512, 1024, 2048, 4096, 8192],
}
predefined_basis = {
"input": ["Complex object"],
@ -93,8 +93,10 @@ def benchmark_json():
)
def is_blocked(params):
if params["input"] == "Complex object" and params["impl"] == "orjson":
return True
if params["input"] == "Complex object":
# Some libraries can't handle the complex object
if params["impl"] in {"orjson", "libpy_simdjson"}:
return True
# For each variation of your experiment, create a row.
for params in benchmark.iter_params():
@ -104,14 +106,15 @@ def benchmark_json():
# method here.
impl_info = json_impls[params["impl"]]
params["impl_version"] = impl_info["version"]
module = impl_info["module"]
method = impl_info[params["func"]]
if method is None:
# Not all libraries implement all methods
continue
py_data = data_lut[params["input"]](params["size"])
if params["func"] == "dumps":
method = module.dumps
data = data_lut[params["input"]](params["size"])
data = py_data
elif params["func"] == "loads":
method = module.loads
to_encode = data_lut[params["input"]](params["size"])
data = json.dumps(to_encode)
data = json.dumps(py_data)
# Timerit will run some user-specified number of loops.
# and compute time stats with similar methodology to timeit
for timer in benchmark.measure():