Title: feat(optimizer): fold array length and indexing on literal arrays (#225) by Unisay · Pull Request #292 · purescript-lua/purescript-lua · GitHub
Open Graph Title: feat(optimizer): fold array length and indexing on literal arrays (#225) by Unisay · Pull Request #292 · purescript-lua/purescript-lua
X Title: feat(optimizer): fold array length and indexing on literal arrays (#225) by Unisay · Pull Request #292 · purescript-lua/purescript-lua
Description: Closes #225. A pattern match on a fixed-length array whose scrutinee is a manifest array literal is decidable at compile time, but the optimizer left it as a runtime length check plus element reads. This PR adds the three transformations the issue proposes, and the existing folds finish the job. The rules reduceArrayRead — the reduceObjectProp sibling for arrays: folds arrayLength over an in-place LiteralArray to its element count, and an in-range ArrayIndex to the element itself. The issue's piece (2) turned out simpler than proposed: the IR's ArrayIndex already carries a static Natural index, so no literal-index matching is needed. The length folds against the IR element count, which is exact — a LiteralArray codegens to a hole-free positional table — so the caveat about Lua's # over tables with holes never arises: reduceArrayRead ∷ Applicative m ⇒ RewriteRuleM m Ann reduceArrayRead = pure . \case ArrayLength ann (LiteralArray _ elements) → Just $ LiteralInt ann (fromIntegral (length elements)) ArrayIndex ann (LiteralArray _ elements) index → setAnn ann <$> elements !!? fromIntegral index _ → Nothing propagateKnownArrayThroughLet — the literal-array sibling of propagateKnownCtorThroughLet (#214). The match's scrutinee is let-bound and read several times (one length check plus one read per element), so it is never in place for reduceArrayRead to see. When the binder is read only through in-range array reads, each length read becomes the element count, each element read becomes a fresh element-binder bound once to the element (the field-binder discipline of #214, so an element read at several sites is never duplicated), and the binding is dropped. The rule declines on a whole-value read or an out-of-range index — Query.hasWholeValueArrayRead mirrors hasWholeValueRead including the out-of-range-counts-as-whole-value guard. End to end The motivating shape from the issue, in the Golden.ArrayPatternMatch golden — before: local _ = Golden_ArrayPatternMatch_Test_logShow(Golden_ArrayPatternMatch_Test_firstTwo({ [1] = 10, [2] = 20 }))() after: local _ = Effect_Console_log(Data_Show_showIntImpl(30))() Every literal-array call site in that golden folds to its constant (30, -1, -1, 9); the eval/golden.txt oracle is untouched and still passes. The surviving M.Golden_ArrayPatternMatch_Test_firstTwo = … entries in the new golden.lua are the harness shape, not a leak: the golden pipeline links AsModule, so the now-uncalled helpers stay reachable as exports — the same residue every other golden with exported-but-unused-by-main helpers shows. Tests Test-first, per the issue: the unit specs were red before the rules landed. The in-place folds and their decline cases (out-of-range index, unknown-length array) are pinned at optimizedExpression level; the no-duplication discipline is pinned by an alphaEq check that a twice-read non-trivial element becomes one binder read twice. The full collapse runs through optimizedUberModuleChecked (mirroring the #214 end-to-end test), because the rewrite chain deliberately leaves the spent element-binders for DCE — that run also lints every pass boundary, so a GUC or scoping violation from folding through the Let would fail there. Two properties: the collapse across generated scalar elements, and well-scopedness when the index is fuzzed out of range: prop "declines an out-of-range element read, staying well-scoped" do len ← forAll (Gen.int (Range.linear 0 3)) index ← forAll (Gen.integral (Range.linear 0 5)) let original = let1 (Name "v") (literalArray (replicate len (literalInt 1))) $ application (application (refImported m (Name "pair")) (arrayIndex v 0)) (arrayIndex v index) unboundLocals (optimizedExpression original) === [] cabal test all is green, and the randomized specs were stressed across several seeds.
Open Graph Description: Closes #225. A pattern match on a fixed-length array whose scrutinee is a manifest array literal is decidable at compile time, but the optimizer left it as a runtime length check plus element reads...
X Description: Closes #225. A pattern match on a fixed-length array whose scrutinee is a manifest array literal is decidable at compile time, but the optimizer left it as a runtime length check plus element reads...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/pull/292
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:74b41aa7-1883-9a0c-96a0-370e60561854 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D1E8:2E7EBB:7B4A41:9D42D9:6B00E4A6 |
| html-safe-nonce | c57bf683ab19819ab2d33e5cadd7290fd22ccd7abbdbc6f313af973d274f3a1e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMUU4OjJFN0VCQjo3QjRBNDE6OUQ0MkQ5OjZCMDBFNEE2IiwidmlzaXRvcl9pZCI6IjI5OTQ1MzAzMzUzMzg4NDk0NDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 57a044601cd22c17b2854d42ebb48627bba43de81019f11d82fd1fac6fef86f9 |
| hovercard-subject-tag | pull_request:4123160390 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/purescript-lua/purescript-lua/pull/292/files |
| twitter:image | https://avatars.githubusercontent.com/u/1009751?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1009751?s=400&v=4 |
| og:image:alt | Closes #225. A pattern match on a fixed-length array whose scrutinee is a manifest array literal is decidable at compile time, but the optimizer left it as a runtime length check plus element reads... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width