Skip to content

Commit 037389e

Browse files
committed
perf: use orjson for writing compile_commands when available
orjson is much faster than the standard library's json module (1.9 seconds vs 6.6 seconds for a ~140 MB file). Benchmarks: orjson: 67406 function calls (66880 primitive calls) in 1.919 seconds json: 21663390 function calls (18606459 primitive calls) in 6.588 seconds json no-indent: 21660892 function calls (18603961 primitive calls) in 6.437 seconds
1 parent b33a4b0 commit 037389e

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

refresh.template.py

+23-10
Original file line numberDiff line numberDiff line change
@@ -1048,8 +1048,25 @@ def _ensure_cwd_is_workspace_root():
10481048
# Although this can fail (OSError/FileNotFoundError/PermissionError/NotADirectoryError), there's no easy way to recover, so we'll happily crash.
10491049
os.chdir(workspace_root)
10501050

1051-
1052-
if __name__ == '__main__':
1051+
def _write_compile_commands(compile_command_entries: typing.List[str]):
1052+
file_name = 'compile_commands.json'
1053+
try:
1054+
# orjson is much faster than the standard library's json module (1.9 seconds vs 6.6 seconds for a ~140 MB file).
1055+
from orjson import dumps
1056+
with open(file_name, 'wb') as output_file:
1057+
output_file.write(dumps(
1058+
compile_command_entries,
1059+
))
1060+
except ImportError:
1061+
with open(file_name, 'w') as output_file:
1062+
json.dump(
1063+
compile_command_entries,
1064+
output_file,
1065+
indent=2, # Yay, human readability!
1066+
check_circular=False # For speed.
1067+
)
1068+
1069+
def main():
10531070
_ensure_cwd_is_workspace_root()
10541071
_ensure_gitignore_entries_exist()
10551072
_ensure_external_workspaces_link_exists()
@@ -1069,11 +1086,7 @@ def _ensure_cwd_is_workspace_root():
10691086
There should be actionable warnings, above, that led to this.""")
10701087
sys.exit(1)
10711088

1072-
# Chain output into compile_commands.json
1073-
with open('compile_commands.json', 'w') as output_file:
1074-
json.dump(
1075-
compile_command_entries,
1076-
output_file,
1077-
indent=2, # Yay, human readability!
1078-
check_circular=False # For speed.
1079-
)
1089+
_write_compile_commands(compile_command_entries)
1090+
1091+
if __name__ == '__main__':
1092+
main()

0 commit comments

Comments
 (0)