Skip to content

chore(engine): one cache per fact a build reads once, and a presence named by its variable - #2330

Open
FBumann wants to merge 31 commits into
mainfrom
claude/vibrant-clarke-mpdv2p
Open

FBumann wants to merge 31 commits into
mainfrom
claude/vibrant-clarke-mpdv2p

Conversation

@FBumann

@FBumann FBumann commented Dec 6, 2026 •

Copy link
Copy Markdown
Collaborator

Prompt: I think the whole caching layer should get a proper review. I think there is a lot of room for simplififcation and unification with proper renames

Prompt: Fix finding 4 for the proper, safe variant.

Prompt: do the parameter presence too and measure it

Note

The following content was generated by AI.

A build's per-build caches go from eight under unrelated names to five, one per fact. A presence is one with another by the variable, parameter or condition it holds, never by table object, so no cache has to keep an object alive for #2323's dedup to work. Same model, same speed.

What changed, mutation table, A/B, gates

Caches (queries.py, assembly.py)

was now holds
_claims / _claimed, _variable_keys _held_coordinates / _coordinates(mask, on) where a mask holds, over the dimensions it reads; one cache, regions and variables both ask it
_variable_presence[(var, dims)] + _built_presence gone; _variable_presences(var) builds it each read the dims key part never varied; identity no longer needs the object kept
_presence_keys _sparse_dims what _sparse_along found
_read_counts + _named / _named_terms shared field, from shared_expressions(program), + _held_terms / _shared_terms counted once by the assembly, not lazily on the first ask
_collected(terms, kept) _held_in_memory(terms) no kept: a variable's presence collected too keeps its identity
_where_coordinates / _where_rows _admitted / _admitted_rows the caller already called it admitted
  • removed: _named_terms's solution is not None branch, dead since no builder sets both builds and solution
  • unchanged: _empty_tables, the dimensions' cached properties, collect_engine

Identity (terms.py)

  • Presence.of: ('variable', name) or ('parameter', name), set only where the table is that variable's condition or that parameter's rows and nothing else; two presences of one are one whatever table holds them, and the kind keeps a variable and a parameter of one name apart
  • Unclaimed.when: the condition a region claims by; two claims by one condition are one
  • every other presence stays itself only, by table object: two tables with the same rows are never assumed one
  • _relaxed keeps the name where it keeps the table (replace), drops it where it writes a new one

Mutation table, by hand on 33e204c9/d6b3f454, full suite with -n auto, restored with git checkout --, __pycache__ dropped both sides, tree clean after each:

