Improvement: Add instrumentation to allow warning on files that take long to format#989
Open
Improvement: Add instrumentation to allow warning on files that take long to format#989
Conversation
MTelling
commented
Jan 22, 2024
| private final Optional<String> assumeFilename; | ||
| private final boolean reflowLongStrings; | ||
| private final boolean outputReplacements; | ||
| private final Optional<Long> warnOnExpensiveFileDurationMillis; |
Author
There was a problem hiding this comment.
I would probably long term want to be able to set a failIfDurationExceeded option as well. Ideas for if this flag should be altered or if it's just another config?
added 3 commits
January 22, 2024 19:09
…r/palantir-java-format into mt/add-instrumentation-for-files
ash211
reviewed
Jan 22, 2024
Contributor
ash211
left a comment
There was a problem hiding this comment.
This would be helpful for a project I'm working on where we are spending nearly as much time on spotless as we are on compilation!
- 37m 15.661s on Spotless
- 45m 5.492s on Compile
Comment on lines
+22
to
+24
| long start = System.currentTimeMillis(); | ||
| String result = delegate.call(); | ||
| Duration duration = Duration.ofMillis(System.currentTimeMillis() - start); |
Contributor
There was a problem hiding this comment.
if you have Guava on the classpath already, use Stopwatch?
| } | ||
|
|
||
| /** Returns the number of millis to allow processing of a single file to take before warning. */ | ||
| Optional<Long> warnOnExpensiveFileDurationMillis() { |
Contributor
There was a problem hiding this comment.
Can this be Optional<Duration> ?
| private final Optional<String> assumeFilename; | ||
| private final boolean reflowLongStrings; | ||
| private final boolean outputReplacements; | ||
| private final Optional<Long> warnOnExpensiveFileDurationMillis; |
| if (parameters.warnOnExpensiveFileDurationMillis().isPresent() | ||
| && fileResult.duration().toMillis() | ||
| > parameters.warnOnExpensiveFileDurationMillis().get()) { | ||
| errWriter.println(path + ": took " + fileResult.duration().toMillis() + "ms to format, " |
Contributor
There was a problem hiding this comment.
nit: put WARN in this message, so it shows up when people Cmd+F for warnings (which log but do not fail builds)
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.
Before this PR
We have noticed the formatter becomes really slow on functions that nest a lot, causing some files to take +10 seconds to format.
A simple refactor can often fix these issues but it can be hard to identify and enforce that files are kept in a shape that formatter formats quickly.
This PR adds instrumentation to file formatting and, if enabled, prints errors to the console for files that exceed the specified threshold.
After this PR
==COMMIT_MSG==
Add instrumentation to allow warning on files that take long to format
==COMMIT_MSG==
Possible downsides?
I'd rather fix the formatter here but that might take a lot more time and in places where we've seen issues, the code could use a refactor anyway (deeply nested code is not very pretty).