Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 185 additions & 1 deletion checker_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,30 @@
a Deduce program is valid except by reporting cache hits and misses.
"""

from typing import cast
import copy
from typing import TypeVar, cast

from lark.tree import Meta

from abstract_syntax import (
Assert,
Auto,
ConceptDecl,
Define,
GenRecFun,
Import,
ModelDecl,
OverloadedVar,
Postulate,
PostulateFun,
PostulateType,
Predicate,
Print,
PVar,
RecFun,
Statement,
Theorem,
TypeAlias,
Union,
VarRef,
ViewDecl,
Expand Down Expand Up @@ -75,6 +89,7 @@ def reset_stmt_cache() -> None:
global _stmt_cache
_stmt_cache.clear()
_prelude_imports_cache.clear()
_type_check_cache.clear()
_cache_stats["hits"].clear()
_cache_stats["misses"].clear()

Expand Down Expand Up @@ -168,6 +183,11 @@ def _collect_referenced_names(
for v in cast(dict[object, object], node).values():
_collect_referenced_names(v, out)
return out
if isinstance(node, OverloadedVar):
# Before type checking, any of the candidates may be the one
# chosen, so the statement depends on all of them (#1301).
out.update(node.resolved_names)
return out
if isinstance(node, VarRef):
out.add(node.get_name())
return out
Expand Down Expand Up @@ -234,3 +254,167 @@ def _is_global_barrier(stmt: Statement) -> bool:
and models (#1264): a where formula names its concepts by string,
and a model is found by its concept and type, never by name."""
return isinstance(stmt, (Import, Auto, ConceptDecl, ModelDecl))


# ---------------------------------------------------------------------------
# Per-statement type-check cache (#1301)
# ---------------------------------------------------------------------------
#
# ``type_check_stmt`` checks each statement against the env that
# ``process_declaration`` built from the whole file, and returns the
# checked statement. For each module, ``_type_check_cache`` holds the
# checked statements of its last check, keyed by ``(stmt_hash,
# deps_fingerprint)`` (see ``type_check_fingerprints``), with the
# statement's layout (``statement_layout``) and first position. Each check
# replaces the module's entries with the ones it used or created, so
# the cache holds one file's worth per module.
#
# A checked statement carries its source locations, which later phases
# report and ``goal_at`` matches against. On a hit for a statement that
# only moved, ``_relocated`` shifts the cached statement's locations to
# where it now is. Locations it embeds from other statements (say, the
# type of a function it calls) are left as they were.

# (layout, (first line, first position), checked statement)
T = TypeVar("T")
_TypeCheckEntry = tuple[int, tuple[int, int], Statement]
_type_check_cache: dict[str, dict[tuple[int, int], _TypeCheckEntry]] = {}

# Statements whose effect on type checking other statements goes only
# through the names they define. Every other kind is a barrier: views
# and models are found by type, associativity by operator and type,
# imports bring in whole modules, and so on.
_NAME_ONLY_STATEMENTS = (
Assert, Define, GenRecFun, Postulate, PostulateFun, PostulateType,
Predicate, Print, RecFun, Theorem, TypeAlias, Union, ViewRecFun,
)

# The statements whose type check is cached: the ones that do real
# work in ``type_check_stmt`` and have no side effect there.
TYPE_CHECK_CACHED_STATEMENTS = (
Assert, Define, GenRecFun, Postulate, Print, RecFun, Theorem, ViewRecFun,
)


def type_check_fingerprints(stmts: list[Statement],
hashes: list[int]) -> list[int]:
"""For each statement, a fingerprint of the statements its type
check depends on: the ones defining a name it mentions, what those
depend on in turn (a function's type can come from a type alias it
mentions), and every barrier statement in the file (the env is the
whole file's)."""
defined_to_idx: dict[str, int] = {}
for i, s in enumerate(stmts):
for n in _collect_defined_names(s):
defined_to_idx.setdefault(n, i)
barriers = hash(tuple(h for s, h in zip(stmts, hashes)
if not isinstance(s, _NAME_ONLY_STATEMENTS)))
deps_fps: list[int] = []
full_fps: list[int] = [] # a statement's hash folded with its deps
for i, s in enumerate(stmts):
deps = {defined_to_idx[n] for n in _collect_referenced_names(s)
if n in defined_to_idx} - {i}
fp = hash((barriers, tuple(full_fps[j] if j < i else hashes[j]
for j in sorted(deps))))
deps_fps.append(fp)
full_fps.append(hash((hashes[i], fp)))
return deps_fps


