|
36 | 36 | import time
|
37 | 37 | import types
|
38 | 38 | import typing # MIN_PY=3.9: Switch e.g. typing.List[str] -> list[str]
|
39 |
| -# orjson is much faster than the standard library's json module (1.9 seconds vs 6.6 seconds for a ~140 MB file). |
40 |
| -import orjson |
| 39 | + |
41 | 40 |
|
42 | 41 | @enum.unique
|
43 | 42 | class SGR(enum.Enum):
|
@@ -508,7 +507,11 @@ def _get_headers(compile_action, source_path: str):
|
508 | 507 | cache_last_modified = os.path.getmtime(cache_file_path) # Do before opening just as a basic hedge against concurrent write, even though we won't handle the concurrent delete case perfectly.
|
509 | 508 | try:
|
510 | 509 | with open(cache_file_path) as cache_file:
|
511 |
| - action_key, headers = orjson.loads(cache_file.read()) |
| 510 | + try: |
| 511 | + from orjson import loads |
| 512 | + action_key, headers = loads(cache_file.read()) |
| 513 | + except ImportError: |
| 514 | + action_key, headers = json.load(cache_file) |
512 | 515 | except json.JSONDecodeError:
|
513 | 516 | # Corrupted cache, which can happen if, for example, the user kills the program, since writes aren't atomic.
|
514 | 517 | # But if it is the result of a bug, we want to print it before it's overwritten, so it can be reported
|
@@ -559,8 +562,13 @@ def _get_headers(compile_action, source_path: str):
|
559 | 562 |
|
560 | 563 | def _cache_compile_action(compile_action, cache_file_path, headers):
|
561 | 564 | cache = (compile_action.actionKey, list(headers))
|
562 |
| - with open(cache_file_path, 'wb') as cache_file: |
563 |
| - cache_file.write(orjson.dumps(cache)) |
| 565 | + try: |
| 566 | + from orjson import dumps |
| 567 | + with open(cache_file_path, 'wb') as cache_file: |
| 568 | + cache_file.write(dumps(cache)) |
| 569 | + except ImportError: |
| 570 | + with open(cache_file_path, 'w') as cache_file: |
| 571 | + json.dump(cache, cache_file) |
564 | 572 |
|
565 | 573 | def _get_files(compile_action):
|
566 | 574 | """Gets the ({source files}, {header files}) clangd should be told the command applies to."""
|
@@ -1055,8 +1063,21 @@ def _ensure_cwd_is_workspace_root():
|
1055 | 1063 |
|
1056 | 1064 | def _write_compile_commands(compile_command_entries: typing.List[str]):
|
1057 | 1065 | file_name = 'compile_commands.json'
|
1058 |
| - with open(file_name, 'wb') as output_file: |
1059 |
| - output_file.write(orjson.dumps(compile_command_entries)) |
| 1066 | + try: |
| 1067 | + # orjson is much faster than the standard library's json module (1.9 seconds vs 6.6 seconds for a ~140 MB file). |
| 1068 | + from orjson import dumps |
| 1069 | + with open(file_name, 'wb') as output_file: |
| 1070 | + output_file.write(dumps( |
| 1071 | + compile_command_entries, |
| 1072 | + )) |
| 1073 | + except ImportError: |
| 1074 | + with open(file_name, 'w') as output_file: |
| 1075 | + json.dump( |
| 1076 | + compile_command_entries, |
| 1077 | + output_file, |
| 1078 | + indent=2, # Yay, human readability! |
| 1079 | + check_circular=False # For speed. |
| 1080 | + ) |
1060 | 1081 |
|
1061 | 1082 | def main():
|
1062 | 1083 | _ensure_cwd_is_workspace_root()
|
|
0 commit comments