perf(corpus): faster remote sessions, with metadata tables cached by sha256 (#64) #82

Merged
jared merged 2 commits from perf/remote-metadata-cache-64 into main 2026-11-12 21:49:45 -05:00
Owner

Summary

  • cog_open() turns on DuckDB's enable_http_metadata_cache. Every view and query had been re-requesting the same HTTP file metadata.
  • R/cache.R, previously a stub, keeps the ten metadata tables (about 3 MB) in USCOGDATA_CACHE_DIR/corpus/. A cached copy is used only while its sha256 matches the manifest, and a download is checked before it is moved into place. On any failure the session reads remotely, as before, and prints a message. A local corpus is never cached. The yearly partitions stay remote.
  • NEWS and README updated.

Measured against the public corpus (2026-11-12, one run each)

before first session later sessions
session open 5.6 s 1.76 s 0.38 s
first one-year query 1.2 s 1.16 s 0.68 s
same query again 0.55 s 0.02 s

The first session's first query was not timed separately for the HTTP-metadata change alone, which measured 0.8 s.

Test plan

  • 7 offline tests for the cache, using an injected fetch: cold, warm, stale, sha mismatch, HTTP error, missing sha, views split between cache and corpus.
  • testthat::test_local(): 1,290 pass, 0 fail.
  • Live-corpus tests (USCOGDATA_LIVE_TEST=true): 8 pass.
  • cog_spending() for Madison (552025209777), 2012 and 2020: identical 46 rows and digest on this branch and on main.
  • CI green.

Note: profiling triggered HTTP 429 from Hugging Face's anonymous rate limit. The cache reduces requests; cog_mirror() remains the answer for heavy repeat work.

Closes #64

🤖 Generated with Claude Code

## Summary - `cog_open()` turns on DuckDB's `enable_http_metadata_cache`. Every view and query had been re-requesting the same HTTP file metadata. - `R/cache.R`, previously a stub, keeps the ten metadata tables (about 3 MB) in `USCOGDATA_CACHE_DIR/corpus/`. A cached copy is used only while its sha256 matches the manifest, and a download is checked before it is moved into place. On any failure the session reads remotely, as before, and prints a message. A local corpus is never cached. The yearly partitions stay remote. - NEWS and README updated. ## Measured against the public corpus (2026-11-12, one run each) | | before | first session | later sessions | |---|---:|---:|---:| | session open | 5.6 s | 1.76 s | 0.38 s | | first one-year query | 1.2 s | 1.16 s | 0.68 s | | same query again | 0.55 s | 0.02 s | | The first session's first query was not timed separately for the HTTP-metadata change alone, which measured 0.8 s. ## Test plan - [x] 7 offline tests for the cache, using an injected fetch: cold, warm, stale, sha mismatch, HTTP error, missing sha, views split between cache and corpus. - [x] `testthat::test_local()`: 1,290 pass, 0 fail. - [x] Live-corpus tests (`USCOGDATA_LIVE_TEST=true`): 8 pass. - [x] `cog_spending()` for Madison (552025209777), 2012 and 2020: identical 46 rows and digest on this branch and on main. - [ ] CI green. Note: profiling triggered HTTP 429 from Hugging Face's anonymous rate limit. The cache reduces requests; `cog_mirror()` remains the answer for heavy repeat work. Closes #64 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jared added 2 commits 2026-11-12 21:28:02 -05:00
On the remote path every CREATE VIEW and every query re-requested the same
file metadata over HTTP. Against the public Hugging Face corpus on 2026-11-12
(one run per arm, from this Mac), turning on DuckDB's
enable_http_metadata_cache moved:

  session open             5.6 s -> 1.6 s
  first one-year query     1.2 s -> 0.8 s
  same query again         0.55 s -> 0.02 s

parquet_metadata_cache alone changed nothing measurable, so it stays off.
The cache is per connection, so nothing persists between sessions; that half
of #64 is still open. Live-corpus tests pass with the setting on.

Refs #64

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
feat(corpus): cache the metadata tables by sha256 between sessions (#64)
R-CMD-check / check (push) Successful in 5m9s
R-CMD-check / check (pull_request) Successful in 4m53s
8b92ce33a5
R/cache.R was a stub, so a remote session read the ten metadata tables
(about 3 MB) over HTTP every time. cog_open() now keeps them in
USCOGDATA_CACHE_DIR/corpus/ and points the metadata views there. A
cached file is used only while its sha256 matches the session's manifest;
a download is checked before it replaces anything. Any failure (an HTTP
error such as Hugging Face's 429, a mismatch, an unwritable directory)
falls back to remote reads with a message. A local corpus is read in place
and never cached. The yearly partitions stay remote; cog_mirror() is
still the tool for repeat work on them.

Against the public corpus, 2026-11-12, one run each, fresh cache:

  session open   1.76 s cold -> 0.38 s warm
  first query    1.16 s cold -> 0.68 s warm

cog_spending() for Madison (552025209777), 2012 and 2020, returns the same
46 rows and the same digest as main. Live-corpus tests pass.

Closes #64

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jared merged commit dfb5b4c711 into main 2026-11-12 21:49:45 -05:00
jared deleted branch perf/remote-metadata-cache-64 2026-11-12 21:49:45 -05:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Civilytics/uscogdata#82