def statement_start(stmt: Statement) -> tuple[int, int]:
loc = stmt.location
return (loc.line, getattr(loc, "start_pos", 0))


def _metas(node: object, out: list[Meta]) -> list[Meta]:
"""Every location in ``node``, in a fixed order."""
if node is None or isinstance(node, (str, int, bool, float)):
return out
if isinstance(node, Meta):
out.append(node)
return out
if isinstance(node, (list, tuple)):
for x in cast(list[object] | tuple[object, ...], node):
_metas(x, out)
return out
if isinstance(node, dict):
for v in cast(dict[object, object], node).values():
_metas(v, out)
return out
if hasattr(node, "__dict__"):
for k, v in cast(dict[str, object], vars(node)).items():
if k != "__hash_cache__":
_metas(v, out)
return out


def statement_layout(stmt: Statement) -> int:
"""A hash of the statement's file and of its locations, relative to
where it starts when they are in its own file. Two statements with
the same hash (``_hash_ast``) and layout differ only in where they
are in the same file. (Two files with the same base name share a
module name, so the file must be part of it.)"""
filename = getattr(stmt.location, "filename", None)
line, pos = statement_start(stmt)
sig: list[tuple[object, ...]] = []
for m in _metas(stmt, []):
if m.empty:
sig.append(())
elif getattr(m, "filename", None) == filename:
sig.append((m.line - line, m.column, m.end_line - line,
m.end_column, getattr(m, "start_pos", 0) - pos,
getattr(m, "end_pos", 0) - pos))
else:
sig.append((getattr(m, "filename", None), m.line, m.column,
m.end_line, m.end_column))
return hash((filename, tuple(sig)))


def _relocated(node: T, old: Statement, old_first: tuple[int, int],
new_first: tuple[int, int]) -> T:
"""A copy of ``node``, part of the checked statement ``old`` that
started at ``old_first``, with ``old``'s own locations moved so that
it starts at ``new_first``."""
filename = getattr(old.location, "filename", None)
start, end = old_first[1], getattr(old.location, "end_pos", 0)
dline, dpos = new_first[0] - old_first[0], new_first[1] - old_first[1]
moved = copy.deepcopy(node)
seen: set[int] = set()
for m in _metas(moved, []):
if id(m) in seen or m.empty \
or getattr(m, "filename", None) != filename \
or not start <= getattr(m, "start_pos", -1) <= end:
continue
seen.add(id(m))
m.line += dline
m.end_line += dline
m.start_pos += dpos
m.end_pos += dpos
return moved


def lookup_type_check(module_name: str, key: tuple[int, int], layout: int,
stmt: Statement) -> Statement | None:
"""The cached type check of ``stmt``, given its key and layout,
moved to where ``stmt`` is now."""
entry = _type_check_cache.get(module_name, {}).get(key)
Comment thread
jsiek marked this conversation as resolved.
Resolved
if entry is None or entry[0] != layout:
return None
_, old_first, checked = entry
first = statement_start(stmt)
if isinstance(checked, Theorem) and isinstance(stmt, Theorem):
# Type checking passes the proof through, so take it from
# ``stmt``, which is already where it belongs.
what = checked.what if first == old_first \
else _relocated(checked.what, checked, old_first, first)
return Theorem(stmt.location, checked.name, what, stmt.proof,
checked.isLemma, visibility=checked.visibility)
if first != old_first:
checked = _relocated(checked, checked, old_first, first)
return checked


