Skip to content

Commit

Permalink
Merge pull request #27746 from hashicorp/alisdair/optimize-large-mult…
Browse files Browse the repository at this point in the history
…i-line-string-outputs

cli: Optimize for large multi-line string outputs
  • Loading branch information
alisdair authored Feb 11, 2021
2 parents 14936e6 + 943df48 commit 04e512d
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions command/format/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,23 +901,35 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
}
}

diffLines := ctySequenceDiff(oldLines, newLines)
for _, diffLine := range diffLines {
p.buf.WriteString(strings.Repeat(" ", indent+2))
p.writeActionSymbol(diffLine.Action)

switch diffLine.Action {
case plans.NoOp, plans.Delete:
p.buf.WriteString(diffLine.Before.AsString())
case plans.Create:
p.buf.WriteString(diffLine.After.AsString())
default:
// Should never happen since the above covers all
// actions that ctySequenceDiff can return for strings
p.buf.WriteString(diffLine.After.AsString())
// Optimization for strings which are exactly equal: just print
// directly without calculating the sequence diff. This makes a
// significant difference when this code path is reached via a
// writeValue call with a large multi-line string.
if oldS == newS {
for _, line := range newLines {
p.buf.WriteString(strings.Repeat(" ", indent+4))
p.buf.WriteString(line.AsString())
p.buf.WriteString("\n")
}
} else {
diffLines := ctySequenceDiff(oldLines, newLines)
for _, diffLine := range diffLines {
p.buf.WriteString(strings.Repeat(" ", indent+2))
p.writeActionSymbol(diffLine.Action)

switch diffLine.Action {
case plans.NoOp, plans.Delete:
p.buf.WriteString(diffLine.Before.AsString())
case plans.Create:
p.buf.WriteString(diffLine.After.AsString())
default:
// Should never happen since the above covers all
// actions that ctySequenceDiff can return for strings
p.buf.WriteString(diffLine.After.AsString())

}
p.buf.WriteString("\n")
}
p.buf.WriteString("\n")
}

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

0 comments on commit 04e512d

Please sign in to comment.