Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimize Gvk.ApiVersion() #5058

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions kyaml/resid/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,10 @@ func (x Gvk) stableSortString() string {

// ApiVersion returns the combination of Group and Version
func (x Gvk) ApiVersion() string {
var sb strings.Builder
if x.Group != "" {
sb.WriteString(x.Group)
sb.WriteString("/")
return x.Group + "/" + x.Version
}
sb.WriteString(x.Version)
return sb.String()
return x.Version
}

// StringWoEmptyField returns a string representation of the GVK. Non-exist
Expand Down
18 changes: 18 additions & 0 deletions kyaml/resid/gvk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package resid

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -129,6 +130,23 @@ func TestApiVersion(t *testing.T) {
}
}

func BenchmarkApiVersion(b *testing.B) {
for i, bench := range []Gvk{
{Kind: "k"},
{Version: "v", Kind: "k"},
{Group: "g", Kind: "k"},
{Group: "g", Version: "v"},
{Group: "g", Version: "v", Kind: "k"},
{Group: "bitnami.com", Version: "v1alpha1", Kind: "SealedSecret"},
} {
b.Run(fmt.Sprintf("%d", i), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = bench.ApiVersion()
}
})
}
}

func TestStringWoEmptyField(t *testing.T) {
for _, hey := range stringTests {
assert.Equal(t, hey.r, hey.x.StringWoEmptyField())
Expand Down