Skip to content

Cache type checking per statement (#1301) - #1305

Merged
jsiek merged 2 commits into
mainfrom
issue-1301-typecheck-cache
Nov 20, 2026
Merged

jsiek merged 2 commits into
mainfrom
issue-1301-typecheck-cache

Conversation

@jsiek

@jsiek jsiek commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Fixes #1301.

Example

checker_cache.get_cache_stats() on a warm LSP re-check of test/should-validate/IntTests.pf:

before:  check_proofs hits 84 / misses 55, type_check_stmt runs on every statement (0.116 s)
after:   type_check hits 77 / misses 0, check_proofs hits 84 / misses 55

Warm lsp.query.check times (steady-state runs, no profiler):

File Before After, unchanged After, a line inserted mid-file
test/should-validate/IntTests.pf 0.273 s 0.167 s 0.184 s
test/should-validate/doc_reference.pf 0.237 s 0.203 s 0.218 s

How it works

check_deduce now keeps, per module, the type-checked statements of its last check (checker_cache._type_check_cache).

Key: the statement's structural hash (_hash_ast, location-free) and type_check_fingerprints, which folds in:

  • the statements defining the names it mentions, transitively. If fun f(x : M) uses type M = N and M changes to K, then a theorem that only mentions f is re-checked;
  • every candidate of an overloaded name. _collect_referenced_names now adds all of OverloadedVar.resolved_names; before, it added only the first;
  • every statement in the file that is not a plain named definition (views, models, associative, auto, imports, ...). Those are found by type or have file-wide effects, and type checking uses the whole file's env.

Moved statements: the entry also stores the statement's layout (its locations relative to where it starts) and its start. If a statement has the same hash and layout but starts elsewhere, _relocated shifts the cached statement's own locations (lines and character positions) to where it is now. A theorem takes its proof from the fresh parse, since type checking passes proofs through unchanged.

Not cached:

  • statements whose check reported an error or a warning, so it is reported again;
  • anything while the postulate report is on;
  • kinds whose type check is trivial or has side effects (imports, imperative declarations, ...).

Memory: each check replaces the module's entries with the ones it used or created, so the cache holds one file per module.

Known limitations

  • Locations a cached statement embeds from other statements (say, the type of a function it calls) are not shifted. If those moved by a different amount, the statement's layout differs and it is simply checked again; this is pinned by test_moving_statements_apart_rechecks_one_carrying_anothers_location.
  • Inserting or deleting a top-level statement renames every later statement in uniquify, so all of them miss, in this cache and in the check_proofs cache. Filed as Inserting a top-level statement renames every later statement, defeating the per-statement caches #1303.

Tests

New test/lsp/test_type_check_cache.py covers:

  • an unchanged re-check;
  • moved statements;
  • one edited theorem;
  • an edited function and its users;
  • a type alias reached transitively;
  • every overload as a dependency;
  • a barrier (auto);
  • diagnostics of moved statements, compared with a cold check, for line and column shifts;
  • goal_at in a moved theorem.

test/lsp/test_snapshot.py now expects the type-check miss in its exact stats.

make static, test-deduce.py (260 s, the same as before, so no cost for CLI runs) and pytest test/lsp test/unit (1319 passed after rebasing on #1300) all pass.

🤖 Generated with Claude Code

check_deduce caches each statement's type check per module, keyed by
its structural hash and a fingerprint of what it depends on: the
statements defining the names it mentions (transitively, and every
candidate of an overloaded name) plus every statement that is not a
plain named definition (views, models, associative, imports, ...). A
statement that only moved reuses its entry with its own locations
shifted. Statements whose check reported an error or warning are not
cached, and the postulate report bypasses the cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jsiek
jsiek marked this pull request as ready for review November 20, 2026 13:47
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T13:51:20.980747Z c28eec1 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c28eec1e90

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread checker_cache.py Resolved
Two files with the same base name share a module name, so a check of
one could reuse the other's checked statements, which still name the
first file (Codex review on #1305).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jsiek
jsiek merged commit 086aa06 into main Nov 20, 2026
15 checks passed
@jsiek
jsiek deleted the issue-1301-typecheck-cache branch November 20, 2026 14:12
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.

Incremental: cache type checking per statement in the LSP

1 participant