Skip to content

Build the global layout once per compiled expression or program - #684

Open
bertysentry wants to merge 2 commits into
mainfrom
claude/issue-678-global-layout-once
Open

bertysentry wants to merge 2 commits into
mainfrom
claude/issue-678-global-layout-once

Conversation

@bertysentry

@bertysentry bertysentry commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #678

Problem

Every eval() / execute() on a reused AVM rebuilt the global layout in RuntimeStack.setGlobalLayoutMetadata: a new Object[], an ArrayList of names (copied twice) and a LinkedHashMap of offsets, all from the same offset map as the previous run of the same expression.

Change

  • GlobalVariableLayout (new, io.jawk.intermediate): immutable names-by-offset list + offsets-by-name map. Runtime plumbing between the tuple stream and the interpreter, not an API for embedders (stated in its Javadoc).
  • AwkTuples: builds the layout once in freezeMetadata(), before the compiled program/expression is exposed, so every AVM on every thread gets the same instance with nothing lazy to race on. The field is transient; readObject rebuilds it for a deserialized program. getGlobalVariableLayout(int numGlobals) returns it.
  • RuntimeStack: setNumGlobals(layout) binds the shared layout instead of building one. clearGlobals() clears the slots of a compiled-layout array (Arrays.fill(globals, null)) and keeps it; the next setNumGlobals reuses it when it gets the same layout instance, and allocates otherwise.
  • AVM: remembers the installed compiled tuples (installedTuples) and asks them for the layout in execSetNumGlobals.

The persistent-globals path is not changed: rebindGlobals(List<String>) still builds its own merged by-name layout, and such arrays are never reused or cleared.

Nothing visible changes: every plain run still starts with uninitialized globals. No behavior-changes.md entry (pure performance change).

Tests

  • AwkTestSupport gains runsOnSharedRuntime(...) with evalRun / evalAgainRun / executeRun / persistentRun steps, to chain evaluations, executions and persistent executions on one reused runtime.
  • GlobalLayoutReuseTest (all through AwkTestSupport): repeated eval of the same expression (scalars and arrays), eval continuing on the same eval state, alternating two expressions, execute after eval and eval after execute, repeated execute, pre-assigned globals re-applied on each run, persistent execution still carrying globals, eval after persistent executions.
  • RuntimeStackTest (plain JUnit): layout binding, array reuse for the same layout instance, new array for another layout, rebound (merged) globals never reused.
  • GlobalVariableLayoutTest (plain JUnit): layout construction, read-only views, one instance per compiled expression/program including concurrent first uses, no caching before the metadata is frozen, rebuild after a serialization round trip.

mvn verify is green locally (unit tests, checkstyle, PMD, SpotBugs, license headers, Javadoc); the compatibility-suite results are identical to main.

Note for the sibling PRs

AVM.java changes are limited to one field, one line in each of resetTransientRuntimeState / installExpressionMetadata / installProgramMetadata, and the setNumGlobals call in execSetNumGlobals. AwkTestSupport only gets additions.

🤖 Generated with Claude Code

https://claude.ai/code/session_012oN698mGzDdiNqivAHYLdk

The names-by-offset list and the offsets-by-name map of the globals were
rebuilt by RuntimeStack on every eval() and execute(), from the same
offset map as the previous run. They are now built once per compiled
AwkExpression/AwkProgram, as an immutable GlobalVariableLayout that every
AVM shares, and the runtime stack keeps its (cleared) globals array when
the next run uses the same layout.

Every plain run still starts with uninitialized globals, and the
persistent-globals path, which binds a merged layout by name, is unchanged.

Fixes #678

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012oN698mGzDdiNqivAHYLdk
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 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-29T08:31:36.177453Z 42f7ff5 Manual request
ℹ️ 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: 5a8cb1ab6c

ℹ️ 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".

* every runtime that executes it, on any thread.
* </p>
*/
public final class GlobalVariableLayout {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Document or hide the new global-layout API

This commit exposes GlobalVariableLayout, its factory and accessors, and the inherited public AwkTuples.getGlobalVariableLayout method without updating either the site documentation or README. Either keep this implementation detail out of the public surface or document its supported contract in the required user-facing documentation.

AGENTS.md reference: AGENTS.md:L35-L37

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Traced this: the type cannot be hidden, because the cache lives on io.jawk.intermediate.AwkTuples and is consumed by io.jawk.backend.RuntimeStack/AVM (two packages, Java 8, no modules). It sits next to getGlobalVariableOffsetMap() / getGlobalVariableAarrayMap(), and nothing of io.jawk.intermediate is described in src/site or the README today, which document the embedding API (Awk, AwkProgram, AwkExpression, AVM, CLI). So rather than advertise interpreter plumbing as a supported contract, 42f7ff5 states in the Javadoc of GlobalVariableLayout and AwkTuples.getGlobalVariableLayout that they are runtime plumbing, not an API for embedders. Leaving this thread open for @bertysentry to decide whether a site-doc entry is wanted anyway.


Generated by Claude Code

…Support

- Build the global layout when the compiled metadata is frozen (and again
  on deserialization) instead of lazily, so every runtime gets the same
  instance without any race.
- Run the new AWK-script tests through AwkTestSupport, extended with
  runsOnSharedRuntime(...) to chain evaluations, executions and persistent
  executions on one reused runtime.
- State in the Javadoc that the global-layout types are runtime plumbing,
  not an API for embedders.

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

Copy link
Copy Markdown
Contributor Author

@codex please review again


Generated by Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 42f7ff5a00

ℹ️ 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".

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.

Build the global layout once per compiled expression, not on every eval

2 participants