Skip to content

Commit c1cd267

Browse files
rustyconoverclaude
andcommitted
function_storage: support :memory: backend, default it for fixture HTTP server
FunctionStorageSqlite opens a fresh sqlite3 connection per operation and commits at the end, which on macOS APFS pays a per-call WAL fsync (~5ms each). The test fixture HTTP server is single-process and ephemeral — its hot path was burning seconds on fsyncs that no run will ever read back. - FunctionStorageSqlite(':memory:') now uses a shared-cache URI (file:vgi_storage_<uuid>?mode=memory&cache=shared) with a long-lived anchor connection so the per-op transient connections see the same database. MEMORY journal mode applies implicitly — no fsync. - _resolve_storage in function.py honours VGI_WORKER_SQLITE_PATH; the test fixture HTTP server (vgi-fixture-http) sets it to ':memory:' before any Function.storage access. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 393527f commit c1cd267

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎vgi/_test_fixtures/http_server.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ def main() -> None:
115115
response batches larger than ``--externalize-threshold-bytes`` are uploaded
116116
to S3 and replaced by signed URLs on the wire.
117117
"""
118+
# The test fixture server is single-process and ephemeral — there's no
119+
# value in fsyncing every commit through the WAL. Default to in-memory
120+
# storage unless the caller explicitly picked a path. Must run before
121+
# any Function.storage access so _DefaultStorageDescriptor caches the
122+
# right backend; main() runs before the WSGI app starts handling RPCs.
123+
os.environ.setdefault("VGI_WORKER_SQLITE_PATH", ":memory:")
124+
118125
import typer
119126

120127
app = typer.Typer(add_completion=False)

‎vgi/function.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ def _resolve_storage() -> FunctionStorage:
4040
"""Resolve the default FunctionStorage backend from environment."""
4141
backend = os.environ.get("VGI_WORKER_SHARED_STORAGE", "sqlite").lower()
4242
if backend == "sqlite":
43-
return FunctionStorageSqlite()
43+
# VGI_WORKER_SQLITE_PATH=":memory:" picks the in-process shared-cache
44+
# in-memory backend. Used by single-process test fixtures (notably
45+
# the test fixture HTTP server) to avoid per-op WAL fsync cost.
46+
db_path = os.environ.get("VGI_WORKER_SQLITE_PATH") or None
47+
return FunctionStorageSqlite(db_path=db_path)
4448
if backend == "azure-sql":
4549
from vgi.function_storage_azure_sql import FunctionStorageAzureSql
4650

‎vgi/function_storage.py‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,42 @@ def __init__(self, db_path: str | None = None) -> None:
428428
429429
Args:
430430
db_path: Path to the SQLite database file. If None, uses a default
431-
location in the user's state directory.
431+
location in the user's state directory. Pass ``":memory:"`` to
432+
use a process-local in-memory database; the storage uses a
433+
shared-cache URI plus an anchor connection so the per-op
434+
connections in ``_connect`` see the same DB. Suitable for
435+
single-process test fixtures where commit-fsync overhead
436+
dominates and persistence isn't needed.
432437
433438
"""
434-
self.db_path = db_path if db_path is not None else _get_default_db_path()
439+
if db_path == ":memory:":
440+
# Shared-cache in-memory: every connection to this URI sees the
441+
# same database for as long as at least one connection is open.
442+
# We hold ``_anchor_conn`` for the storage instance's lifetime so
443+
# the DB survives between transient ``_connect`` calls. The
444+
# per-instance UUID namespaces the DB so independent storage
445+
# instances within a single process don't collide.
446+
import uuid
447+
448+
self._memory_uri: str | None = (
449+
f"file:vgi_storage_{uuid.uuid4().hex}?mode=memory&cache=shared"
450+
)
451+
self._anchor_conn: sqlite3.Connection | None = sqlite3.connect(
452+
self._memory_uri, uri=True, timeout=30.0
453+
)
454+
self.db_path = ":memory:"
455+
else:
456+
self._memory_uri = None
457+
self._anchor_conn = None
458+
self.db_path = db_path if db_path is not None else _get_default_db_path()
435459
self._ensure_tables()
436460

437461
def _connect(self) -> sqlite3.Connection:
438462
"""Create a new database connection."""
463+
if self._memory_uri is not None:
464+
# Memory DBs use MEMORY journal mode implicitly; no WAL,
465+
# no fsync — the whole point of using :memory: here.
466+
return sqlite3.connect(self._memory_uri, uri=True, timeout=30.0)
439467
conn = sqlite3.connect(self.db_path, timeout=30.0)
440468
conn.execute("PRAGMA journal_mode=WAL")
441469
return conn

0 commit comments

Comments
 (0)