Title: Unbox non-escaping Ref/STRef cells to mutable Lua locals · Issue #239 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Unbox non-escaping Ref/STRef cells to mutable Lua locals · Issue #239 · purescript-lua/purescript-lua
X Title: Unbox non-escaping Ref/STRef cells to mutable Lua locals · Issue #239 · purescript-lua/purescript-lua
Description: Problem A Ref/STRef created and used inside a function compiles to a heap table {value = …}, with reads as .value and writes as .value = …. When the cell never escapes (not returned, not stored in another structure, not captured past its...
Open Graph Description: Problem A Ref/STRef created and used inside a function compiles to a heap table {value = …}, with reads as .value and writes as .value = …. When the cell never escapes (not returned, not stored in ...
X Description: Problem A Ref/STRef created and used inside a function compiles to a heap table {value = …}, with reads as .value and writes as .value = …. When the cell never escapes (not returned, not stored in ...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/239
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unbox non-escaping Ref/STRef cells to mutable Lua locals","articleBody":"## Problem\n\nA `Ref`/`STRef` created and used inside a function compiles to a heap table `{value = …}`, with reads as `.value` and writes as `.value = …`. When the cell never escapes (not returned, not stored in another structure, not captured past its region), the table buys nothing over a plain mutable local, yet every `new`/`read`/`modify`/`write` pays a table allocation and a field access. This is the companion cost to native loop lowering: a `forE` accumulating into a local ref allocates the cell once and touches `.value` twice per iteration.\n\nA non-escaping `Ref.new v` can emit as `local x = v`, reads as `x`, and writes as `x = …`, keeping the boxed form only when the cell escapes.\n\n## Approach\n\nAn escape check plus a lowering choice. A cell is unboxable when every use is a direct `read`/`write`/`modify` on the same binding and it never flows anywhere as a whole value. That \"never used as a whole value\" test is precisely the `Capture` information from the analysis-lattice issue. When it holds, lower `new v` to a mutable local, `read r` to the local, and `write v r` / `modify f r` to an assignment; otherwise keep `{value = …}`.\n\n## Prerequisites / Relations\n\nBlocked by #231 (the Complexity/Capture analysis lattice): the escape test is the `Capture` flag (\"used as a whole value\" is exactly \"escapes\"). Strong synergy with the native ST/Effect loop issue: the two together turn a ref-accumulating loop into an allocation-free Lua `for`. Independent of magicDo.\n\n## Verification / Measurement\n\nA function that allocates a local ref, mutates it in a loop, and returns the final read compiles with zero `{value=}` tables and plain local assignment in `golden.lua`; the eval golden proves identical output. A cell that escapes (returned or stored) keeps its boxed form, the soundness guard, tested directly. Allocation-counter drop through #172.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-11T12:04:26Z","url":"https://github.com/purescript-lua/purescript-lua/issues/239"}
| 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 | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8d65d5a8-b377-8d0a-88db-9f2f44e909d0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9FAC:8196C:79B045:A3BF36:6B00DB57 |
| html-safe-nonce | 22a5afd7eaa3ca58fe2c8908435b2894917f7f08cb1f92f6f8c381931a05c361 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RkFDOjgxOTZDOjc5QjA0NTpBM0JGMzY6NkIwMERCNTciLCJ2aXNpdG9yX2lkIjoiMjIwNzIzNzkwMTk5MTg2MTA3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 24a84974023cf8d82f732e810f9738046dc6244dcfa64acf98a7626338fc86a8 |
| hovercard-subject-tag | issue:4862147522 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/purescript-lua/purescript-lua/239/issue_layout |
| twitter:image | https://opengraph.githubassets.com/96754105c8c700ae33c17856d722e93d357b393be62b1d7ab94835ceef5c6031/purescript-lua/purescript-lua/issues/239 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/96754105c8c700ae33c17856d722e93d357b393be62b1d7ab94835ceef5c6031/purescript-lua/purescript-lua/issues/239 |
| og:image:alt | Problem A Ref/STRef created and used inside a function compiles to a heap table {value = …}, with reads as .value and writes as .value = …. When the cell never escapes (not returned, not stored in ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Unisay |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width