diff --git a/changedetectionio/store.py b/changedetectionio/store.py index ade3be7e..0cf6f3ab 100644 --- a/changedetectionio/store.py +++ b/changedetectionio/store.py @@ -22,6 +22,13 @@ import uuid as uuid_builder from loguru import logger from blinker import signal +# Try to import orjson for faster JSON serialization +try: + import orjson + HAS_ORJSON = True +except ImportError: + HAS_ORJSON = False + from .processors import get_custom_watch_obj_for_processor from .processors.restock_diff import Restock @@ -426,9 +433,14 @@ class ChangeDetectionStore: # Re #286 - First write to a temp file, then confirm it looks OK and rename it # This is a fairly basic strategy to deal with the case that the file is corrupted, # system was out of memory, out of RAM etc - with open(self.json_store_path+".tmp", 'w') as json_file: - # Use compact JSON in production for better performance - json.dump(data, json_file, indent=2) + if HAS_ORJSON: + # Use orjson for faster serialization + with open(self.json_store_path+".tmp", 'wb') as json_file: + json_file.write(orjson.dumps(data, option=orjson.OPT_INDENT_2)) + else: + # Fallback to standard json module + with open(self.json_store_path+".tmp", 'w') as json_file: + json.dump(data, json_file, indent=2) os.replace(self.json_store_path+".tmp", self.json_store_path) except Exception as e: logger.error(f"Error writing JSON!! (Main JSON file save was skipped) : {str(e)}") diff --git a/requirements.txt b/requirements.txt index 1b95a9dc..22890297 100644 --- a/requirements.txt +++ b/requirements.txt @@ -33,6 +33,9 @@ chardet>2.3.0 wtforms~=3.2 jsonpath-ng~=1.7.0 +# Fast JSON serialization for better performance +orjson~=3.10 + # dnspython - Used by paho-mqtt for MQTT broker resolution # Version pin removed since eventlet (which required the specific 2.6.1 pin) has been eliminated # paho-mqtt will install compatible dnspython version automatically