Propagate newline constrains eagerly when binding states in a Solution.
One of the main ways the formatter defines its formatting style is by having constraints that prohibit newlines in some child pieces when a parent is in a given state.
The two simplest examples (which cover a large amount of code) is that when an InfixPiece or a ListPiece is in State.unsplit, they don't allow any newlines in any of their children. That constraint effectively creates the desired style which is that a split inside an infix operand or list element forces the surrounding expression to split.
Whenever we bind a parent piece to some state, we can now ask it if it allows any of its children to contain newlines. For any given child, if the answer is "no" then:
If the child is already bound to a state that contains newlines, then we know this solution is a dead end. It and every solution you can ever derive from it will contain an invalid newline.
If the child isn't bound to a state yet, we can still look at all of its states and see which of them contain newlines. Any state that does can be eliminated because we'll never successfully bind the child to that state without violating this constraint.
It may turn out that no states are left, in which case again we have a dead end solution.
Or there may be just one valid state (usually State.unsplit), and we can immediately the child to that state and do this whole process recursively for this child.
Or there may be just a couple of states left and we can at least winnow this child's states down to that list when we go to expand it later.
The end result is that even before actually formatting a solution, we can often tell if it's a dead end and discard it. If not, we can often bind a whole bunch of the bound piece's children in one go instead of having to do them one at a time and slowly formatting the interim solutions at each step.
The end result is a pretty decent improvement. Here's the micro benchmarks:
There's a slight regression on some of the tiny microbenchmarks (probably because there's some overhead traversing and binding children to State.unsplit when that solution ends up winning anyway). But the improvement to the larger ones is significant.
More interesting is the overall performance. Here's formatting the Flutter repo:
Current formatter 10.964 ========================================
This PR 8.826 ================================
Short style formatter 4.558 ================
The current formatter is 58.43% slower than the old short style formatter.
This PR is 24.22% faster than the current formatter. It's 48.36% slower than the old formatter, but it gets the formatter 33.37% of the way to the old short style one.
Propagate newline constrains eagerly when binding states in a Solution.
One of the main ways the formatter defines its formatting style is by having constraints that prohibit newlines in some child pieces when a parent is in a given state.
The two simplest examples (which cover a large amount of code) is that when an InfixPiece or a ListPiece is in State.unsplit, they don't allow any newlines in any of their children. That constraint effectively creates the desired style which is that a split inside an infix operand or list element forces the surrounding expression to split.
Whenever we bind a parent piece to some state, we can now ask it if it allows any of its children to contain newlines. For any given child, if the answer is "no" then:
If the child is already bound to a state that contains newlines, then we know this solution is a dead end. It and every solution you can ever derive from it will contain an invalid newline.
If the child isn't bound to a state yet, we can still look at all of its states and see which of them contain newlines. Any state that does can be eliminated because we'll never successfully bind the child to that state without violating this constraint.
It may turn out that no states are left, in which case again we have a dead end solution.
Or there may be just one valid state (usually State.unsplit), and we can immediately the child to that state and do this whole process recursively for this child.
Or there may be just a couple of states left and we can at least winnow this child's states down to that list when we go to expand it later.
The end result is that even before actually formatting a solution, we can often tell if it's a dead end and discard it. If not, we can often bind a whole bunch of the bound piece's children in one go instead of having to do them one at a time and slowly formatting the interim solutions at each step.
The end result is a pretty decent improvement. Here's the micro benchmarks:
There's a slight regression on some of the tiny microbenchmarks (probably because there's some overhead traversing and binding children to State.unsplit when that solution ends up winning anyway). But the improvement to the larger ones is significant.
More interesting is the overall performance. Here's formatting the Flutter repo:
The current formatter is 58.43% slower than the old short style formatter.
This PR is 24.22% faster than the current formatter. It's 48.36% slower than the old formatter, but it gets the formatter 33.37% of the way to the old short style one.