refactor: replace slice-based iteration with go 1.23 iterators #277
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
This pull request refactors the iteration mechanism throughout the codebase by replacing slice-based iterations with the new iterator feature utilizing the
iter
package. Although benchmark tests (see below) did not show a significant change in performance, the new approach streamlines the code with newly introduced patterns regarding iterators.Changes
Iteration Improvements:
Iter
methods inpkg/checks/runtime/checks.go
andpkg/checks/runtime/config.go
to return iterators (usingiter.Seq[...]
) instead of slices.Code Refactoring:
Factory Update:
pkg/factory/factory.go
so that the new iterator is used when processing configuration data.Controller Adjustments:
Shutdown
,Reconcile
, andGenerateCheckSpecs
inpkg/sparrow/controller.go
to work with the newiter.Seq
.Additional Changes:
pkg/sparrow/targets/remote/gitlab/pagination.go
by usingstrings.SplitSeq
for more efficient string splitting.Commits
This commit refactors the 'Iter' methods to return iterators instead of complete slices. We utilize the newly introduced 'iter' standard library package to achieve this. This change improves the performance of the 'Iter' methods by reducing the number of allocations and copying operations.
Signed-off-by: lvlcn-t [email protected]
For additional information look at the commits.
Tests done
I've added a benchmark test to be able to better see if there is any performance gained. You can find the test results below.
E2E tests succeeded- We have none for this (feat(e2e): add comprehensive end-to-end tests and improve CI workflows #222 will add them)Benchmark Results
The new benchmark test for the
Reconcile
function shows nearly identical results before and after the changes:Before:
~1,002,994,868 ns/op, 102,224 B/op, 1,315 allocs/op
After:
~1,003,090,300 ns/op, 102,776 B/op, 1,320 allocs/op
These results confirm that while the refactoring does not significantly change the performance characteristics, it improves the code structure by using new language features.
Before
After
TODO