fix(disk): cap build memory, document close() and build cost
Browse files- README.md +3 -1
- wordlist_query.py +10 -2
README.md
CHANGED
|
@@ -404,7 +404,9 @@ it** for the life of the process rather than reconstructing it per query.
|
|
| 404 |
`WordlistQuery(disk=True)` for the middle ground: a one-time build of a persistent indexed file
|
| 405 |
(~500 MB, written to `wordlist-index.duckdb` in the working directory — pass `disk="/path/to/index.duckdb"`
|
| 406 |
to place it elsewhere), then fast index-seek queries (~tens of ms) at that same low footprint. The file
|
| 407 |
-
is rebuilt automatically when a new release changes the data
|
|
|
|
|
|
|
| 408 |
|
| 409 |
Each result is a `Match(prefix, wordform, function, row, morph_anchor, spelling, anchor_genres)`:
|
| 410 |
- `prefix` — the attached prefix letters (`""` if none).
|
|
|
|
| 404 |
`WordlistQuery(disk=True)` for the middle ground: a one-time build of a persistent indexed file
|
| 405 |
(~500 MB, written to `wordlist-index.duckdb` in the working directory — pass `disk="/path/to/index.duckdb"`
|
| 406 |
to place it elsewhere), then fast index-seek queries (~tens of ms) at that same low footprint. The file
|
| 407 |
+
is rebuilt automatically when a new release changes the data; note the one-time build peaks above the
|
| 408 |
+
steady-state footprint (it is not itself a low-memory operation). Call `.close()` to release the file
|
| 409 |
+
handle when done (needed on Windows before an in-process rebuild).
|
| 410 |
|
| 411 |
Each result is a `Match(prefix, wordform, function, row, morph_anchor, spelling, anchor_genres)`:
|
| 412 |
- `prefix` — the attached prefix letters (`""` if none).
|
wordlist_query.py
CHANGED
|
@@ -41,7 +41,10 @@ Pass `disk=True` (or `disk="/path/to/index.duckdb"`) for the middle ground: a on
|
|
| 41 |
persistent, indexed DuckDB file (inverted spelling->id index), then opened read-only with a capped
|
| 42 |
buffer pool. Resident RAM stays near the low_memory baseline, but queries are index seeks (~tens of
|
| 43 |
ms) instead of scans (~1s). Best for bulk programmatic lookups where eager's RAM is too much. The
|
| 44 |
-
on-disk file (~500 MB) is rebuilt automatically when the dataset content changes (new release)
|
|
|
|
|
|
|
|
|
|
| 45 |
"""
|
| 46 |
from dataclasses import dataclass
|
| 47 |
from enum import Enum
|
|
@@ -191,10 +194,15 @@ class WordlistQuery:
|
|
| 191 |
# and a reader opening `path` sees either the whole old index or the whole new one.
|
| 192 |
# (os.replace is atomic on POSIX; on Windows it still raises if another process holds `path`.)
|
| 193 |
self._log(f"building disk index at {path} (one-time) …")
|
|
|
|
|
|
|
| 194 |
tmp = f"{path}.tmp"
|
| 195 |
for p in (tmp, f"{tmp}.wal"):
|
| 196 |
if os.path.exists(p): os.remove(p)
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
| 198 |
try:
|
| 199 |
b.register("wl", getattr(self.ds.data, "table", self.ds.data))
|
| 200 |
b.register("att", getattr(self.ads.data, "table", self.ads.data))
|
|
|
|
| 41 |
persistent, indexed DuckDB file (inverted spelling->id index), then opened read-only with a capped
|
| 42 |
buffer pool. Resident RAM stays near the low_memory baseline, but queries are index seeks (~tens of
|
| 43 |
ms) instead of scans (~1s). Best for bulk programmatic lookups where eager's RAM is too much. The
|
| 44 |
+
on-disk file (~500 MB) is rebuilt automatically when the dataset content changes (new release); that
|
| 45 |
+
one-time build peaks above the steady-state footprint (capped, spills to disk), so it is not itself a
|
| 46 |
+
low-memory operation. Call `.close()` to release the file handle (only needed in low_memory/disk mode;
|
| 47 |
+
required on Windows before the file can be rebuilt in-process); the instance is unusable afterward.
|
| 48 |
"""
|
| 49 |
from dataclasses import dataclass
|
| 50 |
from enum import Enum
|
|
|
|
| 194 |
# and a reader opening `path` sees either the whole old index or the whole new one.
|
| 195 |
# (os.replace is atomic on POSIX; on Windows it still raises if another process holds `path`.)
|
| 196 |
self._log(f"building disk index at {path} (one-time) …")
|
| 197 |
+
# ponytail: fixed temp name — a second process building the same `path` at once would collide;
|
| 198 |
+
# fine for a single-user helper, add a pid/lock if concurrent first-time builds matter.
|
| 199 |
tmp = f"{path}.tmp"
|
| 200 |
for p in (tmp, f"{tmp}.wal"):
|
| 201 |
if os.path.exists(p): os.remove(p)
|
| 202 |
+
# Cap the one-time build too (DuckDB spills to disk when capped) so it doesn't briefly grab
|
| 203 |
+
# ~80% of RAM on the very box where disk mode was chosen to avoid eager's footprint. The build
|
| 204 |
+
# still peaks above steady-state; it just won't balloon.
|
| 205 |
+
b = duckdb.connect(tmp, config={"memory_limit": "1GB"})
|
| 206 |
try:
|
| 207 |
b.register("wl", getattr(self.ds.data, "table", self.ds.data))
|
| 208 |
b.register("att", getattr(self.ads.data, "table", self.ads.data))
|