Title: Unbox non-escaping Ref/STRef cells to mutable Lua locals by Unisay · Pull Request #315 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Unbox non-escaping Ref/STRef cells to mutable Lua locals by Unisay · Pull Request #315 · purescript-lua/purescript-lua
X Title: Unbox non-escaping Ref/STRef cells to mutable Lua locals by Unisay · Pull Request #315 · purescript-lua/purescript-lua
Description: Closes #239. A Ref/STRef cell compiles to a one-field heap table: the foreign new allocates {value = v}, and every read/write/modify pays a table field access. This PR adds a codegen-time pass (Language.PureScript.Backend.Lua.RefUnbox) that lowers a non-escaping cell to a plain mutable Lua local: new v becomes local r = v, a read run becomes r itself, and write/modify runs become assignments. The load-bearing observation is that a Lua local captured by inner closures is an upvalue — a shared, mutable heap slot with exactly the aliasing behaviour of the {value = …} table — so occurrences under lambdas (loop bodies, nested magic-do chunks) need no special treatment, and the only thing the table provides that a local cannot is first-class identity. The analysis therefore asks one question per cell: is every occurrence the reference argument of a recognised operation? Any other occurrence (stored, returned, passed to an unknown function) keeps the cell boxed, so a miss is a missed optimisation, never a miscompile. What lowers. Recognition is by qualified name — the identity that survives linking, the same scheme magic-do and the native-loop matcher use — over the cell primitives of Effect.Ref and Control.Monad.ST.Internal (_new/new, read, write, modifyImpl) plus the ST functor's foreign map_ as a wrapper, because void (STRef.modify f r) (the ubiquitous modify_ shape) inlines to map_ (\_ → unit) (modifyImpl f r). Only run positions lower (the application carries magic-do's EffectRunArg marker, or is the body of a literal loop lambda that NativeLoop.runStatements runs per iteration); an operation thunk used as a first-class value keeps the cell boxed. A literal modify function whose body ends in a manifest {state, value} record is beta-reduced at emission — parameter bound to the local, Let spine emitted as statements, state field assigned, value field passed on — so the per-iteration record is never allocated either. Before/after, the ST accumulator loop from Golden.NativeLoopsST.Test (quoted verbatim from the committed goldens): local acc = Control_Monad_ST_Internal_new(0)() for i = 0, n + 1 - 1 do Control_Monad_ST_Internal_modifyImpl(function(s_S_0) local sPrime_S_0 = s_S_0 + i return { state = sPrime_S_0, value = sPrime_S_0 } end)(acc)() end return Control_Monad_ST_Internal_read(acc)() local acc = 0 for i = 0, n + 1 - 1 do local s_S_0 = acc local sPrime_S_0 = s_S_0 + i acc = sPrime_S_0 end return acc Soundness guard. The new Golden.RefUnbox.Test pins a cell stored inside another cell (outer <- STRef.new inner): the stored inner keeps its {value = …} table while outer still unboxes, and boxed operations on an aliased cell keep going through the foreign implementation. Golden.NativeLoopsST.Test's countDown pins the per-cell granularity of the value-position restriction: its steps cell (used only in run positions) unboxes while its value cell (read under map_ inside the while condition — a first-class thunk) stays boxed. All eval/golden.txt oracles — which are never auto-accepted — are byte-identical, so runtime behaviour is unchanged across the whole golden corpus, including the write-returns-written-value ST semantics and Effect write's nil result. Measurement (issue's verification asks for the allocation-counter drop). A new macro bench Bench.RefLoop — a hot ST loop accumulating through a local STRef — with committed counter oracles: the static table-allocation census (tnew_census) drops from 2 steady-state sites to 1 (the survivor is the dead foreign new closure body, never called), and the trace report shows the hot loop trace-compiled (JFORL) instead of aborted on per-iteration closures. Wall-clock at n=100000 on this machine: PUC Lua 5.1 0.0347s → 0.0009s, LuaJIT 0.024s → 0.0001s (medians of bench/tools/run_macro.lua). All pre-existing counter oracles are byte-identical, i.e. no other benchmark's shape moved. Mechanics. fromIR carries a new set of unboxed cell names: the Let case decides unboxability per allocation binding (scanning the remaining bindings and body), lowers operation-run statements in place (scoped in do … end blocks whenever the lowering declares locals, keeping magic-do's ~150-statement chunks under Lua's 200-active-locals cap), and threads the extended set through everything downstream; an expression-position run compiles to a chunk ending in a return of the run's value. Two NativeLoop adjustments: runStatements now statementizes chunk-lowered runs (an unboxed-cell run returns its value in a tail return, which a run-for-effect must rewrite into evaluation statements — previously only return-free loop chunks reached that path), and dropValue additionally drops a discarded field read off the module-scope table (built by codegen as a plain table, so no metamethod can fire), which the void wrapper's unit value otherwise leaves behind as a dead local per loop iteration. The two pre-existing -Wname-shadowing warnings in Lua.hs (both \group lambdas shadowing Relude's group) surfaced during the rebuild and are fixed by renaming to dispatchGroup, per the repo's no-tolerated-warnings rule.
Open Graph Description: Closes #239. A Ref/STRef cell compiles to a one-field heap table: the foreign new allocates {value = v}, and every read/write/modify pays a table field access. This PR adds a codegen-time pass (Lan...
X Description: Closes #239. A Ref/STRef cell compiles to a one-field heap table: the foreign new allocates {value = v}, and every read/write/modify pays a table field access. This PR adds a codegen-time pass (Lan...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/pull/315
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:640029a2-2500-bab9-3c7c-aeacce5c4689 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B1C6:96DDF:7BEB83:9E5A6E:6B00E4C4 |
| html-safe-nonce | 652daeb7f3664956a8d83ceb44491958220121b164063ea30928d99c774db816 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMUM2Ojk2RERGOjdCRUI4Mzo5RTVBNkU6NkIwMEU0QzQiLCJ2aXNpdG9yX2lkIjoiNDY1MjIxMzAzOTU2OTg4ODQ1MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1fd5c784e686ed3f83694bc7a6469aee744fc8dc998ffc688cc254053280c9f9 |
| hovercard-subject-tag | pull_request:4141479294 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/purescript-lua/purescript-lua/pull/315/files |
| twitter:image | https://avatars.githubusercontent.com/u/1009751?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1009751?s=400&v=4 |
| og:image:alt | Closes #239. A Ref/STRef cell compiles to a one-field heap table: the foreign new allocates {value = v}, and every read/write/modify pays a table field access. This PR adds a codegen-time pass (Lan... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git |
| octolytics-dimension-user_id | 131581790 |
| octolytics-dimension-user_login | purescript-lua |
| octolytics-dimension-repository_id | 587200487 |
| octolytics-dimension-repository_nwo | purescript-lua/purescript-lua |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 587200487 |
| octolytics-dimension-repository_network_root_nwo | purescript-lua/purescript-lua |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width