Skip to content

Commit

Permalink
Performance: Switch to orjson for faster json auto-formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
DeflateAwning committed Oct 8, 2024
1 parent dcbfde8 commit 347ad8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
12 changes: 8 additions & 4 deletions cts1_ground_support/json_parser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Tools for parsing JSON strings from blobs of text."""

import json
from collections.abc import Iterator
from dataclasses import dataclass

import orjson


@dataclass(kw_only=True)
class ParsedJson:
Expand All @@ -24,7 +25,7 @@ def extract_json_blobs(content: str) -> Iterator[ParsedJson]:
for end_idx in range(len(content) - 1, start_idx, -1):
if content[end_idx] == "}":
try:
data = json.loads(content[start_idx : end_idx + 1])
data = orjson.loads(content[start_idx : end_idx + 1])
yield ParsedJson(
data=data,
start_idx=start_idx,
Expand All @@ -33,7 +34,7 @@ def extract_json_blobs(content: str) -> Iterator[ParsedJson]:
)
start_idx = end_idx
break
except json.JSONDecodeError:
except orjson.JSONDecodeError:
pass
start_idx += 1

Expand All @@ -43,6 +44,9 @@ def auto_format_json_in_blob(blob: str) -> str:
json_parts = list(extract_json_blobs(blob))

for json_part in json_parts:
blob = blob.replace(json_part.original_str, json.dumps(json_part.data, indent=4))
formatted_data: str = orjson.dumps(json_part.data, option=orjson.OPT_INDENT_2).decode(
"utf-8"
)
blob = blob.replace(json_part.original_str, formatted_data)

return blob
14 changes: 8 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ build-backend = "hatchling.build"
name = "cts1_ground_support"
version = "v0-dev"
dependencies = [
"GitPython",
"pyserial",
"loguru",
"dash",
"dash-bootstrap-components",
"dash-split-pane~=1.0.0",
"GitPython<4,>3",
"pyserial>3,<4",
"loguru~=0.7.2",
"dash>2,<3",
"dash-bootstrap-components>1,<2",
"dash-split-pane>=1,<2",
"pytz",
"orjson>3,<4",
"platformdirs>4,<5"
]
requires-python = ">=3.10"
authors = [
Expand Down

0 comments on commit 347ad8a

Please sign in to comment.