Title: Top-level inliner reads pre-pass reference counts: can duplicate arbitrary bindings, and recounts the whole module per binding · Issue #163 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Top-level inliner reads pre-pass reference counts: can duplicate arbitrary bindings, and recounts the whole module per binding · Issue #163 · purescript-lua/purescript-lua
X Title: Top-level inliner reads pre-pass reference counts: can duplicate arbitrary bindings, and recounts the whole module per binding · Issue #163 · purescript-lua/purescript-lua
Description: optimizeModule.withBinding (lib/Language/PureScript/Backend/IR/Optimizer.hs) decides whether a top-level Standalone binding may be inlined as "used once" by consulting uberModuleFreeRefs. Two problems live in that one let. Problem one: t...
Open Graph Description: optimizeModule.withBinding (lib/Language/PureScript/Backend/IR/Optimizer.hs) decides whether a top-level Standalone binding may be inlined as "used once" by consulting uberModuleFreeRefs. Two probl...
X Description: optimizeModule.withBinding (lib/Language/PureScript/Backend/IR/Optimizer.hs) decides whether a top-level Standalone binding may be inlined as "used once" by consulting uberModuleFreeRefs....
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/163
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Top-level inliner reads pre-pass reference counts: can duplicate arbitrary bindings, and recounts the whole module per binding","articleBody":"`optimizeModule.withBinding` (`lib/Language/PureScript/Backend/IR/Optimizer.hs`) decides whether a top-level `Standalone` binding may be inlined as \"used once\" by consulting `uberModuleFreeRefs`. Two problems live in that one `let`.\n\nProblem one: the counts are stale. `uberModuleFreeRefs` is computed from `uberModuleExprs = (bindingExprs =\u003c\u003c uberModuleBindings) \u003c> map snd exports`, where `uberModuleBindings` is the record field of the original module the pass started from. But the fold (`foldrM`) walks the topo-sorted binding list from the dependents' end, and every inlining substitutes into the already-processed accumulator. Those substitutions are invisible to the count. The undercount scenario:\n\n1. `g = Ref f` is referenced from N sites; `f` is referenced only by `g`, so its original count is 1. Aliases of this shape are common: re-exports, dictionary aliases, newtype-erased constructor wrappers.\n2. `g` is a `Ref`, so `isInlinableExpr` inlines it into all N sites. `f` now has N references in the accumulator.\n3. When the fold reaches `f`, the stale count still says 1, `isUsedOnce` fires, and `f`'s body (of arbitrary size, which is the whole point of the used-once rule) is copied into all N sites and its binding dropped.\n\nSemantics survive (the IR is pure), but the guarantee \"used once, therefore safe to inline regardless of size\" is exactly what this heuristic exists to provide, and it does not hold. The error direction matters: an overcount only misses an inlining opportunity and DCE cleans up later, while an undercount duplicates code irreversibly, because the binding is gone by the time the next fixpoint round recounts.\n\nProblem two: the recount is quadratic. The `let` sits inside the per-binding closure, so every `Standalone` binding that reaches the `isUsedOnce` check forces a fresh `countFreeRefs` fold over every expression in the module: O(bindings × module size) per optimize pass, multiplied by the optimize+dce fixpoint iterations. For a whole-program `UberModule` this is the dominant cost of the pass.\n\nStatus: found by code inspection during an architecture audit, not yet pinned by a failing test. A unit test can drive `optimizedUberModule` directly with the alias shape above and assert the large body appears once in the output.\n\nFix directions, cheapest first:\n\n- Count once per `optimizeModule` invocation and maintain the map incrementally: on inlining a binding, add the inlinee's own free-ref counts multiplied by the number of insertions, and remove the dropped binding's contribution.\n- Longer term, occurrence annotations on binders computed by a single analysis pass (GHC's OccAnal shape). `inlineLocalBindings` has the same point-query cost profile (`countFreeRef` per binding per `Let` node per fixpoint round) and would share the infrastructure.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-04T20:27:37Z","url":"https://github.com/purescript-lua/purescript-lua/issues/163"}
| 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:1645c283-3a69-a85b-fd0b-dbf6daf00881 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8DD2:3DD488:72A8AD:931DA7:6B00E45F |
| html-safe-nonce | 6c01e845bffc6b55ab3c0e73b0791993d89131e06831ed1424ddf5286372ff7c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4REQyOjNERDQ4ODo3MkE4QUQ6OTMxREE3OjZCMDBFNDVGIiwidmlzaXRvcl9pZCI6Ijc1NjYyNjM1NTc2Mzg2NDY3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 108d36aef6eb3920db6da56f29b046b80b94cc0907c5bd75b720a5f8aaa3fba3 |
| hovercard-subject-tag | issue:4810446686 |
| 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/163/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bd50ccfad7969a653f0c3bb8101a305ead7ecc13a9320ba302b587d8afa2b934/purescript-lua/purescript-lua/issues/163 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bd50ccfad7969a653f0c3bb8101a305ead7ecc13a9320ba302b587d8afa2b934/purescript-lua/purescript-lua/issues/163 |
| og:image:alt | optimizeModule.withBinding (lib/Language/PureScript/Backend/IR/Optimizer.hs) decides whether a top-level Standalone binding may be inlined as "used once" by consulting uberModuleFreeRefs. Two probl... |
| 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 | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| 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 | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width