pattern-matching refactoring: simplify Default_env.specialize_matrix by avoiding the OrPat exception#9464
Merged
gasche merged 4 commits intoocaml:trunkfrom Apr 23, 2020
Conversation
da4b7cb to
dc068bd
Compare
gasche
commented
Apr 22, 2020
Several functions in the pattern-matching compiler do recursive "specialization" through a filter_rec helper written in tail-call style with a 'rem' parameter containing the matrix rows yet to be processed as input. Typical returns of the function are foo :: filter_rec rem to add a new output, and filter_rec (foo :: rem) to add a new input to be processed (usually by partial decomposition of the current input row). Some places would contain the declaration (let rem = filter_rec rem) to factorize outputs of the first kind, but this gives a programming style that is very confusing as `rem` may now represent either an input or an output of the filter. Using better types (as will be done farther away in the pattern-matching refactoring) avoids this problem completely: specialization then has different input and output types (typically, from general to half-simple patterns), so incorrectly mixing inputs and outputs is impossible. Yay typing.
gasche
commented
Apr 22, 2020
dc068bd to
8426fcb
Compare
Octachron
reviewed
Apr 22, 2020
Octachron
reviewed
Apr 22, 2020
…r side (This commit is more tricky than the previous ones in the patchset and requires a careful review.) This refactoring clarifies and simplifies the specialize_matrix logic by getting rid of the OrPat exception used in a higher-order way (or sometimes not used in certain matchers, when it is possible to "push" the or-pattern down in the pattern). Instead it uses an arity-based criterion to implement the or-pattern-related logic once in the specializer, instead of having to implement it in each matcher. As a result, the compiler improves a bit as it will push or-patterns down during specialization in valid situations that were not implemented before -- probably because they are not terribly important in practice: all constant and arity-1 constructs benefit from optimized or-pattern handling, in particular the following are new: - lazy patterns - non-constant polymorphic variants - size-one records and arrays
8426fcb to
6e153b1
Compare
Octachron
approved these changes
Apr 23, 2020
Member
Octachron
left a comment
There was a problem hiding this comment.
This looks good to me.
Overall, removing an exception from the control flow by generalizing an optimization is really nice.
Member
Author
|
Thanks! I will now work on the next PR. |
Default_env.specialize_matrix by avoiding exceptionsDefault_env.specialize_matrix by avoiding the OrPat exception
gasche
added a commit
to gasche/ocaml
that referenced
this pull request
Aug 11, 2020
gasche
added a commit
that referenced
this pull request
Aug 11, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is the next bit of the big pattern-matching refactoring change (#9321) after #9447 has been merged.
(cc @trefis @Octachron)
This patchset contains a bugfix, and a (tricky) refactoring that clarifies and simplifies the
specialize_matrixlogic by getting rid of theOrPatexception used in a higher-order way. Instead it uses an arity-based criterion to implement the same logic, which lets us do it once in the specializer, instead of having to implement it in each matcher. As a result, the compiler improves a bit as it will push or-patterns down during specialization in valid situations that were not implemented before. For example,lazy p | lazy qis turned intolazy (p | q)-- same non-constant polymorphic variants, etc..