def store_type_checks(module_name: str,
entries: dict[tuple[int, int], _TypeCheckEntry]) -> None:
"""Replace the module's cached type checks with ``entries``."""
_type_check_cache[module_name] = entries
57 changes: 45 additions & 12 deletions checker_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
print_theorems, register_library_name, type_match, type_names,
)
from checker_cache import (
_collect_defined_names, _collect_referenced_names, _hash_ast,
_is_global_barrier, _prelude_imports_cache, _record_hit, _record_miss,
_stmt_cache,
TYPE_CHECK_CACHED_STATEMENTS, _TypeCheckEntry, _collect_defined_names,
_collect_referenced_names, _hash_ast, _is_global_barrier,
_prelude_imports_cache, _record_hit, _record_miss, _stmt_cache,
lookup_type_check, statement_layout, statement_start, store_type_checks,
type_check_fingerprints,
)
from checker_common import *
from checker_predicates import (
Expand Down Expand Up @@ -2517,17 +2519,52 @@ def _collect_diagnostic(exc: Diagnostic) -> None:
ast3_hashes = []

error_on_next_import : dict[str, bool] = {}
for s, sh in ast2_pairs:
# Per-statement type-check cache (#1301). The postulate report records
# what each type check uses, so it needs every one to run.
use_type_check_cache = not get_postulate_report()
fingerprints = type_check_fingerprints(
[s for s, _ in ast2_pairs], [sh for _, sh in ast2_pairs]) \
if use_type_check_cache else []
type_checks: dict[tuple[int, int], _TypeCheckEntry] = {}
for i, (s, sh) in enumerate(ast2_pairs):
cacheable = use_type_check_cache \
and isinstance(s, TYPE_CHECK_CACHED_STATEMENTS) and not s.location.empty
if cacheable:
tc_key = (sh, fingerprints[i])
layout = statement_layout(s)
start = statement_start(s)
new_s = lookup_type_check(module_name, tc_key, layout, s)
if new_s is not None:
_record_hit("type_check")
type_checks[tc_key] = (layout, start, new_s)
ast3.append(new_s)
ast3_hashes.append(sh)
continue
_sink = get_active_sink()
_wsink = get_active_warning_sink()
pre_n = len(_sink) if _sink is not None else 0
pre_wn = len(_wsink) if _wsink is not None else 0
try:
new_s = type_check_stmt(s, env, error_on_next_import)
except Diagnostic as e:
_collect_diagnostic(e)
continue
if cacheable and new_s is not None:
_record_miss("type_check")
# As for ``check_proofs`` below: a statement whose check reported
# an error or a warning must be checked again to report it again.
_sink = get_active_sink()
_wsink = get_active_warning_sink()
if (_sink is None or len(_sink) == pre_n) \
and (_wsink is None or len(_wsink) == pre_wn):
type_checks[tc_key] = (layout, start, new_s)
# If None gets returned we want to remove the current statement
# Which is represented by not appending it to the new ast
if new_s != None:
ast3.append(new_s)
ast3_hashes.append(sh)
if use_type_check_cache:
store_type_checks(module_name, type_checks)
if get_verbose():
for s in ast3:
print(s)
Expand All @@ -2538,14 +2575,10 @@ def _collect_diagnostic(exc: Diagnostic) -> None:
if get_verbose() and needs_checking[0]:
print('checking ' + module_name)
# Per-statement cache for ``check_proofs`` (Steps 13 + 14).
# Earlier loops (``process_declaration``, ``type_check_stmt``)
# emit AST nodes whose ``Meta`` locations participate in
# side-effecting behaviour (e.g. the ``target_hole_location``
# flag used by ``goal_at`` to single out which `?` raises), so
# caching their outputs across calls would let stale locations
# leak into a new run. ``check_proofs`` itself is the bulk of
# the time and its only persistent effect is "verified" -- safe
# to cache.
# Its only persistent effect is "verified", so the cache stores
# just that. (The type-check cache above stores checked
# statements, so it has to move their locations; see
# ``checker_cache``.)
#
# Step 14: the cache key is ``(stmt_hash, deps_fingerprint,
# target, module_name)`` where ``deps_fingerprint`` folds in
Expand Down
1 change: 1 addition & 0 deletions docs/lsp-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Steps 15 and 18 are duals: Step 15 is **introduction** (template chosen by the *
- [x] **Faster lemma ranking (issue #1256, under the iPad umbrella #1213).** `available_lemmas_at` / `lemmas_for_subterm` took ~0.3 s per call on the Mac (~0.7 s on the iPad simulator), the slowest query at a hole. Three changes, with the same ranked results: `MatchFailed` renders its message only when it is read (`match_failed` takes a thunk), since ranking and `replace` discard almost every failed match and used to pretty-print two formulas for each one; `_prelude_lemma_candidates` caches the prelude's candidates and their `_formula_symbols`, reused while the uniquified prelude modules are the same objects; and `_equation_subterm_match` reduces a call-shaped equation side once instead of once per name or type subterm of the goal. At `length_append`'s `node` hole: 0.30 s → 0.027 s (0.095 s for the call that fills the cache).

- [x] **A worker thread for checks; cancel stale checks (issue #1218, under the iPad umbrella #1213).** Every check and request used to run on pygls's I/O loop, one after another: a slow check (or query) blocked everything behind it, and a burst of edits queued a check per edit. Now `lsp/worker.py`'s `Worker` runs them, in order, on one thread with a large stack (the pipeline's module-level state rules out running two at once), and the I/O loop only reads messages and sends results. A new version of a document supersedes its check: a queued check is dropped and a running one is interrupted with `PyThreadState_SetAsyncExc` (raising `CheckSuperseded`), so a burst of edits ends in one completed check, of the final text. `$/cancelRequest` drops a queued request or interrupts a running one (`RequestCancelled`), and `lsp_server.interrupt()` (the iPad's Stop button) interrupts whichever is running. Request handlers stay plain functions, registered through `_feature`. The other half of the issue, re-parsing only the statements that changed, is left for later: re-checks already take ~0.2 s. Since an interrupt can land inside cleanup code, the worker resets the pipeline's scoped state (`library._SCOPED_STATE`) before every job. Tests in `test/lsp/test_lsp_worker.py`, including the server over stdio, and `test/lsp/test_interrupt_fuzz.py`. The design and its rules are in `docs/knowledge-base/lsp-concurrency.md`.
- [x] **Cache type checking per statement (issue #1301).** After the `check_proofs` cache, `type_check_stmt` was the largest per-check cost that did not depend on what changed (20–42% of a warm re-check). `check_deduce` now caches each statement's checked form per module, keyed by its structural hash and `checker_cache.type_check_fingerprints`: the statements defining the names it mentions, *transitively* (a function's type can come from a type alias), with every candidate of an overloaded name, plus every statement that is not a plain named definition anywhere in the file (views, models, `associative`, imports, ... are found by type or have file-wide effects, and the env is the whole file's). A statement that only moved reuses its entry with its own locations shifted (`_relocated`; a theorem takes its proof from the fresh parse). Statements whose check reported an error or warning are not cached, and the postulate report bypasses the cache. Each check replaces the module's entries with the ones it used, so the cache holds one file per module. On `test/should-validate/IntTests.pf` a warm re-check went from 0.273 s to 0.167 s (0.184 s after inserting a line), on `doc_reference.pf` from 0.237 s to 0.203 s (0.218 s). Tests in `test/lsp/test_type_check_cache.py`.

- [x] **Using an `all` given with values (issue #1222, under the iPad umbrella #1213).** `eliminate_at` on an `all` given replaces the hole with `H[?]`, whose `?` is a placeholder term: rarely what's wanted. `instantiation_vars_at(path, content, pos, label, prelude=())` returns the variables of the given's first `all` block as frozen `InstantiationVar(name, type, suggestions)`: the suggestions are the goal's subterms of that type that are in scope at the hole (shortest first), then the variables in scope of that type. `instantiate_at(path, content, pos, label, values, prelude=())` returns the edit putting `have H: <formula> by label[values]` before the `?`, or the checker's message when the values don't check (a string). The formula is the checker's: `have H: ? by label[values]` is checked in place of the hole with the outline recorder on, and a `have` whose formula is `?` proves what its reason does. Exposed over the LSP as `deduce/instantiationVarsAt` and `deduce/instantiateAt`. `Term.free_vars` is now defined for every term (the union over its children, with `all`, `some`, `fun`, `define … in`, `generic` and `switch` cases subtracting what they bind). Tests in `test/lsp/test_instantiate.py`.

Expand Down
8 changes: 2 additions & 6 deletions test/lsp/test_check_proofs_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@
reference it as hits (Step 14's deps-fingerprint kicking in),
- editing T1 invalidates T2 that uses it (Step 14 acceptance).

The earlier two loops (``process_declaration``, ``type_check_stmt``)
are deliberately *not* cached -- their AST output carries
``Meta`` locations that interact with the
``target_hole_location`` flag used by ``goal_at`` and friends, so
caching them across calls would surface as stale-location bugs.
``check_proofs`` is where the bulk of the time goes anyway.
``type_check_stmt`` has a cache of its own (#1301), tested in
``test_type_check_cache.py``.
"""

from __future__ import annotations
Expand Down
3 changes: 2 additions & 1 deletion test/lsp/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def test_second_start_loads_the_snapshot_instead_of_checking_the_prelude(
# The statement cache saw only the user file's two statements (the
# prelude's `import Mini` and the theorem): Mini's own statements,
# checked in the first run, were never checked in this "process".
assert get_cache_stats()["misses"] == {"check_proofs": 2}
# (The type-check cache, #1301, saw only the theorem.)
assert get_cache_stats()["misses"] == {"check_proofs": 2, "type_check": 1}
assert first_misses > 2
assert list((tmp_path / "snapshots").glob("prelude-*.pickle")) == [snapshot]

Expand Down
Loading
Loading