Skip to content

Commit 42486ac

Browse files
authored
engine: don't construct diff string if it isn't requested (#287)
* engine: don't calculate diff if it isn't requested The original code would calculate the diff on every file regardless of whether it was asked for because of the way the debug logging code was structured. This PR adds a way to manually check for active debug code, and uses that to avoid calculating the diff unnecessarily. * clarify comment wording
1 parent 00c0bef commit 42486ac

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

engine/consecutive_engine.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ func (e *ConsecutiveEngine) FormatContent(content []byte) ([]byte, error) {
3939
func (e *ConsecutiveEngine) Format(paths []string) (fmt.Stringer, error) {
4040
formatDiffs, formatErrs := e.formatAll(paths)
4141

42-
// Debug format diff output.
43-
logger.Debug(
44-
logger.DebugCodeDiffs,
45-
fmt.Sprintf("The following files were modified:\n%s", formatDiffs.StrOutput()),
46-
)
42+
// Debug format diff output. Manually check for the debug code
43+
// to be active since the diff string construction can be
44+
// performance intensive, thus don't want to calculate it if
45+
// it's not needed.
46+
if logger.DebugCodeIsActive(logger.DebugCodeDiffs) {
47+
logger.Debug(
48+
logger.DebugCodeDiffs,
49+
fmt.Sprintf("The following files were modified:\n%s", formatDiffs.StrOutput()),
50+
)
51+
}
4752

4853
if len(formatErrs) > 0 {
4954
if e.ContinueOnError {

internal/logger/debug.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func ActivateDebugCode(code string) {
4545
}
4646
}
4747

48+
func DebugCodeIsActive(code DebugCode) bool {
49+
return activeDebugCodes.Contains(code)
50+
}
51+
4852
// Debug prints a message if the given debug code is active.
4953
func Debug(code DebugCode, msg string, args ...any) {
5054
if activeDebugCodes.Contains(code) {

0 commit comments

Comments
 (0)