From 86d319d200dda07433013e3356a0100cea25babb Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Sat, 15 Aug 2026 20:57:43 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[=EC=84=B1=EB=8A=A5=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0]=20SQLite=20WAL=20=EC=A0=80=EB=84=90=20?= =?UTF-8?q?=EB=AA=A8=EB=93=9C=20=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20?= =?UTF-8?q?=EC=9E=AC=EC=84=A4=EC=A0=95=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .jules/bolt.md | 4 ++++ job_store.py | 3 +-- usage_metering.py | 3 +-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 341c7c91..2287781e 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -1,3 +1,7 @@ +## 2025-02-18 - Persistent SQLite PRAGMA journal_mode=WAL +**Learning:** SQLite's `PRAGMA journal_mode=WAL` is persistent per database file. In an architecture with many short-lived connections, executing it redundantly on every connection adds significant overhead. +**Action:** Execute `PRAGMA journal_mode=WAL` once during database initialization (e.g., via `conn.executescript()` alongside schema creation) instead of every time `_connect()` is called. + ## 2024-05-28 - Avoid O(N^2) Path.resolve() in Batch Processing **Learning:** Python's `pathlib.Path.resolve()` is relatively slow because it touches the filesystem to follow symlinks and resolve relative paths. When dealing with a batch operation (e.g., scanning large directories of media files), calculating protected files via `any(target == src.resolve() for src in sources)` on every check leads to massive O(N^2) CPU overhead. **Action:** Pre-resolve the entire list of candidate paths once into a `frozenset` at the beginning of the batch process. Pass this resolved set down the call stack so that collision/protection checks become O(1) hash map lookups instead of triggering millions of unnecessary disk access operations. diff --git a/job_store.py b/job_store.py index 15601581..cb06e8c7 100644 --- a/job_store.py +++ b/job_store.py @@ -95,7 +95,7 @@ def __init__(self, db_path: str) -> None: self._db_path = str(db_path) self._lock = threading.Lock() with self._connect() as conn: - conn.execute(_SCHEMA) + conn.executescript("PRAGMA journal_mode=WAL;\n" + _SCHEMA) @contextmanager def _connect(self) -> Iterator[sqlite3.Connection]: @@ -108,7 +108,6 @@ def _connect(self) -> Iterator[sqlite3.Connection]: conn = sqlite3.connect(self._db_path, timeout=30.0) try: conn.row_factory = sqlite3.Row - conn.execute("PRAGMA journal_mode=WAL") yield conn conn.commit() finally: diff --git a/usage_metering.py b/usage_metering.py index 16fbac74..d456dbd3 100644 --- a/usage_metering.py +++ b/usage_metering.py @@ -121,7 +121,7 @@ def __init__(self, db_path: str | Path) -> None: self._lock = threading.Lock() with closing(self._connect()) as conn: with conn: - conn.execute(_SCHEMA) + conn.executescript("PRAGMA journal_mode=WAL;\n" + _SCHEMA) def _connect(self) -> sqlite3.Connection: """Open a new short-lived connection with WAL mode enabled. @@ -130,7 +130,6 @@ def _connect(self) -> sqlite3.Connection: A fresh :class:`sqlite3.Connection` to the store's database. """ conn = sqlite3.connect(self._db_path, timeout=30.0) - conn.execute("PRAGMA journal_mode=WAL") return conn def record(