|
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 |
| - |
| 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 |
40 | 41 |
|
41 | 42 | @enum.unique
|
42 | 43 | class SGR(enum.Enum):
|
@@ -543,11 +544,7 @@ def _get_headers(compile_action, source_path: str):
|
543 | 544 | 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.
|
544 | 545 | try:
|
545 | 546 | with open(cache_file_path) as cache_file:
|
546 |
| - try: |
547 |
| - from orjson import loads |
548 |
| - action_key, cached_headers = loads(cache_file.read()) |
549 |
| - except ImportError: |
550 |
| - action_key, cached_headers = json.load(cache_file) |
| 547 | + action_key, cached_headers = orjson.loads(cache_file.read()) |
551 | 548 | except json.JSONDecodeError:
|
552 | 549 | # Corrupted cache, which can happen if, for example, the user kills the program, since writes aren't atomic.
|
553 | 550 | # But if it is the result of a bug, we want to print it before it's overwritten, so it can be reported
|
@@ -606,13 +603,8 @@ def _get_headers(compile_action, source_path: str):
|
606 | 603 |
|
607 | 604 | def _cache_compile_action(compile_action, cache_file_path, headers):
|
608 | 605 | cache = (compile_action.actionKey, list(headers))
|
609 |
| - try: |
610 |
| - from orjson import dumps |
611 |
| - with open(cache_file_path, 'wb') as cache_file: |
612 |
| - cache_file.write(dumps(cache)) |
613 |
| - except ImportError: |
614 |
| - with open(cache_file_path, 'w') as cache_file: |
615 |
| - json.dump(cache, cache_file) |
| 606 | + with open(cache_file_path, 'wb') as cache_file: |
| 607 | + cache_file.write(orjson.dumps(cache)) |
616 | 608 |
|
617 | 609 | def _get_files(compile_action):
|
618 | 610 | """Gets the ({source files}, {header files}) clangd should be told the command applies to."""
|
@@ -1326,21 +1318,8 @@ def _ensure_cwd_is_workspace_root():
|
1326 | 1318 |
|
1327 | 1319 | def _write_compile_commands(compile_command_entries: typing.List[str]):
|
1328 | 1320 | file_name = 'compile_commands.json'
|
1329 |
| - try: |
1330 |
| - # orjson is much faster than the standard library's json module (1.9 seconds vs 6.6 seconds for a ~140 MB file). |
1331 |
| - from orjson import dumps |
1332 |
| - with open(file_name, 'wb') as output_file: |
1333 |
| - output_file.write(dumps( |
1334 |
| - compile_command_entries, |
1335 |
| - )) |
1336 |
| - except ImportError: |
1337 |
| - with open(file_name, 'w') as output_file: |
1338 |
| - json.dump( |
1339 |
| - compile_command_entries, |
1340 |
| - output_file, |
1341 |
| - indent=2, # Yay, human readability! |
1342 |
| - check_circular=False # For speed. |
1343 |
| - ) |
| 1321 | + with open(file_name, 'wb') as output_file: |
| 1322 | + output_file.write(orjson.dumps(compile_command_entries)) |
1344 | 1323 |
|
1345 | 1324 | def main():
|
1346 | 1325 | _ensure_cwd_is_workspace_root()
|
|
0 commit comments