mutation caught by
identity by table object, as on main 4 tests, #2323's and #2269's dedup probes
unsafe: every presence with no variable is one …admit_the_same_rows[two tables with the same rows], and a fuzz oracle (a quotient beside a shift)
a claim compared by table object …admit_the_same_rows, two cases
unsafe: a claim compared by its columns alone …admit_the_same_rows[two conditions' claims in one table]
a whole-dimension variable presence unnamed survived; probe added: …narrows_by_one_presence[a condition over every dimension]
a region drops the variable's name survived; probe added: …narrows_by_one_presence[inside a region]
a parameter's presence unnamed (1b73ccb0) …narrows_by_one_presence[a parameter with no row at some coordinates]
unsafe: a variable and a parameter of one name are one (1b73ccb0) …admit_the_same_rows[a variable and a parameter of one name]

A/B (pixi run -e bench python -m bench.ab.compare bb860380 998ac03d --sizes pypsa --rounds 16, base main bb860380, warm sps.build wall, a fresh process per side per round, ABBA, 4 cores, load 0.52 before)

  • 44 of 45 cells no change, 1.0 expected by chance; carbon_management +0.4%, unified -0.8%, both last bits (The osemosys_utopia model differs in its last bit from one build to the next, so its archives refuse undeclared reads #1898)
  • the one faster, port-pypsa_store -10.8% (13/16), read as chance
  • that A/B predates the parameter naming (1b73ccb0); it alone, 998ac03d against 1b73ccb0, same method, load 0.53 before: 44 of 45 no change, carbon_management +0.3%, unified -0.9%; the one slower, port-pypsa_stochastic +3.4% (3/16), read as chance
  • what the parameter naming skips, counted in presences_of over one build: unified 31 parameter presences asked, 21 kept, so 10 semi joins fewer at row indexing; carbon_management 3 asked, 3 kept; not counted inside propagate_absence

Gates

  • pixi run check on 1b73ccb0: 7013 passed, 995 skipped, 4 xfailed
  • not run: docs-build, test-bench, --sizes s below above

Not done

  • _zero_outside's narrowed(…, region) over every dimension of the carrier, a different key from _coordinates's

Generated by Claude Code

claude and others added 30 commits November 28, 2026 16:53
… along

A `cases:` region over some of a term's dimensions widens each presence the
term carries by the coordinates the region leaves out. Keyed by every
dimension, that widening reaches their full product: 1,881,936 rows for each
`name`-keyed region of `pypsa-carbon_management_unified`. A variable is now
present by the dimensions its `where` reads, and a `missing: absent`
parameter by those its rows leave a hole along.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA1kHCvCqjJyD7nXF4E
… builds faster

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA1kHCvCqjJyD7nXF4E
…s complement

A presence a region relaxes held the region's complement written out: the
product of the region's dimensions less its few claimed rows, widened to the
presence's columns. It now keeps those claimed rows beside it, and a filter
admits a row inside the presence or outside a region's claim. An operator
that moves coordinates, a shift or a relation, writes the complement out
first, since a complement does not survive a map of several coordinates to
one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA1kHCvCqjJyD7nXF4E
…ilds faster

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA1kHCvCqjJyD7nXF4E
A named expression two entries read is built once and held in memory, its
presences and their claims with it. A condition several `cases:` regions
test is read once, and the rows a `where` admits are read once for every
constraint that shares it, a constraint's presences narrowing those rows
rather than the condition being read again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA1kHCvCqjJyD7nXF4E
…e per build

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA1kHCvCqjJyD7nXF4E
`_reads` is `_read_counts`, `_held` is `_collected`, and `_admitted_rows` is
`_where_rows`. The changelog line names the change as its pull request does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA1kHCvCqjJyD7nXF4E
…xpression builds faster

The line names the change in the language's words, as its pull request does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA1kHCvCqjJyD7nXF4E
`Presence.materialized` is `Presence.written_out`, as its docstring already
says it, and the changelog lines of #2267 and #2268 name their changes in
the language's words, as their pull requests do. Merges #2267's branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA1kHCvCqjJyD7nXF4E
…laimed' into claude/funny-thompson-fc33p4-shared

# Conflicts:
#	CHANGELOG.md
The test this branch added to tests/test_queries.py stands in tests/internals/engine/test_queries.py, where main moved the module.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA31LM5B1nAaV1HUXet
…unclaimed

Brings main in. The test this branch added to tests/test_queries.py stands in tests/internals/engine/test_queries.py.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA31LM5B1nAaV1HUXet
…on-fc33p4-shared

Brings main in. The tests this branch added to tests/test_queries.py stand in tests/internals/engine/test_queries.py.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA31LM5B1nAaV1HUXet
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3n9zRJuf1H4Qr7wpkDsY
…unclaimed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3n9zRJuf1H4Qr7wpkDsY
…on-fc33p4-shared

main's narrowed_by, from #2161, narrows a where's admitted rows by the presences as narrowed_rows did, and also keys a constraint with no dimensions; narrowed_rows goes. The rows a where admits are still read once per build through _where_rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3n9zRJuf1H4Qr7wpkDsY
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3n9zRJuf1H4Qr7wpkDsY
…unclaimed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3n9zRJuf1H4Qr7wpkDsY
…on-fc33p4-shared

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3n9zRJuf1H4Qr7wpkDsY
…-fc33p4-shared

# Conflicts:
#	CHANGELOG.md
#	src/specsolve/_engine/queries.py
#	tests/internals/engine/test_queries.py
… built with

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA3y1UzriCWf2vF5v7X
…-fc33p4-shared

# Conflicts:
#	CHANGELOG.md
#	src/specsolve/_engine/queries.py
#	tests/internals/engine/test_queries.py
…s variables share with other reads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA3y1UzriCWf2vF5v7X
…named by its variable

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA3y1UzriCWf2vF5v7X
…ts presence is built

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA3y1UzriCWf2vF5v7X
…ches

# Conflicts:
#	src/specsolve/_engine/assembly.py
#	src/specsolve/_engine/queries.py
#	tests/internals/engine/test_queries.py
@coderabbitai

coderabbitai Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 6 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cc1fc48b-9191-4b11-992a-f43c2b47d8cc
Commits

Reviewing files that changed from the base of the PR and between 721cdd6 and 1b73ccb.

Files selected for processing: 4

  • src/specsolve/_engine/assembly.py
  • src/specsolve/_engine/queries.py
  • src/specsolve/_engine/terms.py
  • tests/internals/engine/test_queries.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@read-the-docs-community

read-the-docs-community Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

📖 2 files modified


Preview · build overview · specsolve · built Dec. 6, 2026, 1:04 p.m. UTC

@FBumann
FBumann enabled auto-merge (squash) December 6, 2026 12:37
@FBumann
FBumann disabled auto-merge December 6, 2026 12:38
…ows its rows by it once

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018B3nA3y1UzriCWf2vF5v7X

This branch has not been deployed

No deployments
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.

2 participants