Skip to content

Type-checker: memoise type checks on common subexpressions (str_concat / format) #16

Closed

Description

@hyperpolymath

Follow-up to #1 / #12.

This is the other suggested fix direction from the original OOM report:

Memoise type checks on common subexpressions (especially repeated str_concat/format AST nodes).

PR #12 implemented the depth guard but deliberately did not attempt memoisation. This issue tracks the optional, larger refactor.

Why this is non-trivial

  • The internal type Ty is structurally Clone-able and small, but Expr is not currently Hash/Eq-keyable on its own (spans differ even for structurally-equal expressions; lambdas capture environment).
  • A naive HashMap<*const Expr, Ty> keyed by pointer identity would help for literally shared subexpressions but not for structurally identical re-occurrences of the same format("...", [name, lang]) across different functions.
  • A content-addressed key (hash of structural form, ignoring spans) is more useful but requires a careful definition of structural equality.

What this issue tracks

  1. Determine whether the real-world workload from Type-checker OOM (16-32 GB allocation) on deeply-nested str_concat / format chains #1 actually has many structurally identical subexpressions, or whether each format(...) call site is unique. (Depends on the root-cause investigation issue.)
  2. If memoisation is justified by data:
    • Add a Hash + Eq implementation on a span-stripped projection of Expr.
    • Cache Ty results in the Checker keyed by that projection.
    • Bench against the original repro.
  3. If memoisation is not justified by data, close this issue and rely on the depth guard.

Related

Out of scope

  • Generalised CSE in HIR/MIR (that's a backend concern; this issue is purely about the checker's recursive cost).

Activity

  1. added
    enhancement
    performance
    on May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions