Skip to content

[Lang] Memoise the per-node source-position banner on the function - #858

Merged
hughperkins merged 1 commit into
mainfrom
hp/lang-memo-pos-info
Aug 13, 2026
Merged

[Lang] Memoise the per-node source-position banner on the function#858
hughperkins merged 1 commit into
mainfrom
hp/lang-memo-pos-info

Conversation

@hughperkins

Copy link
Copy Markdown
Collaborator

What

ASTTransformerBase.__call__ attaches source-position info to every generated IR node via get_pos_info, which
TextWrapper-formats a source-line-plus-caret hint. That formatting therefore runs for every stmt/expr node of
every kernel compilation, and it dominates the Python side of a kernel build.

A function is transformed once per inlined call site -- tens of times over on a large kernel -- and each transform
re-formats the identical positions. This memoises the result. The string produced is byte-identical, so every
diagnostic that embeds it is unchanged.

The cache lives on the FuncBase rather than in a module-level dict, because the FuncBase is exactly what fixes
the remaining inputs: file, src, indent, lineno_offset and the function name all derive from it and are
identical across its transforms, so the key is just the node's position and type. That also bounds the cache by the
function's lifetime and keeps it correct across a module reload, which hands out new FuncBase objects.

Impact

On a kernel-build-heavy workload (a qipc solver step, ~130 offloaded tasks), timing the first step after editing
one kernel, interleaved over a single unchanged build:

rep 1 rep 2
before 10.22 s 10.17 s
after 5.60 s 5.59 s

get_pos_info is called 277k times in that run and the memo serves 97.4% of them.

Relationship to #804

#804 attacks the same hotspot by attaching only a cheap File "...", line N header per node and deferring the full
hint to the compile-error path. That is a smaller change and I tried it first (#857), but it is not
behaviour-preserving: the per-node info is not read only by compile errors, it is embedded in DebugInfo and
surfaces in runtime diagnostics. test_overflow.py asserts the offending source line appears in the overflow
message and 64 of its cases fail without it:

assert 'return a + b' in 'Addition overflow detected in File ".../test_overflow.py", line 64, in foo:\n\n'

Memoising avoids that by construction, since the string never changes. test_overflow.py is green here.

#804's other change, replacing the O(len(sys.modules)) scan in _inside_class with a linecache lookup, is
independent of this and still worth landing on its own.

Testing

test_overflow.py, test_exception.py, test_syntax_errors.py, test_nested_kernel_error.py and
test_ast_refactor.py -- the suites that assert on message text -- give 291 passed both with and without the
change, on the same build.

Made with Cursor

ASTTransformerBase.__call__ attaches source-position info to every generated IR node via get_pos_info, which
TextWrapper-formats a source-line-plus-caret hint. That ran for every stmt/expr node of every kernel compilation
and dominated the Python side of a kernel build.

A function is transformed once per inlined call site, so the same positions are formatted tens of times over.
Memoise the result on the FuncBase, which is what fixes every input beyond the node itself, leaving the produced
string byte-identical.

On a kernel-build-heavy workload (~130 offloaded tasks), the first step after editing one kernel goes from ~10.6s
to ~6.1s. get_pos_info is called 277k times there and the memo serves 97.4% of them.
@github-actions

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

@hughperkins

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: a6a0e8aad2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@hughperkins

Copy link
Copy Markdown
Collaborator Author

running genesis unit tests and bench

@hughperkins

Copy link
Copy Markdown
Collaborator Author

Genesis benchmarks look ok:

20260813-memorize-0850

@hughperkins

Copy link
Copy Markdown
Collaborator Author

Genesis unit tests ok:

Screenshot 2026-08-13 at 09 34 49

@hughperkins
hughperkins merged commit 895dd5e into main Aug 13, 2026
87 of 88 checks passed
@hughperkins
hughperkins deleted the hp/lang-memo-pos-info branch August 13, 2026 13:35
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.

1 participant