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

types: Support Set and SetType #126

Merged
merged 9 commits into from
Sep 14, 2021
Prev Previous commit
Next Next commit
types: Revert (Set).Equal() change, benchmark (Set).Validate(), adjus…
…t (Set).Validate() to be less O(n)^2
bflad committed Sep 13, 2021
commit bb01b8cdf07260b7982925ef6ce2153e5deae53e
9 changes: 2 additions & 7 deletions types/set.go
Original file line number Diff line number Diff line change
@@ -140,10 +140,8 @@ func (s SetType) Validate(ctx context.Context, in tftypes.Value, path *tftypes.A
continue
}

for indexInner, elemInner := range elems {
if indexInner == indexOuter {
continue
}
for indexInner := indexOuter + 1; indexInner < len(elems); indexInner++ {
elemInner := elems[indexInner]

if !elemInner.Equal(elemOuter) {
continue
@@ -251,9 +249,6 @@ func (s Set) Equal(o attr.Value) bool {
if s.ElemType == nil && other.ElemType != nil {
return false
}
if other.ElemType == nil {
return false
}
if s.ElemType != nil && !s.ElemType.Equal(other.ElemType) {
paddycarver marked this conversation as resolved.
Show resolved Hide resolved
return false
}
47 changes: 47 additions & 0 deletions types/set_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package types

import (
"context"
"strconv"
"testing"

"github.com/google/go-cmp/cmp"
@@ -297,6 +298,52 @@ func TestSetElementsAs_attributeValueSlice(t *testing.T) {
}
}

func benchmarkSetTypeValidate(b *testing.B, elementCount int) {
elements := make([]tftypes.Value, 0, elementCount)

for idx := range elements {
elements[idx] = tftypes.NewValue(tftypes.String, strconv.Itoa(idx))
}

ctx := context.Background()
in := tftypes.NewValue(
tftypes.Set{
ElementType: tftypes.String,
},
elements,
)
path := tftypes.NewAttributePath().WithAttributeName("test")
set := SetType{}

for n := 0; n < b.N; n++ {
set.Validate(ctx, in, path)
}
}

func BenchmarkSetTypeValidate10(b *testing.B) {
benchmarkSetTypeValidate(b, 10)
}

func BenchmarkSetTypeValidate100(b *testing.B) {
benchmarkSetTypeValidate(b, 100)
}

func BenchmarkSetTypeValidate1000(b *testing.B) {
benchmarkSetTypeValidate(b, 1000)
}

func BenchmarkSetTypeValidate10000(b *testing.B) {
benchmarkSetTypeValidate(b, 10000)
}

func BenchmarkSetTypeValidate100000(b *testing.B) {
benchmarkSetTypeValidate(b, 100000)
}

func BenchmarkSetTypeValidate1000000(b *testing.B) {
benchmarkSetTypeValidate(b, 1000000)
}

func TestSetTypeValidate(t *testing.T) {
t.Parallel()