Title: Inline small pure function bindings regardless of use count · Issue #211 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Inline small pure function bindings regardless of use count · Issue #211 · purescript-lua/purescript-lua
X Title: Inline small pure function bindings regardless of use count · Issue #211 · purescript-lua/purescript-lua
Description: Problem A tiny curried function used at two or more saturated sites (\x y -> x - y, \x y -> x + y, \x y -> x == y) comes out as a shared uncurried worker instead of an inline operator. Since #178 lifts the prelude arithmetic / comparison...
Open Graph Description: Problem A tiny curried function used at two or more saturated sites (\x y -> x - y, \x y -> x + y, \x y -> x == y) comes out as a shared uncurried worker instead of an inline operator. Since #178 l...
X Description: Problem A tiny curried function used at two or more saturated sites (\x y -> x - y, \x y -> x + y, \x y -> x == y) comes out as a shared uncurried worker instead of an inline operator. Sin...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/211
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inline small pure function bindings regardless of use count","articleBody":"## Problem\n\nA tiny curried function used at two or more saturated sites (`\\x y -> x - y`, `\\x y -> x + y`, `\\x y -> x == y`) comes out as a shared uncurried worker instead of an inline operator. Since #178 lifts the prelude arithmetic / comparison / equality foreigns to primops, this now shows up directly: `Golden.Fibonacci.Test` emits `sub$w(v, 1)` / `sub$w(v, 2)` where `v - 1` / `v - 2` would do. The cost is one Lua call per operation. PUC pays it (fib(32) is roughly 7M extra calls); LuaJIT traces the tiny leaf function and mostly erases it.\n\n## Why the current pipeline leaves it\n\nTwo independent factors combine, so the lifted `Just Always` annotation does not save the day:\n\n1. `reduceObjectProp` deliberately strips `Just Always` when it projects a dictionary field (`setAnn` takes the projection's annotation, not the field's), so a large foreign accessor like `foldlArray` does not become inline-always everywhere through a projection. The lifted `intSub` lambda loses its `Always` as it resolves through `ringInt.sub`.\n2. purs already shares `sub ringInt` as a top-level binding (`Golden.Fibonacci.Test.sub`) because `-` occurs more than once in `fib`.\n\nThe result is `sub = \\x y -> x - y` with the annotation gone, used twice, so the optimizer does not inline it (use-count > 1, not `Always`), it survives to the uncurry pass, and uncurry splits the curried binding into a worker.\n\nUnderneath, `isInlinableExpr` is overly conservative for lambdas: it whitelists refs, literals, and `Always` nodes, and gates everything else on use-once. But a binding whose RHS is a lambda (a value) is work-free to duplicate. The body runs once per application no matter how many textual copies exist, so pasting it at N sites never multiplies runtime work. Only code size is at stake.\n\n## Approach\n\nExtend `isInlinableExpr` in `IR.Optimizer` with a size-capped small-value predicate. The guard is shared by `withBinding`, `betaReduce`, and `inlineLocalBinding` (Note [Beta reduction and local inlining share an inlining guard]), so this is a one-place change.\n\nMinimal-correct form: the RHS is a manifest lambda and its node count is at most K. Conservative first cut (recommended), which also bounds a lambda body to shapes that are cheap to duplicate:\n\n- allowed in the body: `LiteralInt/Float/String/Char/Bool`, `Ref`, `PrimBinOp`, `PrimNot`, `Eq`, `IfThenElse`, the projections (`ObjectProp` / `DataArgumentByIndex` / `ReflectCtor`), and nested `AbsN`;\n- disallowed: `AppN` (may carry an effect or an expensive call), `Let`, `LiteralArray` / `LiteralObject`, `ObjectUpdate` (allocation), `ForeignImport`;\n- cap the node count (around 8 to 16), and ideally add a `use-count × size` growth budget in the GHC size/discount spirit.\n\nThe growth budget matters: a tiny function used at thirty sites is thirty copies, which pushes on the 200-locals-per-function limit (see the M-table history, #174). So this wants tuning against #172 measurements, not a fixed constant pulled out of the air.\n\nCorrectness is straightforward: inlining a lambda is not eta reduction (the lambda stays, so Note [Eta reduction is unsound] does not apply), recursive-group members are already excluded from inlining, and `substituteCopyM` freshens binders so GUC holds.\n\n## Prerequisites / Relations\n\nComplementary to #180 (budgeted call-site inlining of dictionary methods), with little overlap. #180 collapses the `Data_Ord_compare(dict)` method dispatch; this cleans up residual shared small bindings. #180 narrows the scope (a dict method inlined at its call site can collapse the arithmetic there, so no shared binding forms), but does not subsume it: `Golden.Fibonacci.Test.sub` is shared by purs before our IR ever sees it, and #180's own output can leave fresh small bindings this pass would pick up. Land #180 first, re-measure, and pursue this only if the residual still matters.\n\n## Scope and churn\n\nThis is a general inliner change, not primop-specific: it inlines any small pure function used more than once, so its blast radius is wider than #178's additions. The conservative predicate (pure primop / `Eq` bodies only) shifts roughly 13 goldens carrying `sub$w` / `add$w` / `eq$w` (Fibonacci, FieldCaching, Loopification, the Long*Bind family, TailRecM2Shadow, LongCallbackChain, Uncurry). It leaves `greaterThanOrEq$w` / `lessThan$w` (bodies apply `compare`), `logShow$w` (applies `log`), and the monad workers alone, since their bodies contain applications.\n\n## Priority\n\nLow. #178 already captures the large win (foreign dispatch to primop), LuaJIT erases most of the remaining per-call gap, and #180 will shrink the residual further.\n\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-08T11:49:46Z","url":"https://github.com/purescript-lua/purescript-lua/issues/211"}
| 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:1e2e79d3-ba17-0509-0007-2488fd8604db |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BA4C:3DD488:551421:6DE967:6B00DB5E |
| html-safe-nonce | adf5fb2f8e44ff691998bf17895f36b69e751b357be8152a46f98c101d41a19d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQTRDOjNERDQ4ODo1NTE0MjE6NkRFOTY3OjZCMDBEQjVFIiwidmlzaXRvcl9pZCI6IjI5MjU2MjE5NzAyMzYxMzYyODYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 369b7ce041f5bd5efa93e15a54418864c014dc18b8084fdd9a04d925e2359da7 |
| hovercard-subject-tag | issue:4837141803 |
| 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/211/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1a26d390725cbe9ec8589d698af30a18ee11e6824b6266327c1fea9cc4463f09/purescript-lua/purescript-lua/issues/211 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1a26d390725cbe9ec8589d698af30a18ee11e6824b6266327c1fea9cc4463f09/purescript-lua/purescript-lua/issues/211 |
| og:image:alt | Problem A tiny curried function used at two or more saturated sites (\x y -> x - y, \x y -> x + y, \x y -> x == y) comes out as a shared uncurried worker instead of an inline operator. Since #178 l... |
| 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