Skip to content

Compute Beancount table data on first access - #297

Closed
ryanduguid wants to merge 1 commit into
beancount:masterfrom
ryanduguid:fix/lazy-beancount-tables
Closed

ryanduguid wants to merge 1 commit into
beancount:masterfrom
ryanduguid:fix/lazy-beancount-tables

Conversation

@ryanduguid

Copy link
Copy Markdown

Summary

Compute the price map, account open/close map, commodity map and account types on first access, then cache each result on its table. Attaching a Beancount source and running a query over postings currently builds all four even when none is used. Closes #244.

The existing builders and query consumers use the same attributes, backed by the standard library:

@cached_property
def price_map(self):
    return prices.build_price_map(self.entries)

Evidence

  • Five new test methods have six failures on the original source, including subcases for independent caches. After the change, queries over postings call none of the four builders; repeated access caches each result independently. Query results, custom account names, reattachment and direct price_map assignment are covered.
  • All 298 tests pass on Linux Python 3.8.20/Beancount 2.3.6, Python 3.11.16/3.2.3 and Python 3.14.7/3.2.3.
  • Changed-file Ruff passes. Full Ruff retains the same five baseline errors in query_render.py and shell.py. Parser and render coverage gates pass at 100%; overall coverage is 91%, with all four new getter return paths covered.
  • Source and wheel builds, installation into a fresh environment and the installed production source hash check pass. The two changed paths have no security scan findings.
  • Five focused tests pass on Windows Python 3.14.7. The full suite retains the unchanged base's three temporary-file handle errors and one CSV newline failure.

Merge danger

Door: two-way. Blast radius: query sources.

The first calculation uses the entries, options and decimal context present at first access. Mutating inputs before that access affects the derived value; later mutation does not invalidate the cache. Reattaching creates new tables.

On Python 3.12 and later, concurrent first access can repeat a calculation because cached_property does not serialise it. Concurrent price-map builds can use different thread decimal contexts for inverse prices; the latest run sets the cached value, following Python's cached_property contract.

Unverified

Remaining CI runtime/dependency combinations, docs build, upstream CI, timing benchmarks, concurrent first access and input mutation outside the tested contracts.

@dnicolodi dnicolodi closed this Oct 2, 2026
@ryanduguid
ryanduguid deleted the fix/lazy-beancount-tables branch October 22, 2026 03:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sources.beancount: Lazily compute tables data

2 participants