Skip to content

Commit

Permalink
feat: use rangefunc for iterators
Browse files Browse the repository at this point in the history
Give rangefunc a try.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
  • Loading branch information
utkuozdemir authored and DmitriyMV committed Aug 14, 2024
1 parent 3fef540 commit a919281
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
24 changes: 7 additions & 17 deletions pkg/controller/conformance/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ func (ctrl *IntToStrController) Run(ctx context.Context, r controller.Runtime, _
return fmt.Errorf("error listing objects: %w", err)
}

for iter := intList.Iterator(); iter.Next(); {
intRes := iter.Value()

for intRes := range intList.All() {
strRes := NewStrResource(ctrl.TargetNamespace, intRes.Metadata().ID(), "")

switch intRes.Metadata().Phase() {
Expand Down Expand Up @@ -183,9 +181,7 @@ func (ctrl *StrToSentenceController) Run(ctx context.Context, r controller.Runti
return fmt.Errorf("error listing objects: %w", err)
}

for iter := strList.Iterator(); iter.Next(); {
strRes := iter.Value()

for strRes := range strList.All() {
sentenceRes := NewSentenceResource(ctrl.TargetNamespace, strRes.Metadata().ID(), "")

switch strRes.Metadata().Phase() {
Expand Down Expand Up @@ -287,8 +283,8 @@ func (ctrl *SumController) Run(ctx context.Context, r controller.Runtime, _ *zap

var sum int

for iter := intList.Iterator(); iter.Next(); {
sum += iter.Value().Value()
for val := range intList.All() {
sum += val.Value()
}

if err = safe.WriterModify(ctx, r, NewIntResource(ctrl.TargetNamespace, ctrl.TargetID, 0), func(r *IntResource) error {
Expand Down Expand Up @@ -409,9 +405,7 @@ func (ctrl *IntDoublerController) Run(ctx context.Context, r controller.Runtime,
return fmt.Errorf("error listing objects: %w", err)
}

for iter := intList.Iterator(); iter.Next(); {
intRes := iter.Value()

for intRes := range intList.All() {
outRes := NewIntResource(ctrl.TargetNamespace, intRes.Metadata().ID(), 0)

if err = safe.WriterModify(ctx, r, outRes, func(r *IntResource) error {
Expand Down Expand Up @@ -480,9 +474,7 @@ func (ctrl *ModifyWithResultController) Run(ctx context.Context, r controller.Ru
return fmt.Errorf("error listing objects: %w", err)
}

for iter := strList.Iterator(); iter.Next(); {
strRes := iter.Value()

for strRes := range strList.All() {
id := strRes.Metadata().ID() + "-out"
val := strRes.Value() + "-modified"

Expand Down Expand Up @@ -575,9 +567,7 @@ func (ctrl *MetricsController) Run(ctx context.Context, r controller.Runtime, _
return fmt.Errorf("error listing objects: %w", err)
}

for iter := intList.Iterator(); iter.Next(); {
intRes := iter.Value()

for intRes := range intList.All() {
if intRes.Value() == 42 {
return fmt.Errorf("magic number caused controller to crash")
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/controller/generic/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func (ctrl *Controller[I]) Run(ctx context.Context, r controller.Runtime, logger

var multiErr error

for iter := inputList.Iterator(); iter.Next(); {
err := ctrl.processInput(ctx, r, logger, iter.Value())
for val := range inputList.All() {
err := ctrl.processInput(ctx, r, logger, val)
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
Expand Down Expand Up @@ -308,9 +308,7 @@ func RemoveOutputs[O generic.ResourceWithRD, I generic.ResourceWithRD](

var inTearDown int

for iter := list.Iterator(); iter.Next(); {
out := iter.Value()

for out := range list.All() {
owner := out.Metadata().Owner()

var allowedOwner bool
Expand Down
8 changes: 2 additions & 6 deletions pkg/controller/generic/transform/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,7 @@ func (ctrl *Controller[Input, Output]) processInputs(
}

// create outputs based on inputs
for iter := inputItems.Iterator(); iter.Next(); {
in := iter.Value()

for in := range inputItems.All() {
mappedOut, present := ctrl.mapFunc(in).Get()
if !present {
// skip this resource
Expand Down Expand Up @@ -399,9 +397,7 @@ func (ctrl *Controller[Input, Output]) cleanupOutputs(
return fmt.Errorf("error listing output resources: %w", err)
}

for iter := outputItems.Iterator(); iter.Next(); {
out := iter.Value()

for out := range outputItems.All() {
// output not owned by this controller, skip it
if out.Metadata().Owner() != ctrl.Name() {
delete(runState.removeInputFinalizers, out.Metadata().ID())
Expand Down
18 changes: 16 additions & 2 deletions pkg/safe/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ package safe
import (
"context"
"fmt"
"iter" //nolint:gci
"slices"

"github.com/siderolabs/gen/channel"
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/gen/xslices" //nolint:gci

"github.com/cosi-project/runtime/pkg/controller/generic"
"github.com/cosi-project/runtime/pkg/resource"
Expand Down Expand Up @@ -294,10 +295,23 @@ func (l *List[T]) Find(fn func(T) bool) (T, bool) {
func (l *List[T]) Swap(i, j int) { l.list.Items[i], l.list.Items[j] = l.list.Items[j], l.list.Items[i] }

// Iterator returns a new iterator over the list.
//
// Deprecated: use [List.All] instead.
func (l *List[T]) Iterator() ListIterator[T] {
return ListIterator[T]{pos: 0, list: *l}
}

// All returns a new rangefunc iterator over the list.
func (l *List[T]) All() iter.Seq[T] {
return func(yield func(T) bool) {
for i := range l.list.Items {
if !yield(l.Get(i)) {
return
}
}
}
}

// ListIterator is a generic iterator over resource.Resource slice.
type ListIterator[T any] struct {
list List[T]
Expand All @@ -306,7 +320,7 @@ type ListIterator[T any] struct {

// IteratorFromList returns a new iterator over the given list.
//
// Deprecated: use [List.Iterator] instead.
// Deprecated: use [List.All] instead.
func IteratorFromList[T any](list List[T]) ListIterator[T] {
return ListIterator[T]{pos: 0, list: list}
}
Expand Down

0 comments on commit a919281

Please sign in to comment.