-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perf(reporthandling): uniqueStrings & trimunique (#81)
* introduced internal package for optimized versions of slice unique & trim * profiled & benchmarked internal/slices package * moved helpers call for listing to the internal package * adapted reporthandling to use internal/slices * removed erroneous comment about make map, corrected benchmark * improved slice alloc on trimmed resources * fixup listing failed to unique Signed-off-by: Frederic BIDON <[email protected]> * fix: renamed package for newly added test Signed-off-by: Frederic BIDON <[email protected]> --------- Signed-off-by: Frederic BIDON <[email protected]>
- Loading branch information
Showing
16 changed files
with
791 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v1 | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v1 | ||
package helpers | ||
|
||
import ( | ||
"reflect" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v1 | ||
package helpers | ||
|
||
import ( | ||
"github.com/armosec/armoapi-go/armotypes" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v1 | ||
package helpers | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package helpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kubescape/opa-utils/reporthandling/apis" | ||
) | ||
|
||
func BenchmarkToUniqueResources(b *testing.B) { | ||
listA := mockAllListsA() | ||
listA.Append(apis.StatusExcluded, "b") | ||
listA.Append(apis.StatusExcluded, "b") | ||
listA.Append(apis.StatusExcluded, "b") | ||
b.ResetTimer() | ||
b.ReportAllocs() | ||
|
||
for n := 0; n < b.N; n++ { | ||
listA.ToUniqueResources() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v1 | ||
package helpers | ||
|
||
func MockAllListsForIntegration() *AllLists { | ||
return &AllLists{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v1 | ||
package helpers | ||
|
||
import "github.com/kubescape/opa-utils/reporthandling/apis" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v1 | ||
package helpers | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# slices | ||
|
||
Internal utility functions that manipulate slices of strings. | ||
|
||
All these methods alter their input slice and do not allocate extra memory. | ||
|
||
`UniqueStrings` is provided as a faster equivalent to `github.com/armosec/utils-go/str.SliceStringToUnique`. | ||
|
||
`Trim` is the same as the previously private function `trimUnique()` with similar performances. Code is just more straightforward. | ||
|
||
`TrimStable` has the same intent as `Trim` but does not alter the order of the input. | ||
|
||
`TrimUnique` and `TrimStableUnique` combine `UniqueStrings` and `Trim` (resp. `TrimStable`) in a single iteration of the input slice. | ||
|
||
## Benchmarks | ||
|
||
``` | ||
go test -v -run Bench -bench . -benchtime 1s | ||
goos: linux | ||
goarch: amd64 | ||
pkg: github.com/kubescape/opa-utils/reporthandling/internal/slices | ||
cpu: AMD Ryzen 7 5800X 8-Core Processor | ||
BenchmarkUnique | ||
BenchmarkUnique/UniqueStrings_x_8 | ||
BenchmarkUnique/UniqueStrings_x_8-16 6007258 192.3 ns/op 0 B/op 0 allocs/op | ||
BenchmarkUnique/UniqueStrings_x_16 | ||
BenchmarkUnique/UniqueStrings_x_16-16 3084657 386.0 ns/op 0 B/op 0 allocs/op | ||
BenchmarkUnique/UniqueStrings_x_32 | ||
BenchmarkUnique/UniqueStrings_x_32-16 3148867 387.5 ns/op 0 B/op 0 allocs/op | ||
BenchmarkUnique/SliceStringToUnique_x_8 | ||
BenchmarkUnique/SliceStringToUnique_x_8-16 2188087 543.4 ns/op 64 B/op 1 allocs/op | ||
BenchmarkUnique/SliceStringToUnique_x_16 | ||
BenchmarkUnique/SliceStringToUnique_x_16-16 2219115 544.2 ns/op 64 B/op 1 allocs/op | ||
BenchmarkUnique/SliceStringToUnique_x_32 | ||
BenchmarkUnique/SliceStringToUnique_x_32-16 2175615 548.6 ns/op 64 B/op 1 allocs/op | ||
BenchmarkTrim | ||
BenchmarkTrim/Trim_x_8 | ||
BenchmarkTrim/Trim_x_8-16 5328590 222.1 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/Trim_x_16 | ||
BenchmarkTrim/Trim_x_16-16 2748080 422.4 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/Trim_x_32 | ||
BenchmarkTrim/Trim_x_32-16 2732326 425.8 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimStable_x_8 | ||
BenchmarkTrim/TrimStable_x_8-16 5337442 216.5 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimStable_x_16 | ||
BenchmarkTrim/TrimStable_x_16-16 2752138 435.2 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimStable_x_32 | ||
BenchmarkTrim/TrimStable_x_32-16 2770256 432.0 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimSwap_x_8_(original_version) | ||
BenchmarkTrim/TrimSwap_x_8_(original_version)-16 5299237 225.4 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimSwap_x_16_(original_version) | ||
BenchmarkTrim/TrimSwap_x_16_(original_version)-16 2744328 437.5 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimSwap_x_32_(original_version) | ||
BenchmarkTrim/TrimSwap_x_32_(original_version)-16 2800281 440.1 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimUnique_x_8 | ||
BenchmarkTrim/TrimUnique_x_8-16 4220919 284.7 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimUnique_x_16 | ||
BenchmarkTrim/TrimUnique_x_16-16 2469658 482.6 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimUnique_x_32 | ||
BenchmarkTrim/TrimUnique_x_32-16 2408644 494.1 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimStableUnique_x_8 | ||
BenchmarkTrim/TrimStableUnique_x_8-16 4250454 288.0 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimStableUnique_x_16 | ||
BenchmarkTrim/TrimStableUnique_x_16-16 2390988 486.5 ns/op 0 B/op 0 allocs/op | ||
BenchmarkTrim/TrimStableUnique_x_32 | ||
BenchmarkTrim/TrimStableUnique_x_32-16 2416376 495.0 ns/op 0 B/op 0 allocs/op | ||
PASS | ||
ok | ||
``` |
Oops, something went wrong.