René's URL Explorer Experiment


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

direct link

Domain: github.com

react-profiling0
react-import-mapreact,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-controllerpull_requests
route-actionfiles
fetch-noncev2:640029a2-2500-bab9-3c7c-aeacce5c4689
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idB1C6:96DDF:7BEB83:9E5A6E:6B00E4C4
html-safe-nonce652daeb7f3664956a8d83ceb44491958220121b164063ea30928d99c774db816
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMUM2Ojk2RERGOjdCRUI4Mzo5RTVBNkU6NkIwMEU0QzQiLCJ2aXNpdG9yX2lkIjoiNDY1MjIxMzAzOTU2OTg4ODQ1MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac1fd5c784e686ed3f83694bc7a6469aee744fc8dc998ffc688cc254053280c9f9
hovercard-subject-tagpull_request:4141479294
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/purescript-lua/purescript-lua/pull/315/files
twitter:imagehttps://avatars.githubusercontent.com/u/1009751?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/1009751?s=400&v=4
og:image:altCloses #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_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git
octolytics-dimension-user_id131581790
octolytics-dimension-user_loginpurescript-lua
octolytics-dimension-repository_id587200487
octolytics-dimension-repository_nwopurescript-lua/purescript-lua
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id587200487
octolytics-dimension-repository_network_root_nwopurescript-lua/purescript-lua
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release624179fb8b88d26ba5bba08894eb60a1c5a1d93d
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/purescript-lua/purescript-lua/pull/315/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fpull%2F315%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fpull%2F315%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=purescript-lua%2Fpurescript-lua
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/315/files
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/315/files
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/315/files
purescript-luahttps://github.com/purescript-lua
purescript-luahttps://github.com/purescript-lua/purescript-lua
Codehttps://github.com/purescript-lua/purescript-lua
Issues48 (48)https://github.com/purescript-lua/purescript-lua/issues
Pull requestshttps://github.com/purescript-lua/purescript-lua/pulls
Discussionshttps://github.com/purescript-lua/purescript-lua/discussions
Actionshttps://github.com/purescript-lua/purescript-lua/actions
Projectshttps://github.com/purescript-lua/purescript-lua/projects
Security and qualityhttps://github.com/purescript-lua/purescript-lua/security
Insightshttps://github.com/purescript-lua/purescript-lua/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2Fnew%2Fchoose
Unisayhttps://github.com/Unisay
mainhttps://github.com/purescript-lua/purescript-lua/tree/main
issue-239/unbox-ref-localshttps://github.com/purescript-lua/purescript-lua/tree/issue-239/unbox-ref-locals
Conversation 0 https://github.com/purescript-lua/purescript-lua/pull/315
Commits 4 https://github.com/purescript-lua/purescript-lua/pull/315/commits
Checks 2 https://github.com/purescript-lua/purescript-lua/pull/315/checks
Files changed https://github.com/purescript-lua/purescript-lua/pull/315/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/315/files
Unbox non-escaping Ref/STRef cells to mutable Lua locals https://github.com/purescript-lua/purescript-lua/pull/315/files#top
Show all changes 4 commits https://github.com/purescript-lua/purescript-lua/pull/315/files
ce6fd33 test(golden): pin the pre-#239 boxed shapes of Ref/STRef cells Unisay Jul 27, 2026 https://github.com/purescript-lua/purescript-lua/pull/315/commits/ce6fd3367850d480cddeb34e88741c2f37f8d170
5b3006c feat(codegen): unbox non-escaping Ref/STRef cells to mutable locals Unisay Jul 27, 2026 https://github.com/purescript-lua/purescript-lua/pull/315/commits/5b3006ca76c5870390fc4f51784f0d9956d992e1
7726771 bench(macro): add the RefLoop spec for the unboxed ST accumulator Unisay Jul 27, 2026 https://github.com/purescript-lua/purescript-lua/pull/315/commits/772677196e68d5c36955c0dd852c5936f26cb093
23cda94 docs(changelog): fragment for #239 cell unboxing Unisay Jul 27, 2026 https://github.com/purescript-lua/purescript-lua/pull/315/commits/23cda9403a8c2c5e5adcdafd28bc2e4ffee52690
Clear filters https://github.com/purescript-lua/purescript-lua/pull/315/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/315/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/315/files
fnew_Bench.RefLoop.txt https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-97592177734437ea5b04b3472c50481fe606fcdb06b7957409e94fd7c38e4749
tnew_Bench.RefLoop.txt https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-91c3bb47e436f789b9d98e6b4d633f27045e990748bb7a809e63b664bfc65e44
trace_ref_loop.txt https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-3db1f6631752847e5e29ec3754bd924615f20a4600fad4ff456274ae96ab2945
ref_loop.lua https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-d40e688272d51f2b5313bfebebe45a9af25614984b139fc44f3b7286feb7bb18
20260727_150000_unisay_unbox_ref_cells.md https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-3623a7598afe9a323bbbbadbc0794ecc18804fe181f64c46ffda0912713d9828
Lua.hs https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-11295f6f97401e5cc1fdfa2a8caa3fc80edc56cd58262d3de010e803f8d3640f
NativeLoop.hs https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-a3888e1533705844ed6fcb418648fbbc4a26c848a680a76eb85d8477c625acdf
RefUnbox.hs https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-9310af99343da574b7e8679322c6ee2ddcc72aec9db92b2827955986faf3bcbe
pslua.cabal https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-cf0302393cf6443df4429c19c0fc326753a83405d0366075a98ac2753e5e1ab2
golden.lua https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-46c90be6e57e4b5074ff97cc3e991ae9c2810fba31ae2d018df9bd4aa9c915ec
golden.lua https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-bbf80c14eb12e728e7f421861af2a2f560b5d062b608c6661970199801337c69
golden.lua https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-414ab0a0ac3c675e25343f168caf9f8dd6450109e8fea78bc704760ffd5a975c
golden.lua https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-1f6ed6ca2bb3ee97221993a2d4f5d32ec26ecf87b3fc3f5b1b2d0ecb457d08db
corefn.json https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-4b67c5c4849e83d6718148948894ee2dd0339a3c18862a63c60096f465303ef0
.gitignore https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-b7c9f629ab73744caa9650edb55d7f2c0e7b35818cd4b6cfaa131550d49e0b4d
golden.txt https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-6a3c1ef5e99af6c7fe7e67b4c45315b941d138c3b34386eca98ad8078d39ef2f
golden.ir https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-22fdd567a068ecde81e1af5eee0ef95a165e3924b992a233a7ea1ff4500adab8
golden.lua https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-57f1a98853c62e96e5cfd688fe4839feb5a50f91db3fd6b6f5149ec5250fb262
golden.lua https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-86b98ca808da9e36c909687862754e31f093ebcfa407f44f41591e1ae7129a5a
RefLoop.purs https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-17eb00e8bb1cc2b1c5a85917e06782e389f584fbf660b15bb0f3dbc69860d82c
Test.purs https://github.com/purescript-lua/purescript-lua/pull/315/files#diff-e6ff2cf728b0132372f25bb9be82a638e9ca4856d9b6c13527b025b9f5a7c03c
bench/goldens/fnew_Bench.RefLoop.txthttps://github.com/purescript-lua/purescript-lua/pull/315/files#diff-97592177734437ea5b04b3472c50481fe606fcdb06b7957409e94fd7c38e4749
View file https://github.com/purescript-lua/purescript-lua/blob/23cda9403a8c2c5e5adcdafd28bc2e4ffee52690/bench/goldens/fnew_Bench.RefLoop.txt
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/315/{{ revealButtonHref }}
bench/goldens/tnew_Bench.RefLoop.txthttps://github.com/purescript-lua/purescript-lua/pull/315/files#diff-91c3bb47e436f789b9d98e6b4d633f27045e990748bb7a809e63b664bfc65e44
View file https://github.com/purescript-lua/purescript-lua/blob/23cda9403a8c2c5e5adcdafd28bc2e4ffee52690/bench/goldens/tnew_Bench.RefLoop.txt
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/315/{{ revealButtonHref }}
bench/goldens/trace_ref_loop.txthttps://github.com/purescript-lua/purescript-lua/pull/315/files#diff-3db1f6631752847e5e29ec3754bd924615f20a4600fad4ff456274ae96ab2945
View file https://github.com/purescript-lua/purescript-lua/blob/23cda9403a8c2c5e5adcdafd28bc2e4ffee52690/bench/goldens/trace_ref_loop.txt
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/315/{{ revealButtonHref }}
bench/macro/ref_loop.luahttps://github.com/purescript-lua/purescript-lua/pull/315/files#diff-d40e688272d51f2b5313bfebebe45a9af25614984b139fc44f3b7286feb7bb18
View file https://github.com/purescript-lua/purescript-lua/blob/23cda9403a8c2c5e5adcdafd28bc2e4ffee52690/bench/macro/ref_loop.lua
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/315/{{ revealButtonHref }}
changelog.d/20260727_150000_unisay_unbox_ref_cells.mdhttps://github.com/purescript-lua/purescript-lua/pull/315/files#diff-3623a7598afe9a323bbbbadbc0794ecc18804fe181f64c46ffda0912713d9828
View file https://github.com/purescript-lua/purescript-lua/blob/23cda9403a8c2c5e5adcdafd28bc2e4ffee52690/changelog.d/20260727_150000_unisay_unbox_ref_cells.md
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/315/{{ revealButtonHref }}
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/315/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.