Title: Case-of-known-constructor through a let-bound scrutinee (#214) by Unisay · Pull Request #216 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Case-of-known-constructor through a let-bound scrutinee (#214) by Unisay · Pull Request #216 · purescript-lua/purescript-lua
X Title: Case-of-known-constructor through a let-bound scrutinee (#214) by Unisay · Pull Request #216 · purescript-lua/purescript-lua
Description: Closes #214. Builds on #213 (now on main). What reduceKnownConstructor (#177) only fires on a constructor that is syntactically in place, and a scrutinee read more than once is never in place. Beta reduction Let-binds a non-trivial argument rather than substitute it into every occurrence (the #167 discipline), and a dictionary method reads its scrutinee several times: a tag test, a payload read, a fallthrough tag test. So an inlined and beta-reduced method lands on let v = Just (x + 1) in if justTag == ReflectCtor v then … v.value0 … else if nothingTag == ReflectCtor v then Nothing else … and nothing folds. The rules see ReflectCtor (Ref v) and ObjectProp (Ref v) "value0", never the constructor, so the whole match stays opaque behind the Let. That is a near-zero win for exactly the multiply-read-scrutinee methods (bind, apply), which are most of them. How propagateKnownCtorThroughLet propagates a known constructor from a Standalone Let binding into the binder's reads. When the RHS is a saturated Ctor application and the binder is read only through constructor-eliminating reads: each ReflectCtor v folds to the tag string (sum types only, as in reduceKnownConstructor); each field read (ObjectProp v "valueᵢ", DataArgumentByIndex i v) folds to a fresh field-binder bound once to the iᵗʰ argument, GHC's case-binder to field-binder split, so a non-trivial argument read at several sites is evaluated once rather than copied to each site; the binding is dropped, its unread arguments discarded with the same licence reduceKnownConstructor has for a field read's siblings. Trivial and dead field-binders then inline or DCE away, and with the reads folded the surrounding Eq/if meets constant folding and the unreachable-branch rules, collapsing the decision tree to its live arm. That is the case-of-known-constructor payoff of #177, now reached through the Let. The rule declines when the binder is read as a whole value anywhere (a sibling binding, or a non-eliminating position such as an argument to a function): dropping it would dangle the reference, and keeping it while binding the fields would duplicate the arguments. A product-type ReflectCtor read has no tag row to fold to, so it too leaves a whole-value read and the rule declines. It also guards against a self-referencing RHS, which cannot arise under the global uniqueness condition but can on the non-GUC input optimizedExpression is exercised on directly. Why now This is the enabler that makes dictionary-method inlining (#180) pay off. Once #180 inlines a method and beta reduction Let-binds the dictionary value, the payload is read through exactly this Let-bound shape; without this rule #180 would inline the method but leave the match opaque. #180 stacks on this branch. Verification New optimizer specs cover the fold and its refusals: the tag fold unlocking the live branch, the payload dropped when only the tag is read, a field read at several sites bound once (proving no duplication), the whole-value and partial-application declines, and an end-to-end collapse to straight-line code run through the checked pipeline so a GUC or scoping violation would fail rather than pass silently. A randomized property stresses the fold across scalar payloads, checking the binder is eliminated and the free-reference multiset never grows. Full suite green (cabal test all): 702 examples, 0 failures, and the IR optimizer specs stay green across several seeds. No structural golden moved and no eval oracle moved, matching the near-zero standalone impact the issue predicts, since the shape appears once method inlining lands. fourmolu and hlint clean, warning-free build.
Open Graph Description: Closes #214. Builds on #213 (now on main). What reduceKnownConstructor (#177) only fires on a constructor that is syntactically in place, and a scrutinee read more than once is never in place. Beta...
X Description: Closes #214. Builds on #213 (now on main). What reduceKnownConstructor (#177) only fires on a constructor that is syntactically in place, and a scrutinee read more than once is never in place. Beta...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/pull/216
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:4f90702d-25a1-7cc4-60f4-267e3c0f99c3 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | EA90:2F9718:82EA0B:B119BA:6B00E4A6 |
| html-safe-nonce | 4a72af1506726480cdd14c6a27bb467392914b402faf3b2066ab302c77cf2737 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTkwOjJGOTcxODo4MkVBMEI6QjExOUJBOjZCMDBFNEE2IiwidmlzaXRvcl9pZCI6IjI3OTAyNTMzNzEzODk0MzcwOTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | abdf213beadf2bc91e5303287771940a68e7d5939f9f19f5db96b956e263c0fd |
| hovercard-subject-tag | pull_request:4020982577 |
| 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/216/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 #214. Builds on #213 (now on main). What reduceKnownConstructor (#177) only fires on a constructor that is syntactically in place, and a scrutinee read more than once is never in place. Beta... |
| 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 | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width