Skip to content

Commit

Permalink
chore: remove reflection zero type when zero value is known
Browse files Browse the repository at this point in the history
reflection uses unsafe types and is cpu intensive. we can reduce cpu
cycles on every api call by simple comparing an empty strings equality
to the value of the AuthToken.

strings.EqualFold is a std lib solution that provides similar
functionality, but that is not necessary for comparing empty strings as
it simply uses the standard equality operator as well

Topic: rm-reflection

Relative: projquay-6620
Signed-off-by: Ross Bryan <[email protected]>
  • Loading branch information
arborite-rh committed Feb 28, 2024
1 parent c39e6d2 commit 4c07622
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 8 deletions.
4 changes: 1 addition & 3 deletions pkg/client/quay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"io/ioutil"
"net/http"
"net/url"

"github.com/quay/quay-bridge-operator/pkg/utils"
)

type QuayClient struct {
Expand Down Expand Up @@ -172,7 +170,7 @@ func (c *QuayClient) newRequest(method, path string, body interface{}) (*http.Re
}

req, err := http.NewRequest(method, u.String(), buf)
if !utils.IsZeroOfUnderlyingType(c.AuthToken) {
if c.AuthToken != "" {
req.Header.Set("Authorization", "Bearer "+c.AuthToken)
}

Expand Down
5 changes: 0 additions & 5 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ package utils

import (
"fmt"
"reflect"

"github.com/quay/quay-bridge-operator/pkg/constants"
"github.com/quay/quay-bridge-operator/pkg/logging"
corev1 "k8s.io/api/core/v1"
)

func IsZeroOfUnderlyingType(x interface{}) bool {
return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}

func RemoveItemsFromSlice(s []string, r []string) []string {

for i, v := range s {
Expand Down

0 comments on commit 4c07622

Please sign in to comment.