Title: Arithmetic workers survive un-inlined in hot loop bodies · Issue #281 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Arithmetic workers survive un-inlined in hot loop bodies · Issue #281 · purescript-lua/purescript-lua
X Title: Arithmetic workers survive un-inlined in hot loop bodies · Issue #281 · purescript-lua/purescript-lua
Description: Problem Some hot loops pay a Lua function call per arithmetic operation because the floated dictionary-application binding (add = Data.Semiring.add semiringInt and friends, which purs floats to module level) survives as an uncurried work...
Open Graph Description: Problem Some hot loops pay a Lua function call per arithmetic operation because the floated dictionary-application binding (add = Data.Semiring.add semiringInt and friends, which purs floats to mod...
X Description: Problem Some hot loops pay a Lua function call per arithmetic operation because the floated dictionary-application binding (add = Data.Semiring.add semiringInt and friends, which purs floats to mod...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/281
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Arithmetic workers survive un-inlined in hot loop bodies","articleBody":"## Problem\n\nSome hot loops pay a Lua function call per arithmetic operation because the floated dictionary-application binding (`add = Data.Semiring.add semiringInt` and friends, which purs floats to module level) survives as an uncurried worker instead of dissolving into the primop. The specialized `tuple_fold` loop compiles to\n\n```lua\ngo_S_sc1Tuple_S_f1, go_S_sc1Tuple_S_f2 =\n Bench_TupleFold_add_S_w(go_S_sc1Tuple_S_f1, go_S_sc1Tuple_S_f2),\n Bench_TupleFold_add_S_w(go_S_sc1Tuple_S_f2, 1)\n```\n\nwhere `Bench_TupleFold_add_S_w = function(x, y) return x + y end`. Under PUC Lua 5.1 this is the whole remaining gap on the `tuple_fold` benchmark: 0.169s with the worker calls against 0.057s for the ideal loop with inline `+`, so about 3x is left on the table. Pre-existing behavior, not introduced by #208: a main-branch build of the same module produces the same worker calls.\n\nThe behavior is inconsistent across modules, which is what makes it look like a missed fold rather than a policy decision:\n\n- `Bench.CurriedStep` (one use each of `add`/`sub`/`eq`): everything dissolves, the loop body is `acc + i, i - 1`.\n- `Bench.TupleFold` (two uses of `add`): `add$w` survives, both call sites in the loop pay a call.\n- `Golden.SpecConstr.Test` (four uses of `add`, four of `sub`): `add` dissolves everywhere into inline `+`, while `sub$w` survives at all four sites, in the same module.\n\n## Approach\n\nDiagnose before fixing: trace the pipeline (`runStepsTraced`) on `Bench.TupleFold` and `Golden.SpecConstr.Test` to find where `add`'s resolution diverges, and why `sub` behaves differently from `add` in the same module (different classes resolve through different foreigns: `Data.Semiring.intAdd` against `Data.Ring.intSub`). Candidate mechanisms: the use-once whole-binding inline is the only path that fires reliably; the call-site paste (`inlineSaturatedCall`) either does not reach these workers or its budget/complexity gate declines them; or the inline-env snapshot goes stale across specialize rounds. The fix is likely a narrow one: a worker whose body is a bare `PrimBinOp` over its parameters is the cheapest possible paste and should inline at every saturated site regardless of use count.\n\n## Prerequisites / Relations\n\nNone blocking. Composes with #208: call-pattern specialization removes the per-iteration box, this issue is about the residual call-per-op in the same loops. Same machinery family as #24 (uncurry worker/wrapper) and #180 (budgeted call-site inlining). The `tuple_fold` benchmark and its counter goldens land with PR #279.\n\n## Verification / Measurement\n\n`bench/run` on `tuple_fold` under PUC Lua 5.1: the linked artifact closes toward the ideal 0.057s from the current 0.169s (the boxed baseline was 0.425s). The loop body in `bench/_build/Bench.TupleFold.lua` shows inline `+` instead of `add$w` calls, and `Golden.SpecConstr.Test`'s `sub$w` sites fold the same way. Existing eval goldens stay byte-identical.\n\n## Open questions\n\n- Why does `add` dissolve in `Golden.SpecConstr.Test` (four uses) but not in `Bench.TupleFold` (two uses), while `sub` survives with four uses in the module where `add` dissolved? The use-count hypothesis fails in both directions, so the actual trigger is still unidentified.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-15T17:01:04Z","url":"https://github.com/purescript-lua/purescript-lua/issues/281"}
| 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:78c6bd95-bedf-a7c1-52c3-e199bae78cfd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C8E4:3DF0BE:8B37F2:B632CB:6B00DFB1 |
| html-safe-nonce | a8d3ddeea9d0cd81aea7b3cd5913c950255873a1ca0e376a0b84c953652607e1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOEU0OjNERjBCRTo4QjM3RjI6QjYzMkNCOjZCMDBERkIxIiwidmlzaXRvcl9pZCI6IjExMzYxMTQxNjk3OTY0MTk1MDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e412a2a206a3597d431bf4e304d696f311a87228c203bc743ca1c346f18cdb9e |
| hovercard-subject-tag | issue:4894680373 |
| 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/281/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e26d491310a45d3436223a7445fcbe8524039cfab54c96c0d71f16d2c1b5cd6f/purescript-lua/purescript-lua/issues/281 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e26d491310a45d3436223a7445fcbe8524039cfab54c96c0d71f16d2c1b5cd6f/purescript-lua/purescript-lua/issues/281 |
| og:image:alt | Problem Some hot loops pay a Lua function call per arithmetic operation because the floated dictionary-application binding (add = Data.Semiring.add semiringInt and friends, which purs floats to mod... |
| 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