Skip to content

Commit

Permalink
Merge pull request #30561 from hashicorp/alisdair/json-output-optimiz…
Browse files Browse the repository at this point in the history
…ation

jsonplan: Improve performance for deep objects
  • Loading branch information
alisdair committed Feb 22, 2022
2 parents 1613747 + 3e4d6b2 commit 1749228
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/command/jsonplan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func unknownAsBool(val cty.Value) cty.Value {
// Omit all of the "false"s for known values for more compact
// serialization
if !vAsBool.RawEquals(cty.False) {
vals[k.AsString()] = unknownAsBool(v)
vals[k.AsString()] = vAsBool
}
}
// The above transform may have changed the types of some of the
Expand Down
56 changes: 56 additions & 0 deletions internal/command/jsonplan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,59 @@ func TestEncodePaths(t *testing.T) {
})
}
}

func deepObjectValue(depth int) cty.Value {
v := cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("a"),
"b": cty.NumberIntVal(2),
"c": cty.True,
"d": cty.UnknownVal(cty.String),
})

result := v

for i := 0; i < depth; i++ {
result = cty.ObjectVal(map[string]cty.Value{
"a": result,
"b": result,
"c": result,
})
}

return result
}

func BenchmarkUnknownAsBool_2(b *testing.B) {
value := deepObjectValue(2)
for n := 0; n < b.N; n++ {
unknownAsBool(value)
}
}

func BenchmarkUnknownAsBool_3(b *testing.B) {
value := deepObjectValue(3)
for n := 0; n < b.N; n++ {
unknownAsBool(value)
}
}

func BenchmarkUnknownAsBool_5(b *testing.B) {
value := deepObjectValue(5)
for n := 0; n < b.N; n++ {
unknownAsBool(value)
}
}

func BenchmarkUnknownAsBool_7(b *testing.B) {
value := deepObjectValue(7)
for n := 0; n < b.N; n++ {
unknownAsBool(value)
}
}

func BenchmarkUnknownAsBool_9(b *testing.B) {
value := deepObjectValue(9)
for n := 0; n < b.N; n++ {
unknownAsBool(value)
}
}

0 comments on commit 1749228

Please sign in to comment.