René's URL Explorer Experiment


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

direct link

Domain: github.com

react-profiling0
react-import-mapreact,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-controllerpull_requests
route-actionfiles
fetch-noncev2:74b41aa7-1883-9a0c-96a0-370e60561854
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idD1E8:2E7EBB:7B4A41:9D42D9:6B00E4A6
html-safe-noncec57bf683ab19819ab2d33e5cadd7290fd22ccd7abbdbc6f313af973d274f3a1e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMUU4OjJFN0VCQjo3QjRBNDE6OUQ0MkQ5OjZCMDBFNEE2IiwidmlzaXRvcl9pZCI6IjI5OTQ1MzAzMzUzMzg4NDk0NDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac57a044601cd22c17b2854d42ebb48627bba43de81019f11d82fd1fac6fef86f9
hovercard-subject-tagpull_request:4123160390
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/purescript-lua/purescript-lua/pull/292/files
twitter:imagehttps://avatars.githubusercontent.com/u/1009751?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/1009751?s=400&v=4
og:image:altCloses #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_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git
octolytics-dimension-user_id131581790
octolytics-dimension-user_loginpurescript-lua
octolytics-dimension-repository_id587200487
octolytics-dimension-repository_nwopurescript-lua/purescript-lua
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id587200487
octolytics-dimension-repository_network_root_nwopurescript-lua/purescript-lua
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release624179fb8b88d26ba5bba08894eb60a1c5a1d93d
ui-targetcanary-1
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/purescript-lua/purescript-lua/pull/292/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fpull%2F292%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fpull%2F292%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=purescript-lua%2Fpurescript-lua
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/292/files
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/292/files
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/292/files
purescript-luahttps://github.com/purescript-lua
purescript-luahttps://github.com/purescript-lua/purescript-lua
Codehttps://github.com/purescript-lua/purescript-lua
Issues48 (48)https://github.com/purescript-lua/purescript-lua/issues
Pull requestshttps://github.com/purescript-lua/purescript-lua/pulls
Discussionshttps://github.com/purescript-lua/purescript-lua/discussions
Actionshttps://github.com/purescript-lua/purescript-lua/actions
Projectshttps://github.com/purescript-lua/purescript-lua/projects
Security and qualityhttps://github.com/purescript-lua/purescript-lua/security
Insightshttps://github.com/purescript-lua/purescript-lua/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2Fnew%2Fchoose
Unisayhttps://github.com/Unisay
mainhttps://github.com/purescript-lua/purescript-lua/tree/main
issue-225/fold-literal-array-readshttps://github.com/purescript-lua/purescript-lua/tree/issue-225/fold-literal-array-reads
Conversation 0 https://github.com/purescript-lua/purescript-lua/pull/292
Commits 1 https://github.com/purescript-lua/purescript-lua/pull/292/commits
Checks 2 https://github.com/purescript-lua/purescript-lua/pull/292/checks
Files changed https://github.com/purescript-lua/purescript-lua/pull/292/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/292/files
feat(optimizer): fold array length and indexing on literal arrays (#225) https://github.com/purescript-lua/purescript-lua/pull/292/files#top
Show all changes 1 commit https://github.com/purescript-lua/purescript-lua/pull/292/files
39095d3 feat(optimizer): fold array length and indexing on literal arrays (#225) Unisay Jul 24, 2026 https://github.com/purescript-lua/purescript-lua/pull/292/commits/39095d3e0f2b8d1096fad0b1ac5087e4c82c151e
Clear filters https://github.com/purescript-lua/purescript-lua/pull/292/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/292/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/292/files
20260724_120000_unisay_fold_literal_array_reads.md https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-ee8872495d8bbb1adfcc40afaf3d19381447751aa86b0913173370c0e0bae32f
Optimizer.hs https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
Query.hs https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-c2db179b779dc853e8aed558f6757abc4bb11d5527b68b571814449b5cf5e203
Spec.hs https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-241ea2421c606c31b9414fb386f9f4d1beb60055ed792935b393101edfd7b949
golden.ir https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-a50730a65be2dd43fb54fcc7256daba19c0f0d185fa91ae70fd74eb6dc88babb
golden.lua https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-f09de2d9591e7ed04c015cf5105c29cef2be557187d5d7b8ca203b70d4f11bfc
changelog.d/20260724_120000_unisay_fold_literal_array_reads.mdhttps://github.com/purescript-lua/purescript-lua/pull/292/files#diff-ee8872495d8bbb1adfcc40afaf3d19381447751aa86b0913173370c0e0bae32f
View file https://github.com/purescript-lua/purescript-lua/blob/39095d3e0f2b8d1096fad0b1ac5087e4c82c151e/changelog.d/20260724_120000_unisay_fold_literal_array_reads.md
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/292/{{ revealButtonHref }}
lib/Language/PureScript/Backend/IR/Optimizer.hshttps://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
View file https://github.com/purescript-lua/purescript-lua/blob/39095d3e0f2b8d1096fad0b1ac5087e4c82c151e/lib/Language/PureScript/Backend/IR/Optimizer.hs
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/292/{{ revealButtonHref }}
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
lib/Language/PureScript/Backend/IR/Query.hshttps://github.com/purescript-lua/purescript-lua/pull/292/files#diff-c2db179b779dc853e8aed558f6757abc4bb11d5527b68b571814449b5cf5e203
View file https://github.com/purescript-lua/purescript-lua/blob/39095d3e0f2b8d1096fad0b1ac5087e4c82c151e/lib/Language/PureScript/Backend/IR/Query.hs
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/292/{{ revealButtonHref }}
https://github.com/purescript-lua/purescript-lua/pull/292/files#diff-c2db179b779dc853e8aed558f6757abc4bb11d5527b68b571814449b5cf5e203
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/292/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.