Skip to content

Commit

Permalink
generic set with backing slice; passing v2 sets test
Browse files Browse the repository at this point in the history
  • Loading branch information
conradhanson committed Mar 25, 2024
1 parent 9556574 commit 01ec9d1
Show file tree
Hide file tree
Showing 10 changed files with 895 additions and 453 deletions.
30 changes: 16 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
GO_ARGS := GOEXPERIMENT=rangefunc

#----------------------------------------------------------------------------------
# Build
#----------------------------------------------------------------------------------
Expand All @@ -6,7 +8,7 @@

.PHONY: mod-download
mod-download:
go mod download
$(GO_ARGS) go mod download


DEPSGOBIN=$(shell pwd)/_output/.bin
Expand All @@ -16,12 +18,12 @@ export PATH:=$(GOBIN):$(PATH)
.PHONY: install-go-tools
install-go-tools: mod-download
mkdir -p $(DEPSGOBIN)
go install github.com/golang/protobuf/protoc-gen-go@v1.5.2
go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
go install github.com/solo-io/protoc-gen-ext@v0.0.18
go install github.com/golang/mock/mockgen@v1.4.4
go install github.com/onsi/ginkgo/v2/ginkgo@v2.9.5
go install golang.org/x/tools/cmd/goimports
$(GO_ARGS) go install github.com/golang/protobuf/protoc-gen-go@v1.5.2
$(GO_ARGS) go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
$(GO_ARGS) go install github.com/solo-io/protoc-gen-ext@v0.0.18
$(GO_ARGS) go install github.com/golang/mock/mockgen@v1.4.4
$(GO_ARGS) go install github.com/onsi/ginkgo/v2/ginkgo@v2.9.5
$(GO_ARGS) go install golang.org/x/tools/cmd/goimports

# proto compiler installation
PROTOC_VERSION:=3.15.8
Expand Down Expand Up @@ -54,13 +56,13 @@ install-tools: install-go-tools install-protoc
.PHONY: generate-code
generate-code: install-tools update-licenses
$(DEPSGOBIN)/protoc --version
go run api/generate.go
$(GO_ARGS) go run api/generate.go
# the api/generate.go command is separated out to enable us to run go generate on the generated files (used for mockgen)
# this re-gens test protos
go test ./codegen
go generate -v ./...
$(GO_ARGS) go test ./codegen
$(GO_ARGS) go generate -v ./...
$(DEPSGOBIN)/goimports -w .
go mod tidy
$(GO_ARGS) go mod tidy

generate-changelog:
@ci/changelog.sh
Expand All @@ -73,18 +75,18 @@ generate-changelog:
# set TEST_PKG to run a specific test package
.PHONY: run-tests
run-tests:
PATH=$(DEPSGOBIN):$$PATH ginkgo -r -failFast -trace -progress \
$(GO_ARGS) PATH=$(DEPSGOBIN):$$PATH ginkgo -r -failFast -trace -progress \
-progress \
-compilers=4 \
-skipPackage=$(SKIP_PACKAGES) $(TEST_PKG) \
-failOnPending \
-randomizeAllSpecs \
-randomizeSuites \
-keepGoing
$(DEPSGOBIN)/goimports -w .
$(GO_ARGS) $(DEPSGOBIN)/goimports -w .

run-test:
PATH=$(DEPSGOBIN):$$PATH ginkgo $(GINKGO_FLAGS) $(TEST_PKG)
$(GO_ARGS) PATH=$(DEPSGOBIN):$$PATH ginkgo $(GINKGO_FLAGS) $(TEST_PKG)

