Skip to content

Commit 4dfcd0e

Browse files
committed
Use simple heuristics to reduce expontential blowup
1 parent c46b521 commit 4dfcd0e

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- [**Luau**] Increased the shape size of the expression in a type assertion so that it will correctly hang if over width ([#466](https://github.com/JohnnyMorganz/StyLua/issues/466))
1818
- Fixed binary expression in a table field containing a comment being collapsed leading to malformed formatted ([#471](https://github.com/JohnnyMorganz/StyLua/issues/471))
1919
- Fixed end parentheses of a function call with a multiline comment internally being expanded onto a new line unnecessarily ([#473](https://github.com/JohnnyMorganz/StyLua/issues/473))
20+
- Fixed severe performance regression with complex nested function calls ([#477](https://github.com/JohnnyMorganz/StyLua/issues/477))
2021

2122
## [0.13.1] - 2022-04-11
2223
### Fixed

src/formatters/functions.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,17 @@ fn function_args_multiline_heuristic(
184184

185185
// Format all the arguments on an infinite width, so that we can prepare them and check to see whether they
186186
// need expanding. We will ignore punctuation for now
187-
let first_iter_formatted_arguments = arguments
188-
.iter()
189-
.map(|argument| format_expression(ctx, argument, shape.with_infinite_width()));
187+
let first_iter_formatted_arguments = arguments.iter().map(|argument| {
188+
if shape.using_simple_heuristics() {
189+
argument.to_owned()
190+
} else {
191+
format_expression(
192+
ctx,
193+
argument,
194+
shape.with_simple_heuristics().with_infinite_width(),
195+
)
196+
}
197+
});
190198

191199
// Apply some heuristics to determine whether we should expand the function call
192200
let mut singleline_shape = shape + PAREN_LEN;

src/shape.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ pub struct Shape {
102102
offset: usize,
103103
/// The maximum number of characters we want to fit on a line. This is inferred from the configuration
104104
column_width: usize,
105+
/// Whether we should use simple heuristic checking.
106+
/// This is enabled when we are calling within a heuristic itself, to reduce the exponential blowup
107+
simple_heuristics: bool,
105108
}
106109

107110
impl Shape {
@@ -111,6 +114,7 @@ impl Shape {
111114
indent: Indent::new(ctx),
112115
offset: 0,
113116
column_width: ctx.config().column_width,
117+
simple_heuristics: false,
114118
}
115119
}
116120

@@ -179,6 +183,19 @@ impl Shape {
179183
}
180184
}
181185

186+
/// Whether simple heuristics should be used when calculating formatting shape
187+
/// This is to reduce the expontential blowup of discarded test formatting
188+
pub fn using_simple_heuristics(&self) -> bool {
189+
self.simple_heuristics
190+
}
191+
192+
pub fn with_simple_heuristics(&self) -> Shape {
193+
Self {
194+
simple_heuristics: true,
195+
..*self
196+
}
197+
}
198+
182199
/// Resets the offset for the shape
183200
pub fn reset(&self) -> Shape {
184201
Self { offset: 0, ..*self }

0 commit comments

Comments
 (0)