René's URL Explorer Experiment


Title: SpecConstr: specialize recursive functions on known-constructor call patterns by Unisay · Pull Request #279 · purescript-lua/purescript-lua · GitHub

Open Graph Title: SpecConstr: specialize recursive functions on known-constructor call patterns by Unisay · Pull Request #279 · purescript-lua/purescript-lua

X Title: SpecConstr: specialize recursive functions on known-constructor call patterns by Unisay · Pull Request #279 · purescript-lua/purescript-lua

Description: Closes #208. A recursive function with a constructor accumulator builds the box on every iteration and takes it apart at the top of the next one. This PR brings GHC's SpecConstr discipline to the IR: when a recursive-group member scrutinizes a parameter and its recursion passes a known constructor at that position, the pass mints a specialized copy that takes the constructor's fields as separate parameters, then rewrites every qualifying call site to it. The boxed original survives as the entry point for callers that don't know the constructor. The canonical fold now compiles to a loop with no allocation in it. Before: go = function(acc) while true do local i = acc[2] if i < n then acc = Data_Tuple_Tuple_S_w(acc[1] + i, i + 1) -- a table per iteration else return acc end end end After: go_S_sc1Tuple = function(s, i) while true do if i < n then s, i = s + i, i + 1 else return { s, i } -- the box exists only where it escapes end end end How it works The pass itself folds nothing. The specialized body is a binder-freshened copy of the original with every read of the specialized parameter replaced by a rebox (Tuple f1 f2 over the new field parameters). It rides in the specialize+dce fixpoint, so the next optimize round's case-of-known-constructor folds (#177/#214) collapse the reboxes at eliminating reads into the tag string and the field references. A rebox at a whole-value read (the exit path above) stays, which is exactly right: it allocates where the box escapes, not per iteration. Guards follow GHC's: recursive bindings only, the scrutiny requirement (specializing an unscrutinized box would just move the allocation), the pattern must come from the group's own call sites, and a per-binding cap (specConstrLimit = 3, the -fspec-constr-count analogue). Instead of iterating to a fixpoint internally, one run mints one specialization layer and the enclosing specialize+dce fixpoint provides the bounded iteration. Specializations are pattern-keyed by name ($sc), so a rerun that meets a known pattern reuses the existing binding instead of minting a duplicate, and the pass reports Unmodified once no unhandled pattern remains. Rewriting applies module-wide, not just inside the group: the entry call go (Tuple 0 0) qualifies too, which is what lets DCE drop the boxed entry entirely when nobody boxed is left calling it (visible in the golden: sumCount keeps only the specialized loop). The known-constructor resolution cluster (resolveKnownCtorApp and friends) moved from the optimizer into IR.Query so the new pass can share it without an import cycle. It now returns a CtorShape record instead of a tuple, since the specializer needs the constructor's identity to rebuild the rebox, not just its tag. Covered shapes The golden test pins four cases: a product-type Tuple accumulator (fully unboxed, boxed entry DCE'd), a sum-type Maybe accumulator (only the Just pattern recurs, tag tests fold away, the Nothing arm stays on the boxed entry), a mutually recursive ping/pong pair carrying the same pattern (both specialize, and the pair loops boxlessly), and a negative control whose boxed parameter is dead (left alone). Measurements New tuple_fold macro benchmark, n = 5e6: runtime boxed (main) specialized ideal PUC Lua 5.1 0.425s 0.169s 0.057s LuaJIT 0.0021s 0.0022s 0.0020s PUC Lua gets ~2.5x from dropping the per-iteration table build. LuaJIT is unchanged, and that's expected: its allocation sinking already elided the box inside the trace. The win there is structural (the loop bytecode carries no TNEW, so nothing depends on sinking succeeding) rather than a headline number. The remaining PUC gap to ideal is the add$w worker calls the loop still makes instead of inline +, a pre-existing shape (the main-branch build has the same calls) unrelated to this pass. Counter goldens pin the specialized shape: Bench.TupleFold.lua:8 JLOOP, blacklisted=0, and the loop body free of table construction. Tests cabal test all: 928 examples, 0 failures, including the checked pipeline runner (per-pass invariant linting) over every golden module and the hand-verified eval oracles. Property specs re-run across several seeds. bench/ci counters match the committed goldens, and the twelve pre-existing counter files are byte-identical, so no other benchmark shape moved. https://claude.ai/code/session_01FwfS3b4MkgCj54u47ruzU4

