Repository navigation
Evaluate an expression against the globals a program left (#680) - #681
bertysentry wants to merge 4 commits into
Conversation
AVM.evalPersistingGlobals(expression, input) evaluates a compiled expression against the globals that the AVM retains, typically those left by executePersistingGlobals(): scalars and arrays, read by name. What the expression assigns stays. The retained globals are not copied. The expression runs on a frame of its own layout, where only the variables it uses are bound to the retained values of the same name, and stored back afterwards: the cost of an evaluation does not depend on how many globals are retained. getVariable() now reads a retained global by name when the last script has no slot for it, and no longer fails after clearPersistentGlobals() or restorePersistentMemory(), whose globals the offsets of the last script did not address any more. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e50133b2b7
ℹ️ 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".
- Tests: the cases that run a script, then expressions on its globals, go through AwkTestSupport, which gains evalPersistingGlobals(expression, record). The other tests exercise the AVM methods around a persisting evaluation and run no script: they start from a restored snapshot. - SYMTAB also holds the retained globals that the running script does not name, linked to their values, for a persisting evaluation as for a persisting execution. - getGlobalVariableNames() keeps answering the names of the last script after its layout is uninstalled. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx
|
@codex please review again Generated by Claude Code |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3f6b565264
ℹ️ 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".
- A global that an expression leaves uninitialized is retained too: it may have been assigned (x = unset), and what an expression assigns stays. Only a slot that was never read nor assigned adds nothing, as a persisting execution retains every global of its program. - SYMTAB refuses to replace a retained array with a scalar, or a retained scalar with an array, also when the running script does not name that global: its type is then the type of its retained value. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx
|
@codex please review again Generated by Claude Code |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: babae61408
ℹ️ 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".
| List<Integer> offsets = new ArrayList<>(); | ||
| for (Map.Entry<String, Integer> entry : offsetsByName.entrySet()) { | ||
| frameLength = Math.max(frameLength, entry.getValue().intValue() + 1); | ||
| if (isPersistentEligibleGlobal(entry.getKey())) { |
There was a problem hiding this comment.
Honor the expression's POSIX mode when retaining globals
When an expression was compiled in POSIX mode but the shared AwkSettings is switched back to non-POSIX before creating or using the AVM, this eligibility check treats ERRNO and ARGIND as managed specials even though the compiled expression addresses them as ordinary global slots. For example, a POSIX-compiled (ERRNO = 5) evaluates successfully, but ERRNO is omitted from this binding and its assigned value is discarded during unbinding. Determine eligibility from compiledExpression.isPosix() rather than the AVM's current mutable settings.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Traced, and left as it is in this PR. The observation is right: isManagedSpecialVariable() reads settings.isPosix(), the mode of the AVM, so an expression compiled with --posix and evaluated on a non-POSIX AVM does not get its ERRNO / ARGIND slots bound or stored back.
But this is not specific to evalPersistingGlobals(). Everything that decides whether these two names are special uses the mode of the AVM, not the mode of the compiled script: executePersistingGlobals() (collectPersistentGlobalValues() drops them the same way in that mixed setup), snapshotPersistentMemory(), getVariable(), assignVariable() and SYMTAB. Using compiledExpression.isPosix() in the binding alone would retain an ERRNO that the snapshot then drops and that getVariable("ERRNO") answers from the JRT instead, which is less consistent than today. Making the mode follow the compiled script everywhere is a change to existing behavior beyond #680.
Leaving this thread open for @bertysentry to decide whether that mixed-mode case deserves its own issue.
Generated by Claude Code
Keeps both Unreleased bullets of behavior-changes.md: #680 and #679. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 978575f585
ℹ️ 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".
| if (array == null && isRetainedGlobal(name)) { | ||
| // the script does not name this retained global: its value tells its type | ||
| Object retained = runtimeStack.getGlobalVariable(name); | ||
| if (retained instanceof Map) { | ||
| array = Boolean.TRUE; |
There was a problem hiding this comment.
Preserve types for unmaterialized retained globals
The fresh case not covered by the earlier SYMTAB fix is a declared global whose slot was never materialized: after a persisting program such as BEGIN { if (0) temp[1] = 1 }, temp is known statically to be an array but its retained value is null. A later expression such as (SYMTAB[$1] = $2) with $1 == "temp" reaches this branch, cannot infer the retained type from the value, and therefore replaces the array global with a scalar; the next expression or program that uses temp[...] then fails. Retain the prior layout's scalar/array metadata alongside the value rather than relying only on instanceof Map.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Traced, and not changed in this PR. The sequence is right: after BEGIN { if (0) temp[1] = 1 }, temp is retained with no value, so (SYMTAB[$1] = $2) with $1 == "temp" makes it a scalar, and a later script that uses temp[...] fails.
But the retained globals have never carried the static type of the script that declared them: a retained global is typed by its value only. The same already holds between two persisting executions on main: after that program, executePersistingGlobals() of BEGIN { temp = 5 } makes temp a scalar, and running the first program again then fails on temp[...]. An unassigned retained global is untyped, and the first script that assigns it gives it its type, which is also how gawk treats a variable that was never used. The SYMTAB path of this PR follows that rule.
Keeping the scalar/array declaration of every script next to the retained values (and deciding what happens when two scripts declare the same unassigned name differently) would be a change to the persistent memory itself, beyond #680.
Leaving this thread open for the maintainer to decide whether that is wanted.
Generated by Claude Code
Fixes #680.
What
AVM.evalPersistingGlobals(expression, input)evaluates a compiled expression against the globals that theAVMretains, typically those a program left withexecutePersistingGlobals():SYMTAB. A name it uses and that no run has set is uninitialized.executePersistingGlobals()), also when the evaluation fails after the assignment. Every global that the expression read or assigned is retained afterwards, as a persisting execution retains every global of its program (a name that was only read is retained uninitialized).NR,NF,FS, ...) start from their defaults, as witheval().ARGV,ARGCandENVIRONare not retained.clearPersistentGlobals()ends it, as today. A plaineval()/execute()still resets everything.There is also an overload with per-call overrides,
evalPersistingGlobals(expression, input, Map), to mirroreval(expression, input, Map). It was not asked for in the issue: say so if you would rather not have it.How
The retained globals are not copied. The expression runs on a small frame of its own layout:
RuntimeStack.bindGlobalFrame(frame)keeps the retained globals aside and makes the frame the globals that the tuples address by offset. Only the slots of the user-defined globals of the expression are filled, from the retained values of the same name.finally),unbindGlobalFrame()puts the retained globals back, and the slots of the expression are stored back by name (retainGlobalVariable, which appends a slot for a name that was used for the first time).Which slots of an expression are bound is computed once per expression and per
AVM(aWeakHashMap, identity keys). The cost of an evaluation therefore depends on the number of variables the expression uses, not on the number of retained globals: a rough timing loop on my side gave about 1.1 to 1.2 µs per evaluation with 5 retained globals as with 200, against about 1.0 µs for a plaineval()of a comparable field-only expression. Not a JMH measurement.Also in this PR
After the evaluation, the offsets of the expression no longer address the retained globals, so the layout is uninstalled and
getVariable()reads the retained globals by name. That made two existing problems visible, which are fixed here with the same reset:getVariable(name)afterclearPersistentGlobals()threw aNullPointerException.getVariable(name)afterrestorePersistentMemory()read the slot of another variable, or threw anArrayIndexOutOfBoundsException.Two more changes follow from reading retained globals by name:
getVariable()andSYMTABalso give a retained global that the running script does not name, andSYMTABkeeps its type (an array cannot be replaced with a scalar). This changesexecutePersistingGlobals()too: a program used to see inSYMTABonly the globals it named itself.getGlobalVariableNames()keeps answering the names of the last script after its layout is uninstalled.Tests and docs
AwkEvalPersistingGlobalsTest(29 tests) andRuntimeStackGlobalFrameTest(8 tests). The tests that run a script and then expressions on its globals go throughAwkTestSupport, which gainsevalPersistingGlobals(expression, record); the others run no script and call theAVMmethods directly.java-advanced.md(new section),java-compile.md(table row),behavior-changes.md(Unreleased bullet).mvn clean verify site: BUILD SUCCESS, 1056 unit tests pass, Checkstyle / PMD / SpotBugs at 0.Notes
RuntimeStack,AVM.execSetNumGlobalsandAwkTestSupport. Whichever of the two merges second needs a merge ofmain.compileExpression()does not parse theinoperator ($1 in arr), with or without this PR. Not addressed here.🤖 Generated with Claude Code
https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx