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: replace unsafe with the raw method for string to bytes conversion #3936

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions auth.go
Expand Up @@ -9,8 +9,6 @@ import (
"encoding/base64"
"net/http"
"strconv"

"github.com/gin-gonic/gin/internal/bytesconv"
)

// AuthUserKey is the cookie name for user credential in basic auth.
Expand All @@ -34,7 +32,7 @@ func (a authPairs) searchCredential(authValue string) (string, bool) {
return "", false
}
for _, pair := range a {
if subtle.ConstantTimeCompare(bytesconv.StringToBytes(pair.value), bytesconv.StringToBytes(authValue)) == 1 {
if subtle.ConstantTimeCompare([]byte(pair.value), []byte(authValue)) == 1 {
return pair.user, true
}
}
Expand Down Expand Up @@ -90,7 +88,7 @@ func processAccounts(accounts Accounts) authPairs {

func authorizationHeader(user, password string) string {
base := user + ":" + password
return "Basic " + base64.StdEncoding.EncodeToString(bytesconv.StringToBytes(base))
return "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
}

// BasicAuthForProxy returns a Basic HTTP Proxy-Authorization middleware.
Expand Down
5 changes: 2 additions & 3 deletions binding/form_mapping.go
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"time"

"github.com/gin-gonic/gin/internal/bytesconv"
"github.com/gin-gonic/gin/internal/json"
)

Expand Down Expand Up @@ -239,9 +238,9 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
case multipart.FileHeader:
return nil
}
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
return json.Unmarshal([]byte(val), value.Addr().Interface())
case reflect.Map:
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
return json.Unmarshal([]byte(val), value.Addr().Interface())
case reflect.Ptr:
if !value.Elem().IsValid() {
value.Set(reflect.New(value.Type().Elem()))
Expand Down
12 changes: 6 additions & 6 deletions render/json.go
Expand Up @@ -97,9 +97,9 @@ func (r SecureJSON) Render(w http.ResponseWriter) error {
return err
}
// if the jsonBytes is array values
if bytes.HasPrefix(jsonBytes, bytesconv.StringToBytes("[")) && bytes.HasSuffix(jsonBytes,
bytesconv.StringToBytes("]")) {
if _, err = w.Write(bytesconv.StringToBytes(r.Prefix)); err != nil {
if bytes.HasPrefix(jsonBytes, []byte("[")) && bytes.HasSuffix(jsonBytes,
[]byte("]")) {
if _, err = w.Write([]byte(r.Prefix)); err != nil {
return err
}
}
Expand All @@ -126,19 +126,19 @@ func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
}

callback := template.JSEscapeString(r.Callback)
if _, err = w.Write(bytesconv.StringToBytes(callback)); err != nil {
if _, err = w.Write([]byte(callback)); err != nil {
return err
}

if _, err = w.Write(bytesconv.StringToBytes("(")); err != nil {
if _, err = w.Write([]byte("(")); err != nil {
return err
}

if _, err = w.Write(ret); err != nil {
return err
}

if _, err = w.Write(bytesconv.StringToBytes(");")); err != nil {
if _, err = w.Write([]byte(");")); err != nil {
return err
}

Expand Down
4 changes: 1 addition & 3 deletions render/text.go
Expand Up @@ -7,8 +7,6 @@ package render
import (
"fmt"
"net/http"

"github.com/gin-gonic/gin/internal/bytesconv"
)

// String contains the given interface object slice and its format.
Expand Down Expand Up @@ -36,6 +34,6 @@ func WriteString(w http.ResponseWriter, format string, data []any) (err error) {
_, err = fmt.Fprintf(w, format, data...)
return
}
_, err = w.Write(bytesconv.StringToBytes(format))
_, err = w.Write([]byte(format))
return
}
4 changes: 2 additions & 2 deletions tree.go
Expand Up @@ -93,14 +93,14 @@ func (n *node) addChild(child *node) {

func countParams(path string) uint16 {
var n uint16
s := bytesconv.StringToBytes(path)
s := []byte(path)
n += uint16(bytes.Count(s, strColon))
n += uint16(bytes.Count(s, strStar))
return n
}

func countSections(path string) uint16 {
s := bytesconv.StringToBytes(path)
s := []byte(path)
return uint16(bytes.Count(s, strSlash))
}

Expand Down