Open Graph Description: Closes #208. A recursive function with a constructor accumulator builds the box on every iteration and takes it apart at the top of the next one. This PR brings GHC's SpecConstr discipline to t...

X Description: Closes #208. A recursive function with a constructor accumulator builds the box on every iteration and takes it apart at the top of the next one. This PR brings GHC&#39;s SpecConstr discipline ...

Opengraph URL: https://github.com/purescript-lua/purescript-lua/pull/279

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:c3f9bb1d-aa8d-0942-8fcd-2c7f8650679a
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idD56E:3DD488:738C98:943FD3:6B00E4A6
html-safe-nonced3695a7b41e9bdb63973782206efeb5c241ff4a61ff241c6dc2259fba0056795
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTZFOjNERDQ4ODo3MzhDOTg6OTQzRkQzOjZCMDBFNEE2IiwidmlzaXRvcl9pZCI6IjU3OTM5ODMyNTQyOTgxNTgyNDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac1fe7cc4c5d4200a8fa1a981d7b7878cf90b456dbf271f0ba06f95ebfafbafe5c
hovercard-subject-tagpull_request:4059683354
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/279/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 #208. A recursive function with a constructor accumulator builds the box on every iteration and takes it apart at the top of the next one. This PR brings GHC's SpecConstr discipline to t...
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-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/purescript-lua/purescript-lua/pull/279/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fpull%2F279%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%2F279%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/279/files
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/279/files
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/279/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-208/spec-constrhttps://github.com/purescript-lua/purescript-lua/tree/issue-208/spec-constr
Conversation 1 https://github.com/purescript-lua/purescript-lua/pull/279
Commits 3 https://github.com/purescript-lua/purescript-lua/pull/279/commits
Checks 2 https://github.com/purescript-lua/purescript-lua/pull/279/checks
Files changed https://github.com/purescript-lua/purescript-lua/pull/279/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/279/files
SpecConstr: specialize recursive functions on known-constructor call patterns https://github.com/purescript-lua/purescript-lua/pull/279/files#top
Show all changes 3 commits https://github.com/purescript-lua/purescript-lua/pull/279/files
55be35e test(ps): pin boxed-accumulator codegen before call-pattern specializ… Unisay Jul 15, 2026 https://github.com/purescript-lua/purescript-lua/pull/279/commits/55be35e8ad46e468334ad0b125b871ce632184f0
6e42cd3 feat(ir): specialize recursive bindings on known-constructor call pat… Unisay Jul 15, 2026 https://github.com/purescript-lua/purescript-lua/pull/279/commits/6e42cd3399e3a11da1a724f8cd962db4ba1cd199
89623a2 test(bench): add tuple_fold macro benchmark for call-pattern speciali… Unisay Jul 15, 2026 https://github.com/purescript-lua/purescript-lua/pull/279/commits/89623a2fb3840465b3fec1dfc0d8f6491416b31e
Clear filters https://github.com/purescript-lua/purescript-lua/pull/279/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/279/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/279/files
fnew_Bench.TupleFold.txt https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-a8be393657476741dfab9e43b02f723aa7bfc0dc541ffe2def186479d7274eb5
trace_tuple_fold.txt https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-b03006a551d958cf2dee0f6f97358ab984fe99ae5b7edfba3cc2af86ec427492
tuple_fold.lua https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-ab4142c7ca575369f3d8ec4389b122f76ac975a8bcbd6bc1ff8eae8d88e417e3
20260715_120000_unisay_spec_constr.md https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-18d296c58ed36bfc02df70bf231044f7bfd19fb535bfc1f9e04c023a59f34ac1
Optimizer.hs https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
Query.hs https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-c2db179b779dc853e8aed558f6757abc4bb11d5527b68b571814449b5cf5e203
SpecConstr.hs https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-f6dc5b8484710315c160ee989c08f321efbbff40ae39cd9baae8d20ad3a41fd7
pslua.cabal https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-cf0302393cf6443df4429c19c0fc326753a83405d0366075a98ac2753e5e1ab2
corefn.json https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-66bfa3b3b531cde61021f0acf958b0bca85a021889a8e0dcf283a4ac4158509d
.gitignore https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-3315cea04bdb49af64845d6c2b626db9dc372ab1f72ef97065f573aea6c1c0c6
golden.txt https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-45a024337ca44ed681036e9fdd071068c6ef839c812cbe7ee6d107dfa8363fd8
golden.ir https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-5a8697c32b69161fd9b5f35c113e261ae0490d7cabdd203fe1e7d947d6452b40
golden.lua https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-071592f558dbea3b0d5dece641920d6e652b30971d89ff173826b816b2d67109
TupleFold.purs https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-dadb59ef5df203e3a98a3c77b46ee76d20217745a77f5561b3b8973434f0aa47
Test.purs https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-32fa01733760386f40b4bc59f6ab352513a09b4ceaa00e73df9f56a2993925fd
bench/goldens/fnew_Bench.TupleFold.txthttps://github.com/purescript-lua/purescript-lua/pull/279/files#diff-a8be393657476741dfab9e43b02f723aa7bfc0dc541ffe2def186479d7274eb5
View file https://github.com/purescript-lua/purescript-lua/blob/89623a2fb3840465b3fec1dfc0d8f6491416b31e/bench/goldens/fnew_Bench.TupleFold.txt
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/279/{{ revealButtonHref }}
bench/goldens/trace_tuple_fold.txthttps://github.com/purescript-lua/purescript-lua/pull/279/files#diff-b03006a551d958cf2dee0f6f97358ab984fe99ae5b7edfba3cc2af86ec427492
View file https://github.com/purescript-lua/purescript-lua/blob/89623a2fb3840465b3fec1dfc0d8f6491416b31e/bench/goldens/trace_tuple_fold.txt
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/279/{{ revealButtonHref }}
bench/macro/tuple_fold.luahttps://github.com/purescript-lua/purescript-lua/pull/279/files#diff-ab4142c7ca575369f3d8ec4389b122f76ac975a8bcbd6bc1ff8eae8d88e417e3
View file https://github.com/purescript-lua/purescript-lua/blob/89623a2fb3840465b3fec1dfc0d8f6491416b31e/bench/macro/tuple_fold.lua
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/279/{{ revealButtonHref }}
changelog.d/20260715_120000_unisay_spec_constr.mdhttps://github.com/purescript-lua/purescript-lua/pull/279/files#diff-18d296c58ed36bfc02df70bf231044f7bfd19fb535bfc1f9e04c023a59f34ac1
View file https://github.com/purescript-lua/purescript-lua/blob/89623a2fb3840465b3fec1dfc0d8f6491416b31e/changelog.d/20260715_120000_unisay_spec_constr.md
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/279/{{ revealButtonHref }}
lib/Language/PureScript/Backend/IR/Optimizer.hshttps://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
View file https://github.com/purescript-lua/purescript-lua/blob/89623a2fb3840465b3fec1dfc0d8f6491416b31e/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/279/{{ revealButtonHref }}
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-487ead9d0ca9c3e6f68e23c604b0ae9070089e0dd0f7d214cdce5fbc878f3315
lib/Language/PureScript/Backend/IR/Query.hshttps://github.com/purescript-lua/purescript-lua/pull/279/files#diff-c2db179b779dc853e8aed558f6757abc4bb11d5527b68b571814449b5cf5e203
View file https://github.com/purescript-lua/purescript-lua/blob/89623a2fb3840465b3fec1dfc0d8f6491416b31e/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/279/{{ revealButtonHref }}
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-c2db179b779dc853e8aed558f6757abc4bb11d5527b68b571814449b5cf5e203
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-c2db179b779dc853e8aed558f6757abc4bb11d5527b68b571814449b5cf5e203
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-c2db179b779dc853e8aed558f6757abc4bb11d5527b68b571814449b5cf5e203
https://github.com/purescript-lua/purescript-lua/pull/279/files#diff-c2db179b779dc853e8aed558f6757abc4bb11d5527b68b571814449b5cf5e203
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/279/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.