René's URL Explorer Experiment


Title: Revisit M-table binding storage: per-function caching and budget-aware chunk locals · Issue #174 · purescript-lua/purescript-lua · GitHub

Open Graph Title: Revisit M-table binding storage: per-function caching and budget-aware chunk locals · Issue #174 · purescript-lua/purescript-lua

X Title: Revisit M-table binding storage: per-function caching and budget-aware chunk locals · Issue #174 · purescript-lua/purescript-lua

Description: Problem Every top-level binding lives in the M table, so every inter-binding reference is a hash lookup: the linked Data.Array output has 681 M.* references across 124 bindings. Measured: reading M.field in a loop is 2.4x slower than a l...

Open Graph Description: Problem Every top-level binding lives in the M table, so every inter-binding reference is a hash lookup: the linked Data.Array output has 681 M.* references across 124 bindings. Measured: reading M...

X Description: Problem Every top-level binding lives in the M table, so every inter-binding reference is a hash lookup: the linked Data.Array output has 681 M.* references across 124 bindings. Measured: reading M...

Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/174

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Revisit M-table binding storage: per-function caching and budget-aware chunk locals","articleBody":"## Problem\n\nEvery top-level binding lives in the `M` table, so every inter-binding reference is a hash lookup: the linked `Data.Array` output has 681 `M.*` references across 124 bindings. Measured: reading `M.field` in a loop is 2.4x slower than a local under PUC Lua 5.1; in a LuaJIT trace it is a guarded load where a local is a register.\n\nHistory (the constraint that shaped the current design): early codegen emitted top-level bindings as chunk locals and real programs hit Lua 5.1 limits — 200 locals per chunk, and the 60-upvalue limit amplified by 5.1's pass-through accumulation, where a nested function reading an outer local costs an upvalue slot in every intermediate function (#19). The `M` table was the fix: unbounded fields, and `M` itself is the single upvalue everywhere. Any revisit must handle the limits by construction, not assume programs stay small.\n\n## Approach\n\nBudget-aware emission with `M` as the overflow valve (the design principle). Every budget overflow degrades the specific reference or binding back to today's `M` form, so the worst case is exactly the current output and the limits stop being correctness cliffs.\n\n### Stage 1: per-function caching of M fields (limit-proof by construction)\n\nStorage untouched: `M` remains the single source of truth and the single upvalue, so neither limit can be approached. A `Lua.Optimizer` pass caches `M.x` fields used two or more times in a generated function body into locals at function entry (capped at ~30 per function, prioritized by use count):\n\n```lua\nM.Data_Array_span = function(p)\n  return function(arr)\n    local index, add = M.Data_Array_index, M.Data_Array_add\n    local go\n    go = function(i)\n      local v = index(arr)(i)\n      -- ...\n    end\n```\n\nSemantics: the read moves to function entry — by the time any generated function is called, module init has assigned the bindings it reads, and `M` is not mutated within a call; the runtime-lazy scheme is unaffected. This is the classic \"localize globals\" optimization from LuaJIT guides.\n\n### Stage 2: two-tier storage with budget accounting\n\nTop-K bindings by static reference count are emitted as real chunk locals; the tail stays in `M`; exported bindings are mirrored into the export surface. Two budgets are computed over the finished Lua AST before printing:\n\n1. Locals: a chunk-local counter with a ceiling of ~180 (200 minus fixture locals and headroom). Overflow keeps the binding in `M`.\n2. Upvalues: the killer of the old design. Accounting runs bottom-up over the function tree: `upvals(f) = |own outer-local references ∪ children's pass-through demands|`. When a function proto would exceed ~55, individual references are demoted — printed as `M.x` — while the binding stays a local for everyone else.\n\nA program that fits the budgets (like `Data.Array` with 124 bindings) loses the `M` table entirely: pure locals, with the module export table referencing them directly. `K` for larger programs is chosen from #172 measurements.\n\n### Rejected alternatives\n\nPer-module closure scopes wired by upvalues — the road that led to #19; transitive pass-through accumulation makes the explosion structural. Globals/`setfenv` — `GETGLOBAL` in 5.1 is the same hash lookup, on `_G`. Integer-indexed storage (`B[42]`) — still a guarded load in traces, and unreadable output.\n\n### ADR\n\nThis changes the codegen structure, so the decision gets an ADR — `docs/adr/0001-top-level-binding-storage.md`, the first in the repo, establishing the practice. It records the full history (locals, then `M`, then two-tier), the budget model, and the rejected alternatives above.\n\n## Prerequisites / Relations\n\nIndependent in the dependency graph — pure codegen, no hard prerequisite. It builds on the #19 history above (the constraint the design must respect). The `K` threshold for large programs is chosen from #172's measurements, and #172's `M.field`-vs-local microbenchmark is the tool that scores the win. The multiplier grows once uncurrying (#24) and loopification (#181) land: real loops then exist and a cached local becomes a loop-hoisted register in a LuaJIT trace.\n\n## Verification / Measurement\n\nModest immediately: 2.4x on multi-use function bodies and module init (the #172 `M.field`-vs-local microbenchmark plus macro timings). The headline structural observable: a program that fits the budgets — `Data.Array`, 124 bindings — emits no `M` table at all, only chunk locals with the export table referencing them directly. Structural goldens churn mechanically (`PSLUA_GOLDEN_ACCEPT` + diff review); as a pure-codegen change, eval goldens must not move.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-06T09:40:26Z","url":"https://github.com/purescript-lua/purescript-lua/issues/174"}

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/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:d3364399-306a-0c15-4d2a-5a3cf42b296a
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBD86:272DC0:663350:840312:6B00DFBA
html-safe-nonce3cd9e3a4f1f54610a994bfc31b91005669bdb7a1dbe3a1de9ed456860ee6a487
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDg2OjI3MkRDMDo2NjMzNTA6ODQwMzEyOjZCMDBERkJBIiwidmlzaXRvcl9pZCI6Ijc1NzgxNTM5OTk1MjY4NDYzOTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacaf9c5957f1bbb0c5e8a9a7c2a54d7405beb36c4953b95c913363cb6c9d829c64
hovercard-subject-tagissue:4817872772
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/purescript-lua/purescript-lua/174/issue_layout
twitter:imagehttps://opengraph.githubassets.com/8f3530cc85238d41c2479888fb213194d817e522e9b1bfb071d9704ff3388311/purescript-lua/purescript-lua/issues/174
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/8f3530cc85238d41c2479888fb213194d817e522e9b1bfb071d9704ff3388311/purescript-lua/purescript-lua/issues/174
og:image:altProblem Every top-level binding lives in the M table, so every inter-binding reference is a hash lookup: the linked Data.Array output has 681 M.* references across 124 bindings. Measured: reading M...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameUnisay
hostnamegithub.com
expected-hostnamegithub.com
None043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf
turbo-cache-controlno-preview
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
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release5425dc24e8bd8d6fca9641a12afc984825a43875
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/purescript-lua/purescript-lua/issues/174#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2F174
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%2Fissues%2F174
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=purescript-lua%2Fpurescript-lua
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/174
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/174
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/174
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
https://github.com/purescript-lua/purescript-lua
New issuehttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/174
New issuehttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/174
#246https://github.com/purescript-lua/purescript-lua/pull/246
Revisit M-table binding storage: per-function caching and budget-aware chunk localshttps://github.com/purescript-lua/purescript-lua/issues/174#top
#246https://github.com/purescript-lua/purescript-lua/pull/246
https://github.com/Unisay
OptimisationA Compiler Optimisationhttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22Optimisation%22
area: codegenLua code generation / printinghttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22area%3A%20codegen%22
https://github.com/Unisay
Unisayhttps://github.com/Unisay
on Jul 6, 2026https://github.com/purescript-lua/purescript-lua/issues/174#issue-4817872772
#19https://github.com/purescript-lua/purescript-lua/issues/19
#172https://github.com/purescript-lua/purescript-lua/issues/172
#19https://github.com/purescript-lua/purescript-lua/issues/19
#19https://github.com/purescript-lua/purescript-lua/issues/19
#172https://github.com/purescript-lua/purescript-lua/issues/172
#172https://github.com/purescript-lua/purescript-lua/issues/172
#24https://github.com/purescript-lua/purescript-lua/issues/24
#181https://github.com/purescript-lua/purescript-lua/issues/181
#172https://github.com/purescript-lua/purescript-lua/issues/172
Unisayhttps://github.com/Unisay
area: codegenLua code generation / printinghttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22area%3A%20codegen%22
OptimisationA Compiler Optimisationhttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22Optimisation%22
on Jul 6, 2026https://github.com/purescript-lua/purescript-lua/issues/174#event-27599983212
Unisayhttps://github.com/Unisay
on Jul 6, 2026https://github.com/purescript-lua/purescript-lua/issues/174#event-27606252366
Closing audit: prove the cumulative win of the optimisation series #187https://github.com/purescript-lua/purescript-lua/issues/187
Unisayhttps://github.com/Unisay
on Jul 8, 2026https://github.com/purescript-lua/purescript-lua/issues/174#event-27700995431
Merge pull request #207 from purescript-lua/issue-174/m-field-cachinghttps://github.com/purescript-lua/purescript-lua/commit/922a528d69d8dbe0f09c96347517b19690fd9c16
#207https://github.com/purescript-lua/purescript-lua/pull/207
922a528https://github.com/purescript-lua/purescript-lua/commit/922a528d69d8dbe0f09c96347517b19690fd9c16
Unisayhttps://github.com/Unisay
on Jul 8, 2026https://github.com/purescript-lua/purescript-lua/issues/174#event-27701040453
Unisayhttps://github.com/Unisay
on Jul 12, 2026https://github.com/purescript-lua/purescript-lua/issues/174#event-27872254668
test(bench): accept counter-golden line shifts from top-level binding…https://github.com/purescript-lua/purescript-lua/commit/64744a9504c0814f7ad76ba216fabc4d4ba0b6d7
64744a9https://github.com/purescript-lua/purescript-lua/commit/64744a9504c0814f7ad76ba216fabc4d4ba0b6d7
Unisayhttps://github.com/Unisay
completedhttps://github.com/purescript-lua/purescript-lua/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#246https://github.com/purescript-lua/purescript-lua/pull/246
on Jul 12, 2026https://github.com/purescript-lua/purescript-lua/issues/174#event-27872353837
Unisayhttps://github.com/Unisay
on Jul 12, 2026https://github.com/purescript-lua/purescript-lua/issues/174#event-27875400213
perf: rebuild sharing for multi-use foreign accessor reads (#248)https://github.com/purescript-lua/purescript-lua/commit/98043eb8f01b2b94ca145db91f239aa6d15c3d84
#248https://github.com/purescript-lua/purescript-lua/issues/248
98043ebhttps://github.com/purescript-lua/purescript-lua/commit/98043eb8f01b2b94ca145db91f239aa6d15c3d84
Sign up for freehttps://github.com/signup?return_to=https://github.com/purescript-lua/purescript-lua/issues/174
Sign in to commenthttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/174
Unisayhttps://github.com/Unisay
OptimisationA Compiler Optimisationhttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22Optimisation%22
area: codegenLua code generation / printinghttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22area%3A%20codegen%22
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.