Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonplan: Improve performance for deep objects #30561

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
}