Skip to content

Commit 01ec9d1

Browse files
committed
generic set with backing slice; passing v2 sets test
1 parent 9556574 commit 01ec9d1

File tree

10 files changed

+895
-453
lines changed

10 files changed

+895
-453
lines changed

Makefile

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
GO_ARGS := GOEXPERIMENT=rangefunc
2+
13
#----------------------------------------------------------------------------------
24
# Build
35
#----------------------------------------------------------------------------------
@@ -6,7 +8,7 @@
68

79
.PHONY: mod-download
810
mod-download:
9-
go mod download
11+
$(GO_ARGS) go mod download
1012

1113

1214
DEPSGOBIN=$(shell pwd)/_output/.bin
@@ -16,12 +18,12 @@ export PATH:=$(GOBIN):$(PATH)
1618
.PHONY: install-go-tools
1719
install-go-tools: mod-download
1820
mkdir -p $(DEPSGOBIN)
19-
go install github.com/golang/protobuf/[email protected]
20-
go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
21-
go install github.com/solo-io/[email protected]
22-
go install github.com/golang/mock/[email protected]
23-
go install github.com/onsi/ginkgo/v2/[email protected]
24-
go install golang.org/x/tools/cmd/goimports
21+
$(GO_ARGS) go install github.com/golang/protobuf/[email protected]
22+
$(GO_ARGS) go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
23+
$(GO_ARGS) go install github.com/solo-io/[email protected]
24+
$(GO_ARGS) go install github.com/golang/mock/[email protected]
25+
$(GO_ARGS) go install github.com/onsi/ginkgo/v2/[email protected]
26+
$(GO_ARGS) go install golang.org/x/tools/cmd/goimports
2527

2628
# proto compiler installation
2729
PROTOC_VERSION:=3.15.8
@@ -54,13 +56,13 @@ install-tools: install-go-tools install-protoc
5456
.PHONY: generate-code
5557
generate-code: install-tools update-licenses
5658
$(DEPSGOBIN)/protoc --version
57-
go run api/generate.go
59+
$(GO_ARGS) go run api/generate.go
5860
# the api/generate.go command is separated out to enable us to run go generate on the generated files (used for mockgen)
5961
# this re-gens test protos
60-
go test ./codegen
61-
go generate -v ./...
62+
$(GO_ARGS) go test ./codegen
63+
$(GO_ARGS) go generate -v ./...
6264
$(DEPSGOBIN)/goimports -w .
63-
go mod tidy
65+
$(GO_ARGS) go mod tidy
6466

6567
generate-changelog:
6668
@ci/changelog.sh
@@ -73,18 +75,18 @@ generate-changelog:
7375
# set TEST_PKG to run a specific test package
7476
.PHONY: run-tests
7577
run-tests:
76-
PATH=$(DEPSGOBIN):$$PATH ginkgo -r -failFast -trace -progress \
78+
$(GO_ARGS) PATH=$(DEPSGOBIN):$$PATH ginkgo -r -failFast -trace -progress \
7779
-progress \
7880
-compilers=4 \
7981
-skipPackage=$(SKIP_PACKAGES) $(TEST_PKG) \
8082
-failOnPending \
8183
-randomizeAllSpecs \
8284
-randomizeSuites \
8385
-keepGoing
84-
$(DEPSGOBIN)/goimports -w .
86+
$(GO_ARGS) $(DEPSGOBIN)/goimports -w .
8587

8688
run-test:
87-
PATH=$(DEPSGOBIN):$$PATH ginkgo $(GINKGO_FLAGS) $(TEST_PKG)
89+
$(GO_ARGS) PATH=$(DEPSGOBIN):$$PATH ginkgo $(GINKGO_FLAGS) $(TEST_PKG)
8890

8991
#----------------------------------------------------------------------------------
9092
# Third Party License Management

contrib/pkg/sets/sets.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,27 @@ func (t *threadSafeResourceSet) Delta(newSet ResourceSet) ResourceDelta {
166166
func (t *threadSafeResourceSet) Clone() ResourceSet {
167167
return t.set.Clone()
168168
}
169+
170+
// // must have GOEXPERIMENT=rangefunc enabled
171+
// // example -> for k, v := r.All2 { ... }
172+
// func (r *threadSafeResourceSet) All2() iter.Seq2[int, ezkube.ResourceId] {
173+
// return func(yield func(int, ezkube.ResourceId) bool) {
174+
// for i, resource := range r.set.set {
175+
// if !yield(i, resource) {
176+
// break
177+
// }
178+
// }
179+
// }
180+
// }
181+
182+
// // must have GOEXPERIMENT=rangefunc enabled
183+
// // example -> for v := r.All1 { ... }
184+
// func (r *threadSafeResourceSet) All1() iter.Seq[ezkube.ResourceId] {
185+
// return func(yield func(ezkube.ResourceId) bool) {
186+
// for _, resource := range r.set.set {
187+
// if !yield(resource) {
188+
// break
189+
// }
190+
// }
191+
// }
192+
// }

contrib/pkg/sets/sets_bench_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
"github.com/rotisserie/eris"
1616
things_test_io_v1 "github.com/solo-io/skv2/codegen/test/api/things.test.io/v1"
17-
v1sets "github.com/solo-io/skv2/codegen/test/api/things.test.io/v1/sets"
17+
v2sets "github.com/solo-io/skv2/contrib/pkg/sets/v2"
1818
"github.com/solo-io/skv2/pkg/ezkube"
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -40,26 +40,36 @@ func benchmarkResourcesSet(count int, b *testing.B) {
4040
CreationTimestamp: metav1.Time{Time: metav1.Now().Time.Add(time.Duration(i))},
4141
},
4242
}
43-
4443
}
4544

