Skip to content

Commit 9393c22

Browse files
authored
Reduce size of JSON data in Mypy cache (#14808)
By default, Python outputs spaces after commas and colons when dumping JSON data. This is normally fine, but when exporting large amounts of JSON (such as cache info) it is more space efficient to export JSON without whitespaces. To do this, simply pass `separators=(",", ":")` to `json.dumps()`. Although the space savings aren't massive, they do reduce the size of the cache folder by a couple of megabytes: | Method | Size | |---------|--------| | Current | 31.2MB | | This PR | 29.3MB | For comparison, the size of the cache folder with the `--sqlite-cache` option is about 27.5MB.
1 parent dc5ff29 commit 9393c22

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

mypy/build.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ def stats_summary(self) -> Mapping[str, object]:
919919

920920

921921
def deps_to_json(x: dict[str, set[str]]) -> str:
922-
return json.dumps({k: list(v) for k, v in x.items()})
922+
return json.dumps({k: list(v) for k, v in x.items()}, separators=(",", ":"))
923923

924924

925925
# File for storing metadata about all the fine-grained dependency caches
@@ -987,7 +987,7 @@ def write_deps_cache(
987987

988988
meta = {"snapshot": meta_snapshot, "deps_meta": fg_deps_meta}
989989

990-
if not metastore.write(DEPS_META_FILE, json.dumps(meta)):
990+
if not metastore.write(DEPS_META_FILE, json.dumps(meta, separators=(",", ":"))):
991991
manager.log(f"Error writing fine-grained deps meta JSON file {DEPS_META_FILE}")
992992
error = True
993993

@@ -1055,7 +1055,8 @@ def generate_deps_for_cache(manager: BuildManager, graph: Graph) -> dict[str, di
10551055

10561056
def write_plugins_snapshot(manager: BuildManager) -> None:
10571057
"""Write snapshot of versions and hashes of currently active plugins."""
1058-
if not manager.metastore.write(PLUGIN_SNAPSHOT_FILE, json.dumps(manager.plugins_snapshot)):
1058+
snapshot = json.dumps(manager.plugins_snapshot, separators=(",", ":"))
1059+
if not manager.metastore.write(PLUGIN_SNAPSHOT_FILE, snapshot):
10591060
manager.errors.set_file(_cache_dir_prefix(manager.options), None, manager.options)
10601061
manager.errors.report(0, 0, "Error writing plugins snapshot", blocker=True)
10611062

@@ -1487,7 +1488,7 @@ def validate_meta(
14871488
if manager.options.debug_cache:
14881489
meta_str = json.dumps(meta_dict, indent=2, sort_keys=True)
14891490
else:
1490-
meta_str = json.dumps(meta_dict)
1491+
meta_str = json.dumps(meta_dict, separators=(",", ":"))
14911492
meta_json, _, _ = get_cache_names(id, path, manager.options)
14921493
manager.log(
14931494
"Updating mtime for {}: file {}, meta {}, mtime {}".format(
@@ -1517,7 +1518,7 @@ def json_dumps(obj: Any, debug_cache: bool) -> str:
15171518
if debug_cache:
15181519
return json.dumps(obj, indent=2, sort_keys=True)
15191520
else:
1520-
return json.dumps(obj, sort_keys=True)
1521+
return json.dumps(obj, sort_keys=True, separators=(",", ":"))
15211522

15221523

15231524
def write_cache(
@@ -3639,4 +3640,4 @@ def write_undocumented_ref_info(state: State, metastore: MetadataStore, options:
36393640
assert not ref_info_file.startswith(".")
36403641

36413642
deps_json = get_undocumented_ref_info_json(state.tree)
3642-
metastore.write(ref_info_file, json.dumps(deps_json))
3643+
metastore.write(ref_info_file, json.dumps(deps_json, separators=(",", ":")))

mypyc/codegen/emitmodule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def write_cache(
369369
"src_hashes": hashes[group_map[id]],
370370
}
371371

372-
result.manager.metastore.write(newpath, json.dumps(ir_data))
372+
result.manager.metastore.write(newpath, json.dumps(ir_data, separators=(",", ":")))
373373

374374
result.manager.metastore.commit()
375375

0 commit comments

Comments
 (0)