#----------------------------------------------------------------------------------
# Third Party License Management
Expand Down
24 changes: 24 additions & 0 deletions contrib/pkg/sets/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,27 @@ func (t *threadSafeResourceSet) Delta(newSet ResourceSet) ResourceDelta {
func (t *threadSafeResourceSet) Clone() ResourceSet {
return t.set.Clone()
}

// // must have GOEXPERIMENT=rangefunc enabled
// // example -> for k, v := r.All2 { ... }
// func (r *threadSafeResourceSet) All2() iter.Seq2[int, ezkube.ResourceId] {
// return func(yield func(int, ezkube.ResourceId) bool) {
// for i, resource := range r.set.set {
// if !yield(i, resource) {
// break
// }
// }
// }
// }

// // must have GOEXPERIMENT=rangefunc enabled
// // example -> for v := r.All1 { ... }
// func (r *threadSafeResourceSet) All1() iter.Seq[ezkube.ResourceId] {
// return func(yield func(ezkube.ResourceId) bool) {
// for _, resource := range r.set.set {
// if !yield(resource) {
// break
// }
// }
// }
// }
28 changes: 19 additions & 9 deletions contrib/pkg/sets/sets_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

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

}

for n := 0; n < b.N; n++ {
s := v1sets.NewPaintSet(ezkube.CreationTimestampAscending, ezkube.CreationTimestampsCompare)
// for _, resource := range resources {
// s := v1sets.NewPaintSet(ezkube.CreationTimestampAscending, ezkube.CreationTimestampsCompare)
s := v2sets.NewResourceSet[*things_test_io_v1.Paint](ezkube.ObjectsAscending, ezkube.CompareObjects)
s.Insert(resources...)
// }
l := s.List()
// l := s.List(filterResource)

// SortByCreationTime(l) // only for map implementation
for _, r := range l {

i := 0
for _, r := range s.List(filterResource) {
r.GetName()
i++
}

if count <= filterAfter {
continue
}
if i != filterAfter+1 {
b.Fatalf("expected 20000, got %d", i)
}
}
}

const filterAfter = 19999

// skip iterating resources > 20001
func filterResource(resource *things_test_io_v1.Paint) bool {
i, _ := strconv.Atoi(strings.Split(resource.GetName(), "-")[1])
return i < 20001
return i > filterAfter
}

// SortByCreationTime accepts a slice of client.Object instances and sorts it by creation timestamp in ascending order.
Expand Down
50 changes: 28 additions & 22 deletions contrib/pkg/sets/sets_internal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sets

import (
"slices"
"sort"

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

func (r *Resources) List(filterResource ...func(ezkube.ResourceId) bool) []ezkube.ResourceId {
if len(filterResource) == 0 {
return r.set
}
resources := make([]ezkube.ResourceId, 0, len(r.set))
for _, resource := range r.set {
var filtered bool
for _, filter := range filterResource {
if filter(resource) {
filtered = true
break
}
}
if !filtered {
resources = append(resources, resource)
}
}
return resources
return r.set
}

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

// insert the resource at the determined index
newSet := make([]ezkube.ResourceId, len(r.set)+1)
copy(newSet, r.set[:insertIndex])
newSet[insertIndex] = resource
copy(newSet[insertIndex+1:], r.set[insertIndex:])
r.set = newSet
r.set = slices.Insert(r.set, insertIndex, resource)
}
}

// Delete removes all items from the set.
func (r *Resources) Delete(items ...ezkube.ResourceId) {
// slices.Delete[]()
for _, item := range items {
i := sort.Search(r.Length(), func(i int) bool {
return r.compareFunc(r.set[i], item) >= 0
Expand Down Expand Up @@ -311,3 +293,27 @@ func (r *Resources) PopAny() (ezkube.ResourceId, bool) {
}
return nil, false
}

// // must have GOEXPERIMENT=rangefunc enabled
// // used in for v := r.All { ... }
// func (r *Resources) All1() iter.Seq[ezkube.ResourceId] {
// return func(yield func(ezkube.ResourceId) bool) {
// for _, resource := range r.set {
// if !yield(resource) {
// break
// }
// }
// }
// }

// // must have GOEXPERIMENT=rangefunc enabled
// // used in for k, v := r.All2 { ... }
// func (r *Resources) All2() iter.Seq2[int, ezkube.ResourceId] {
// return func(yield func(int, ezkube.ResourceId) bool) {
// for i, resource := range r.set {
// if !yield(i, resource) {
// break
// }
// }
// }
// }
Loading

0 comments on commit 01ec9d1

Please sign in to comment.