From a60e65e110ae4f8d3be23bd89fd2f2ccdb00696c Mon Sep 17 00:00:00 2001 From: mokurin000 <1348292515a@gmail.com> Date: Sun, 3 Aug 2025 10:32:29 +0800 Subject: [PATCH] perf: migrate to orjson for performance --- .gitignore | 7 ++++++- pyproject.toml | 9 +++++++++ utils/export_players.py | 14 ++++++-------- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore index e25073b..7f085ef 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,9 @@ /players.redb* /players*.json* -/b50*.json* \ No newline at end of file +/b50*.json* + +/.python-version +/uv.lock + +/.venv \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..028783c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,9 @@ +[project] +name = "sdgb-utils-rs" +version = "0.1.0" +description = "Add your description here" +readme = "README.md" +requires-python = ">=3.12" +dependencies = [ + "orjson>=3.11.1", +] diff --git a/utils/export_players.py b/utils/export_players.py index 4685455..c0ee5fc 100644 --- a/utils/export_players.py +++ b/utils/export_players.py @@ -1,4 +1,4 @@ -import json +import orjson as json import hashlib from typing import Callable from datetime import datetime @@ -7,7 +7,7 @@ from decimal import Decimal, getcontext SALT = b"Lt2N5xgjJOqRsT5qVt7wWYw6SqOPZDI7" with open("musicDB.json", "r", encoding="utf-8") as f: - music_db = json.load(f) + music_db = json.loads(f.read()) music_db = {entry["id"]: entry for entry in music_db} @@ -169,19 +169,17 @@ def process( output_file: str, ): record_time() - with open(input_file, "r", encoding="utf-8") as f: - data = json.load(f) + with open(input_file, "rb") as f: + data = json.loads(f.read()) print(f"loaded, cost {record_time():.2f}s") - record_time() for entry in data: salted_hash_userid(entry) clean_fields(entry) print(f"processed, cost {record_time():.2f}s") - record_time() - with open(output_file, "w", encoding="utf-8") as f: - json.dump(data, f, ensure_ascii=False) + with open(output_file, "wb") as f: + f.write(json.dumps(data)) print(f"written out, cost {record_time():.2f}s") return data