4645
for n := 0; n < b.N; n++ {
47-
s := v1sets.NewPaintSet(ezkube.CreationTimestampAscending, ezkube.CreationTimestampsCompare)
48-
// for _, resource := range resources {
46+
// s := v1sets.NewPaintSet(ezkube.CreationTimestampAscending, ezkube.CreationTimestampsCompare)
47+
s := v2sets.NewResourceSet[*things_test_io_v1.Paint](ezkube.ObjectsAscending, ezkube.CompareObjects)
4948
s.Insert(resources...)
50-
// }
51-
l := s.List()
52-
// l := s.List(filterResource)
49+
5350
// SortByCreationTime(l) // only for map implementation
54-
for _, r := range l {
51+
52+
i := 0
53+
for _, r := range s.List(filterResource) {
5554
r.GetName()
55+
i++
56+
}
57+
58+
if count <= filterAfter {
59+
continue
60+
}
61+
if i != filterAfter+1 {
62+
b.Fatalf("expected 20000, got %d", i)
5663
}
5764
}
5865
}
5966

67+
const filterAfter = 19999
68+
69+
// skip iterating resources > 20001
6070
func filterResource(resource *things_test_io_v1.Paint) bool {
6171
i, _ := strconv.Atoi(strings.Split(resource.GetName(), "-")[1])
62-
return i < 20001
72+
return i > filterAfter
6373
}
6474

6575
// SortByCreationTime accepts a slice of client.Object instances and sorts it by creation timestamp in ascending order.

contrib/pkg/sets/sets_internal.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sets
22

33
import (
4+
"slices"
45
"sort"
56

67
"github.com/solo-io/skv2/pkg/controllerutils"
@@ -113,23 +114,7 @@ func (r *Resources) Keys() sets.String {
113114
}
114115

115116
func (r *Resources) List(filterResource ...func(ezkube.ResourceId) bool) []ezkube.ResourceId {
116-
if len(filterResource) == 0 {
117-
return r.set
118-
}
119-
resources := make([]ezkube.ResourceId, 0, len(r.set))
120-
for _, resource := range r.set {
121-
var filtered bool
122-
for _, filter := range filterResource {
123-
if filter(resource) {
124-
filtered = true
125-
break
126-
}
127-
}
128-
if !filtered {
129-
resources = append(resources, resource)
130-
}
131-
}
132-
return resources
117+
return r.set
133118
}
134119

135120
func (r *Resources) UnsortedList(filterResource ...func(ezkube.ResourceId) bool) []ezkube.ResourceId {
@@ -162,16 +147,13 @@ func (r *Resources) Insert(resources ...ezkube.ResourceId) {
162147
}
163148

164149
// insert the resource at the determined index
165-
newSet := make([]ezkube.ResourceId, len(r.set)+1)
166-
copy(newSet, r.set[:insertIndex])
167-
newSet[insertIndex] = resource
168-
copy(newSet[insertIndex+1:], r.set[insertIndex:])
169-
r.set = newSet
150+
r.set = slices.Insert(r.set, insertIndex, resource)
170151
}
171152
}
172153

173154
// Delete removes all items from the set.
174155
func (r *Resources) Delete(items ...ezkube.ResourceId) {
156+
// slices.Delete[]()
175157
for _, item := range items {
176158
i := sort.Search(r.Length(), func(i int) bool {
177159
return r.compareFunc(r.set[i], item) >= 0
@@ -311,3 +293,27 @@ func (r *Resources) PopAny() (ezkube.ResourceId, bool) {
311293
}
312294
return nil, false
313295
}
296+
297+
// // must have GOEXPERIMENT=rangefunc enabled
298+
// // used in for v := r.All { ... }
299+
// func (r *Resources) All1() iter.Seq[ezkube.ResourceId] {
300+
// return func(yield func(ezkube.ResourceId) bool) {
301+
// for _, resource := range r.set {
302+
// if !yield(resource) {
303+
// break
304+
// }
305+
// }
306+
// }
307+
// }
308+
309+
// // must have GOEXPERIMENT=rangefunc enabled
310+
// // used in for k, v := r.All2 { ... }
311+
// func (r *Resources) All2() iter.Seq2[int, ezkube.ResourceId] {
312+
// return func(yield func(int, ezkube.ResourceId) bool) {
313+
// for i, resource := range r.set {
314+
// if !yield(i, resource) {
315+
// break
316+
// }
317+
// }
318+
// }
319+
// }

0 commit comments

Comments
 (0)