Title: betaReduce substitutes a non-trivial argument into every occurrence of the parameter, multiplying work · Issue #167 · purescript-lua/purescript-lua · GitHub
Open Graph Title: betaReduce substitutes a non-trivial argument into every occurrence of the parameter, multiplying work · Issue #167 · purescript-lua/purescript-lua
X Title: betaReduce substitutes a non-trivial argument into every occurrence of the parameter, multiplying work · Issue #167 · purescript-lua/purescript-lua
Description: Problem betaReduce (lib/Language/PureScript/Backend/IR/Optimizer.hs) rewrites App (Abs (ParamNamed param) body) r by substituting r for every occurrence of param, with no look at either the shape of r or the number of occurrences. In a s...
Open Graph Description: Problem betaReduce (lib/Language/PureScript/Backend/IR/Optimizer.hs) rewrites App (Abs (ParamNamed param) body) r by substituting r for every occurrence of param, with no look at either the shape o...
X Description: Problem betaReduce (lib/Language/PureScript/Backend/IR/Optimizer.hs) rewrites App (Abs (ParamNamed param) body) r by substituting r for every occurrence of param, with no look at either the shape o...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/167
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"betaReduce substitutes a non-trivial argument into every occurrence of the parameter, multiplying work","articleBody":"## Problem\n\n`betaReduce` (`lib/Language/PureScript/Backend/IR/Optimizer.hs`) rewrites `App (Abs (ParamNamed param) body) r` by substituting `r` for every occurrence of `param`, with no look at either the shape of `r` or the number of occurrences. In a strict language that duplicates evaluation: `(\\x -> ... x ... x ...) (g y)` becomes `... (g y) ... (g y) ...`, and the work of `g y` now runs once per occurrence instead of once.\n\nThe path is live. Redexes with non-trivial arguments show up whenever the inliner puts a lambda into call-head position, which it does for used-once bindings and for `@inline always` annotations; the more the inliner improves, the more such redexes this rule sees.\n\nThis is a performance defect, not a correctness one. Effect and ST computations are nullary thunks in the IR, so evaluating a duplicated argument builds the thunk twice but runs the effect exactly as many times as the body calls it. The remaining evaluation-order edge — an `Exception` inside the duplicated or dropped argument — follows the policy the pipeline already declares for let-bound values (see the FloatIn module note: discardable and reorderable once nothing observes them).\n\n## Approach\n\nApply GHC's rule: substitute only when the argument is trivial (a `Ref` or a literal) or the parameter occurs at most once; otherwise rewrite the redex to `Let param = r in body` and leave the decision to `inlineLocalBindings`, which already counts occurrences.\n\n## Prerequisites / Relations\n\nOptimizer correctness/efficiency bug, independent. Related to #142 and #143: once their occurrence-annotation rework lands, the occurrence check here becomes a field read instead of a traversal.\n\n## Verification / Measurement\n\nA redex `(\\x -> ... x ... x ...) (g y)` with a non-trivial argument no longer duplicates `g y` into every occurrence of the parameter; instead it is let-bound and the argument's work is evaluated once. Trivial arguments (a `Ref` or literal) and single-occurrence parameters still substitute directly.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-05T17:28:28Z","url":"https://github.com/purescript-lua/purescript-lua/issues/167"}
| 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:8ed960b6-0b8a-4f8f-0d5c-0b1da84b7035 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9002:3DD488:637B1B:801C99:6B00DFC4 |
| html-safe-nonce | 46d53b8a928cf0cf890f5c3520f9324fd83ddc8f140cddb29f6039c83131ae60 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDAyOjNERDQ4ODo2MzdCMUI6ODAxQzk5OjZCMDBERkM0IiwidmlzaXRvcl9pZCI6IjY3MzUwODM4ODY3NjM2MzA1MzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1528a1b5d3e1f08350dfa4a8db91315adefba0461d8a1268de00a3caf10ebb87 |
| hovercard-subject-tag | issue:4813780147 |
| 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/167/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f01c5ffc855e76d27585840764ee68c4026385eecafd776f51e16ac8c2563d58/purescript-lua/purescript-lua/issues/167 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f01c5ffc855e76d27585840764ee68c4026385eecafd776f51e16ac8c2563d58/purescript-lua/purescript-lua/issues/167 |
| og:image:alt | Problem betaReduce (lib/Language/PureScript/Backend/IR/Optimizer.hs) rewrites App (Abs (ParamNamed param) body) r by substituting r for every occurrence of param, with no look at either the shape o... |
| 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