diff options
| author | Theofilos Augoustis <theofilos.augoustis@gmail.com> | 2025-11-11 22:30:12 +0000 |
|---|---|---|
| committer | Theofilos Augoustis <theofilos.augoustis@gmail.com> | 2025-11-11 22:30:49 +0000 |
| commit | 140a8c654054a3d456190554175dc56e285cb819 (patch) | |
| tree | 4eec811c9b39d15028470e384e2eff74e3b5e02e | |
| parent | ab083cb653ee0fe74f792517805af9bd59ad691d (diff) | |
| download | focaccia-ta/json-dumps.tar.gz focaccia-ta/json-dumps.zip | |
Use orjson instead of regular json (providing 2x faster dumps and loads) ta/json-dumps
| -rw-r--r-- | pyproject.toml | 1 | ||||
| -rw-r--r-- | src/focaccia/parser.py | 19 | ||||
| -rw-r--r-- | uv.lock | 18 |
3 files changed, 30 insertions, 8 deletions
diff --git a/pyproject.toml b/pyproject.toml index 5cc1e29..ce85a55 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ authors = [ dependencies = [ "cffi", "miasm", + "orjson", "brotli", "pycapnp", "setuptools", diff --git a/src/focaccia/parser.py b/src/focaccia/parser.py index c157a36..5602be6 100644 --- a/src/focaccia/parser.py +++ b/src/focaccia/parser.py @@ -1,8 +1,8 @@ """Parsing of JSON files containing snapshot data.""" -import base64 -import json import re +import base64 +import orjson as json from typing import TextIO from .arch import supported_architectures, Arch @@ -22,7 +22,7 @@ def _get_or_throw(obj: dict, key: str): def parse_transformations(json_stream: TextIO) -> Trace[SymbolicTransform]: """Parse symbolic transformations from a text stream.""" - data = json.load(json_stream) + data = json.loads(json_stream.read()) env = TraceEnvironment.from_json(_get_or_throw(data, 'env')) strace = [SymbolicTransform.from_json(item) \ @@ -33,14 +33,15 @@ def parse_transformations(json_stream: TextIO) -> Trace[SymbolicTransform]: def serialize_transformations(transforms: Trace[SymbolicTransform], out_stream: TextIO): """Serialize symbolic transformations to a text stream.""" - json.dump({ + data = json.dumps({ 'env': transforms.env.to_json(), 'states': [t.to_json() for t in transforms], - }, out_stream, indent=4) + }, option=json.OPT_INDENT_2).decode() + out_stream.write(data) def parse_snapshots(json_stream: TextIO) -> Trace[ProgramState]: """Parse snapshots from our JSON format.""" - json_data = json.load(json_stream) + json_data = json.loads(json_stream.read()) arch = supported_architectures[_get_or_throw(json_data, 'architecture')] env = TraceEnvironment.from_json(_get_or_throw(json_data, 'env')) @@ -62,7 +63,8 @@ def parse_snapshots(json_stream: TextIO) -> Trace[ProgramState]: def serialize_snapshots(snapshots: Trace[ProgramState], out_stream: TextIO): """Serialize a list of snapshots to out JSON format.""" if not snapshots: - return json.dump({}, out_stream, indent = 4) + empty = json.dumps({}, option=json.OPT_INDENT_2).decode() + out_stream.write(empty) arch = snapshots[0].arch res = { @@ -81,7 +83,8 @@ def serialize_snapshots(snapshots: Trace[ProgramState], out_stream: TextIO): }) res['snapshots'].append({ 'registers': regs, 'memory': mem }) - json.dump(res, out_stream, indent=4) + data = json.dumps(res, option=json.OPT_INDENT_2).decode() + out_stream.write(data) def _make_unknown_env() -> TraceEnvironment: return TraceEnvironment('', [], False, [], '?') diff --git a/uv.lock b/uv.lock index a9ea1d1..dc00fbe 100644 --- a/uv.lock +++ b/uv.lock @@ -88,6 +88,7 @@ dependencies = [ { name = "cffi", marker = "(platform_machine == 'aarch64' and sys_platform == 'linux') or (platform_machine == 'x86_64' and sys_platform == 'linux')" }, { name = "cpuid", marker = "(platform_machine == 'aarch64' and sys_platform == 'linux') or (platform_machine == 'x86_64' and sys_platform == 'linux')" }, { name = "miasm", marker = "(platform_machine == 'aarch64' and sys_platform == 'linux') or (platform_machine == 'x86_64' and sys_platform == 'linux')" }, + { name = "orjson", marker = "(platform_machine == 'aarch64' and sys_platform == 'linux') or (platform_machine == 'x86_64' and sys_platform == 'linux')" }, { name = "pycapnp", marker = "(platform_machine == 'aarch64' and sys_platform == 'linux') or (platform_machine == 'x86_64' and sys_platform == 'linux')" }, { name = "setuptools", marker = "(platform_machine == 'aarch64' and sys_platform == 'linux') or (platform_machine == 'x86_64' and sys_platform == 'linux')" }, ] @@ -107,6 +108,7 @@ requires-dist = [ { name = "cffi" }, { name = "cpuid", git = "https://github.com/taugoust/cpuid.py.git?rev=master" }, { name = "miasm", directory = "miasm" }, + { name = "orjson" }, { name = "pycapnp" }, { name = "pyright", marker = "extra == 'dev'" }, { name = "pytest", marker = "extra == 'dev'" }, @@ -172,6 +174,22 @@ wheels = [ ] [[package]] +name = "orjson" +version = "3.11.4" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/c6/fe/ed708782d6709cc60eb4c2d8a361a440661f74134675c72990f2c48c785f/orjson-3.11.4.tar.gz", hash = "sha256:39485f4ab4c9b30a3943cfe99e1a213c4776fb69e8abd68f66b83d5a0b0fdc6d", size = 5945188, upload-time = "2025-10-24T15:50:38.027Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/4e/47/bf85dcf95f7a3a12bf223394a4f849430acd82633848d52def09fa3f46ad/orjson-3.11.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:600e0e9ca042878c7fdf189cf1b028fe2c1418cc9195f6cb9824eb6ed99cb938", size = 130137, upload-time = "2025-10-24T15:49:12.544Z" }, + { url = "https://files.pythonhosted.org/packages/b4/4d/a0cb31007f3ab6f1fd2a1b17057c7c349bc2baf8921a85c0180cc7be8011/orjson-3.11.4-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:7bbf9b333f1568ef5da42bc96e18bf30fd7f8d54e9ae066d711056add508e415", size = 129152, upload-time = "2025-10-24T15:49:13.754Z" }, + { url = "https://files.pythonhosted.org/packages/00/d4/9aee9e54f1809cec8ed5abd9bc31e8a9631d19460e3b8470145d25140106/orjson-3.11.4-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ad355e8308493f527d41154e9053b86a5be892b3b359a5c6d5d95cda23601cb2", size = 137519, upload-time = "2025-10-24T15:49:16.557Z" }, + { url = "https://files.pythonhosted.org/packages/db/ea/67bfdb5465d5679e8ae8d68c11753aaf4f47e3e7264bad66dc2f2249e643/orjson-3.11.4-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:c8a7517482667fb9f0ff1b2f16fe5829296ed7a655d04d68cd9711a4d8a4e708", size = 136749, upload-time = "2025-10-24T15:49:17.796Z" }, + { url = "https://files.pythonhosted.org/packages/01/7e/62517dddcfce6d53a39543cd74d0dccfcbdf53967017c58af68822100272/orjson-3.11.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:97eb5942c7395a171cbfecc4ef6701fc3c403e762194683772df4c54cfbb2210", size = 136325, upload-time = "2025-10-24T15:49:19.347Z" }, + { url = "https://files.pythonhosted.org/packages/18/ae/40516739f99ab4c7ec3aaa5cc242d341fcb03a45d89edeeaabc5f69cb2cf/orjson-3.11.4-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:149d95d5e018bdd822e3f38c103b1a7c91f88d38a88aada5c4e9b3a73a244241", size = 140204, upload-time = "2025-10-24T15:49:20.545Z" }, + { url = "https://files.pythonhosted.org/packages/82/18/ff5734365623a8916e3a4037fcef1cd1782bfc14cf0992afe7940c5320bf/orjson-3.11.4-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:624f3951181eb46fc47dea3d221554e98784c823e7069edb5dbd0dc826ac909b", size = 406242, upload-time = "2025-10-24T15:49:21.884Z" }, + { url = "https://files.pythonhosted.org/packages/1b/48/78302d98423ed8780479a1e682b9aecb869e8404545d999d34fa486e573e/orjson-3.11.4-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:525021896afef44a68148f6ed8a8bf8375553d6066c7f48537657f64823565b9", size = 139951, upload-time = "2025-10-24T15:49:24.428Z" }, +] + +[[package]] name = "packaging" version = "25.0" source = { registry = "https://pypi.org/simple" } |