Skip to content

Commit 04e512d

Browse files
authored
Merge pull request #27746 from hashicorp/alisdair/optimize-large-multi-line-string-outputs
cli: Optimize for large multi-line string outputs
2 parents 14936e6 + 943df48 commit 04e512d

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

command/format/diff.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -901,23 +901,35 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
901901
}
902902
}
903903

904-
diffLines := ctySequenceDiff(oldLines, newLines)
905-
for _, diffLine := range diffLines {
906-
p.buf.WriteString(strings.Repeat(" ", indent+2))
907-
p.writeActionSymbol(diffLine.Action)
908-
909-
switch diffLine.Action {
910-
case plans.NoOp, plans.Delete:
911-
p.buf.WriteString(diffLine.Before.AsString())
912-
case plans.Create:
913-
p.buf.WriteString(diffLine.After.AsString())
914-
default:
915-
// Should never happen since the above covers all
916-
// actions that ctySequenceDiff can return for strings
917-
p.buf.WriteString(diffLine.After.AsString())
904+
// Optimization for strings which are exactly equal: just print
905+
// directly without calculating the sequence diff. This makes a
906+
// significant difference when this code path is reached via a
907+
// writeValue call with a large multi-line string.
908+
if oldS == newS {
909+
for _, line := range newLines {
910+
p.buf.WriteString(strings.Repeat(" ", indent+4))
911+
p.buf.WriteString(line.AsString())
912+
p.buf.WriteString("\n")
913+
}
914+
} else {
915+
diffLines := ctySequenceDiff(oldLines, newLines)
916+
for _, diffLine := range diffLines {
917+
p.buf.WriteString(strings.Repeat(" ", indent+2))
918+
p.writeActionSymbol(diffLine.Action)
919+
920+
switch diffLine.Action {
921+
case plans.NoOp, plans.Delete:
922+
p.buf.WriteString(diffLine.Before.AsString())
923+
case plans.Create:
924+
p.buf.WriteString(diffLine.After.AsString())
925+
default:
926+
// Should never happen since the above covers all
927+
// actions that ctySequenceDiff can return for strings
928+
p.buf.WriteString(diffLine.After.AsString())
918929

930+
}
931+
p.buf.WriteString("\n")
919932
}
920-
p.buf.WriteString("\n")
921933
}
922934

923935
p.buf.WriteString(strings.Repeat(" ", indent)) // +4 here because there's no symbol

0 commit comments

Comments
 (0)