Skip to content

Commit a2df1a0

Browse files
committed
perf: optionally use 'orjson' if available
This creates a "json" shim such that using it guarantees compatibility between orjson and the stdlib json. The interface of the shim is compatible with the stdlib json, though the output will be formatted slightly differently.
1 parent 9b77f7d commit a2df1a0

File tree

16 files changed

+353
-30
lines changed

16 files changed

+353
-30
lines changed

pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ dependencies = [
3434
dynamic = ["version"] # set in src/taskgraph/__init__.py
3535

3636
[project.optional-dependencies]
37-
load-image = [
38-
"zstandard"
39-
]
37+
load-image = ["zstandard"]
38+
orjson = ["orjson"]
4039

4140
[project.scripts]
4241
taskgraph = "taskgraph.main:main"

src/taskgraph/actions/registry.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
import functools
7-
import json
87
from collections import namedtuple
98
from types import FunctionType
109

@@ -13,7 +12,7 @@
1312
from taskgraph import create
1413
from taskgraph.config import load_graph_config
1514
from taskgraph.parameters import Parameters
16-
from taskgraph.util import hash, taskcluster, yaml
15+
from taskgraph.util import hash, json, taskcluster, yaml
1716
from taskgraph.util.python_path import import_sibling_modules
1817

1918
actions = []

src/taskgraph/create.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55

6-
import json
76
import logging
87
import os
98
import sys
109
from concurrent import futures
1110

1211
from slugid import nice as slugid
1312

13+
from taskgraph.util import json
1414
from taskgraph.util.parameterization import resolve_timestamps
1515
from taskgraph.util.taskcluster import CONCURRENCY, get_session
1616
from taskgraph.util.time import current_json_time
@@ -116,8 +116,7 @@ def create_task(session, task_id, label, task_def):
116116
[task_id, task_def],
117117
sys.stdout,
118118
sort_keys=True,
119-
indent=4,
120-
separators=(",", ": "),
119+
indent=2,
121120
)
122121
# add a newline
123122
print("")

src/taskgraph/decision.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55

6-
import json
76
import logging
87
import os
98
import pathlib
@@ -19,6 +18,7 @@
1918
from taskgraph.generator import TaskGraphGenerator
2019
from taskgraph.parameters import Parameters, get_version
2120
from taskgraph.taskgraph import TaskGraph
21+
from taskgraph.util import json
2222
from taskgraph.util.python_path import find_object
2323
from taskgraph.util.schema import Schema, validate_schema
2424
from taskgraph.util.vcs import Repository, get_repository
@@ -374,7 +374,7 @@ def write_artifact(filename, data):
374374
yaml.safe_dump(data, f, allow_unicode=True, default_flow_style=False)
375375
elif filename.endswith(".json"):
376376
with open(path, "w") as f:
377-
json.dump(data, f, sort_keys=True, indent=2, separators=(",", ": "))
377+
json.dump(data, f, sort_keys=True, indent=2)
378378
elif filename.endswith(".gz"):
379379
import gzip
380380

src/taskgraph/docker.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55

6-
import json
76
import os
87
import shlex
98
import subprocess
@@ -18,7 +17,7 @@
1817
except ImportError as e:
1918
zstd = e
2019

21-
from taskgraph.util import docker
20+
from taskgraph.util import docker, json
2221
from taskgraph.util.taskcluster import (
2322
get_artifact_url,
2423
get_root_url,

src/taskgraph/main.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import argparse
66
import atexit
7-
import json
87
import logging
98
import os
109
import re
@@ -56,9 +55,9 @@ def format_taskgraph_labels(taskgraph):
5655

5756

5857
def format_taskgraph_json(taskgraph):
59-
return json.dumps(
60-
taskgraph.to_json(), sort_keys=True, indent=2, separators=(",", ": ")
61-
)
58+
from taskgraph.util import json
59+
60+
return json.dumps(taskgraph.to_json(), sort_keys=True, indent=2)
6261

6362

6463
def format_taskgraph_yaml(taskgraph):
@@ -757,6 +756,7 @@ def actions(args):
757756
from taskgraph.actions import render_actions_json
758757
from taskgraph.generator import TaskGraphGenerator
759758
from taskgraph.parameters import parameters_loader
759+
from taskgraph.util import json
760760

761761
if args.pop("verbose", False):
762762
logging.root.setLevel(logging.DEBUG)
@@ -766,7 +766,7 @@ def actions(args):
766766
tgg = TaskGraphGenerator(root_dir=args.get("root"), parameters=parameters)
767767

768768
actions = render_actions_json(tgg.parameters, tgg.graph_config, "DECISION-TASK")
769-
print(json.dumps(actions, sort_keys=True, indent=2, separators=(",", ": ")))
769+
print(json.dumps(actions, sort_keys=True, indent=2))
770770
except Exception:
771771
traceback.print_exc()
772772
sys.exit(1)
@@ -784,6 +784,7 @@ def actions(args):
784784
def action_callback(options):
785785
from taskgraph.actions import trigger_action_callback
786786
from taskgraph.actions.util import get_parameters
787+
from taskgraph.util import json
787788

788789
try:
789790
# the target task for this action (or null if it's a group action)
@@ -833,7 +834,7 @@ def test_action_callback(options):
833834
import taskgraph.actions
834835
import taskgraph.parameters
835836
from taskgraph.config import load_graph_config
836-
from taskgraph.util import yaml
837+
from taskgraph.util import json, yaml
837838

838839
def load_data(filename):
839840
with open(filename) as f:

src/taskgraph/parameters.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import gzip
66
import hashlib
7-
import json
87
import os
98
import time
109
from datetime import datetime
@@ -18,7 +17,7 @@
1817
import mozilla_repo_urls
1918
from voluptuous import ALLOW_EXTRA, Any, Optional, Required, Schema
2019

21-
from taskgraph.util import yaml
20+
from taskgraph.util import json, yaml
2221
from taskgraph.util.readonlydict import ReadOnlyDict
2322
from taskgraph.util.schema import validate_schema
2423
from taskgraph.util.taskcluster import find_task_id, get_artifact_url

src/taskgraph/transforms/docker_image.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5-
6-
import json
75
import logging
86
import os
97
import re
@@ -12,6 +10,7 @@
1210

1311
import taskgraph
1412
from taskgraph.transforms.base import TransformSequence
13+
from taskgraph.util import json
1514
from taskgraph.util.docker import create_context_tar, generate_context_hash
1615
from taskgraph.util.schema import Schema
1716

src/taskgraph/transforms/run/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
"""
1111

1212
import copy
13-
import json
1413
import logging
1514

1615
from voluptuous import Exclusive, Extra, Optional, Required
1716

1817
from taskgraph.transforms.base import TransformSequence
1918
from taskgraph.transforms.cached_tasks import order_tasks
2019
from taskgraph.transforms.task import task_description_schema
20+
from taskgraph.util import json
2121
from taskgraph.util import path as mozpath
2222
from taskgraph.util.python_path import import_sibling_modules
2323
from taskgraph.util.schema import Schema, validate_schema

src/taskgraph/transforms/run/common.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
consistency.
88
"""
99

10-
import json
1110
from typing import Any, Dict, List, Union
1211

1312
from taskgraph.transforms.base import TransformConfig
14-
from taskgraph.util import path
13+
from taskgraph.util import json, path
1514
from taskgraph.util.caches import CACHES, get_checkout_dir
1615
from taskgraph.util.taskcluster import get_artifact_prefix
1716

0 commit comments

Comments
 (0)