Skip to content

Commit

Permalink
v3(enhancement): remove utils.Trim* (#2030)
Browse files Browse the repository at this point in the history
stdlib functions have same performance in go1.19
  • Loading branch information
trim21 committed Aug 20, 2022
1 parent 73d0b71 commit b161f80
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 538 deletions.
16 changes: 8 additions & 8 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func (c *DefaultCtx) Accepts(offers ...string) string {
for len(header) > 0 && commaPos != -1 {
commaPos = strings.IndexByte(header, ',')
if commaPos != -1 {
spec = utils.Trim(header[:commaPos], ' ')
spec = strings.TrimLeft(header[:commaPos], " ")
} else {
spec = utils.TrimLeft(header, ' ')
spec = strings.TrimLeft(header, " ")
}
if factorSign := strings.IndexByte(spec, ';'); factorSign != -1 {
spec = spec[:factorSign]
Expand Down Expand Up @@ -508,10 +508,10 @@ func (c *DefaultCtx) IPs() (ips []string) {
for {
commaPos = bytes.IndexByte(header, ',')
if commaPos != -1 {
ips[i] = utils.Trim(c.app.getString(header[:commaPos]), ' ')
ips[i] = strings.Trim(c.app.getString(header[:commaPos]), " ")
header, i = header[commaPos+1:], i+1
} else {
ips[i] = utils.Trim(c.app.getString(header), ' ')
ips[i] = strings.Trim(c.app.getString(header), " ")
return
}
}
Expand All @@ -526,7 +526,7 @@ func (c *DefaultCtx) Is(extension string) bool {
}

return strings.HasPrefix(
utils.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), ' '),
strings.TrimLeft(utils.UnsafeString(c.fasthttp.Request.Header.ContentType()), " "),
extensionHeader,
)
}
Expand Down Expand Up @@ -597,7 +597,7 @@ func (c *DefaultCtx) Links(link ...string) {
_, _ = bb.WriteString(`; rel="` + link[i] + `",`)
}
}
c.setCanonical(HeaderLink, utils.TrimRight(c.app.getString(bb.Bytes()), ','))
c.setCanonical(HeaderLink, strings.TrimRight(c.app.getString(bb.Bytes()), ","))
bytebufferpool.Put(bb)
}

Expand Down Expand Up @@ -1245,7 +1245,7 @@ func (c *DefaultCtx) WriteString(s string) (int, error) {
// XHR returns a Boolean property, that is true, if the request's X-Requested-With header field is XMLHttpRequest,
// indicating that the request was issued by a client library (such as jQuery).
func (c *DefaultCtx) XHR() bool {
return utils.EqualFoldBytes(utils.UnsafeBytes(c.Get(HeaderXRequestedWith)), []byte("xmlhttprequest"))
return utils.EqualFold(c.Get(HeaderXRequestedWith), "xmlhttprequest")
}

// configDependentPaths set paths for route recognition and prepared paths for the user,
Expand All @@ -1267,7 +1267,7 @@ func (c *DefaultCtx) configDependentPaths() {
}
// If StrictRouting is disabled, we strip all trailing slashes
if !c.app.config.StrictRouting && len(c.detectionPathBuffer) > 1 && c.detectionPathBuffer[len(c.detectionPathBuffer)-1] == '/' {
c.detectionPathBuffer = utils.TrimRightBytes(c.detectionPathBuffer, '/')
c.detectionPathBuffer = bytes.TrimRight(c.detectionPathBuffer, "/")
}
c.detectionPath = c.app.getString(c.detectionPathBuffer)

Expand Down
5 changes: 2 additions & 3 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"time"
"unsafe"

"github.com/gofiber/fiber/v3/utils"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -223,7 +222,7 @@ func getGroupPath(prefix, path string) string {
path = "/" + path
}

return utils.TrimRight(prefix, '/') + path
return strings.TrimRight(prefix, "/") + path
}

// return valid offer for header negotiation
Expand All @@ -238,7 +237,7 @@ func getOffer(header string, offers ...string) string {
for len(header) > 0 && commaPos != -1 {
commaPos = strings.IndexByte(header, ',')
if commaPos != -1 {
spec = utils.Trim(header[:commaPos], ' ')
spec = strings.TrimSpace(header[:commaPos])
} else {
spec = header
}
Expand Down
7 changes: 3 additions & 4 deletions middleware/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils"
)

// Config defines the config for middleware.
Expand Down Expand Up @@ -133,7 +132,7 @@ func New(config ...Config) fiber.Handler {
)

if len(path) > 1 {
path = utils.TrimRight(path, '/')
path = strings.TrimRight(path, "/")
}
file, err = cfg.Root.Open(path)
if err != nil && os.IsNotExist(err) && cfg.NotFoundFile != "" {
Expand All @@ -153,7 +152,7 @@ func New(config ...Config) fiber.Handler {

// Serve index if path is directory
if stat.IsDir() {
indexPath := utils.TrimRight(path, '/') + cfg.Index
indexPath := strings.TrimRight(path, "/") + cfg.Index
index, err := cfg.Root.Open(indexPath)
if err == nil {
indexStat, err := index.Stat()
Expand Down Expand Up @@ -226,7 +225,7 @@ func SendFile(c fiber.Ctx, fs http.FileSystem, path string) (err error) {

// Serve index if path is directory
if stat.IsDir() {
indexPath := utils.TrimRight(path, '/') + ConfigDefault.Index
indexPath := strings.TrimRight(path, "/") + ConfigDefault.Index
index, err := fs.Open(indexPath)
if err == nil {
indexStat, err := index.Stat()
Expand Down
3 changes: 1 addition & 2 deletions middleware/filesystem/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils"
)

func getFileExtension(path string) string {
Expand Down Expand Up @@ -41,7 +40,7 @@ func dirList(c fiber.Ctx, f http.File) error {
fmt.Fprint(c, "<ul>")

if len(basePathEscaped) > 1 {
parentPathEscaped := html.EscapeString(utils.TrimRight(c.Path(), '/') + "/..")
parentPathEscaped := html.EscapeString(strings.TrimRight(c.Path(), "/") + "/..")
fmt.Fprintf(c, `<li><a href="%s" class="dir">..</a></li>`, parentPathEscaped)
}

Expand Down
6 changes: 3 additions & 3 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"
"unicode"

"github.com/gofiber/fiber/v3/utils"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -51,6 +50,7 @@ const (
optionalParam byte = '?' // concludes a parameter by name and makes it optional
paramStarterChar byte = ':' // start character for a parameter with name
slashDelimiter byte = '/' // separator for the route, unlike the other delimiters this character at the end can be optional
slashDelimiterStr = "/" // separator for the route, unlike the other delimiters this character at the end can be optional
escapeChar byte = '\\' // escape character
paramConstraintStart byte = '<' // start of type constraint for a parameter
paramConstraintEnd byte = '>' // end of type constraint for a parameter
Expand Down Expand Up @@ -159,7 +159,7 @@ func addParameterMetaInfo(segs []*routeSegment) []*routeSegment {
} else {
comparePart = segs[i].Const
if len(comparePart) > 1 {
comparePart = utils.TrimRight(comparePart, slashDelimiter)
comparePart = strings.TrimRight(comparePart, slashDelimiterStr)
}
}
}
Expand Down Expand Up @@ -354,7 +354,7 @@ func findNextCharsetPositionConstraint(search string, charset []byte) int {
if char == paramConstraintEnd {
constraintEnd = pos
}
//fmt.Println(string(char))
// fmt.Println(string(char))
if pos != -1 && (pos < nextPosition || nextPosition == -1) {
if pos > constraintStart && pos < constraintEnd {
nextPosition = pos
Expand Down
4 changes: 2 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (app *App) addPrefixToRoute(prefix string, route *Route) *Route {
}
// Strict routing, remove trailing slashes
if !app.config.StrictRouting && len(prettyPath) > 1 {
prettyPath = utils.TrimRight(prettyPath, '/')
prettyPath = strings.TrimRight(prettyPath, "/")
}

route.Path = prefixedPath
Expand Down Expand Up @@ -246,7 +246,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) Router {
}
// Strict routing, remove trailing slashes
if !app.config.StrictRouting && len(pathPretty) > 1 {
pathPretty = utils.TrimRight(pathPretty, '/')
pathPretty = strings.TrimRight(pathPretty, "/")
}
// Is layer a middleware?
isUse := method == methodUse
Expand Down
Loading

2 comments on commit b161f80

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: b161f80 Previous: 1830fb6 Ratio
Benchmark_AcquireCtx 1828 ns/op 1568 B/op 5 allocs/op 726.4 ns/op 1568 B/op 5 allocs/op 2.52
Benchmark_Router_Handler_Strict_Case 292.2 ns/op 0 B/op 0 allocs/op 144.1 ns/op 0 B/op 0 allocs/op 2.03
Benchmark_Router_Next 201.4 ns/op 0 B/op 0 allocs/op 91.21 ns/op 0 B/op 0 allocs/op 2.21
Benchmark_Router_Handler_CaseSensitive 292.8 ns/op 0 B/op 0 allocs/op 145.5 ns/op 0 B/op 0 allocs/op 2.01
Benchmark_Router_Handler_StrictRouting 294 ns/op 0 B/op 0 allocs/op 143.3 ns/op 0 B/op 0 allocs/op 2.05
Benchmark_Cache 16686 ns/op 49369 B/op 6 allocs/op 310.5 ns/op 16 B/op 2 allocs/op 53.74
Benchmark_Cache_AdditionalHeaders 1457 ns/op 592 B/op 9 allocs/op 410.2 ns/op 16 B/op 2 allocs/op 3.55
Benchmark_Etag 251.7 ns/op 0 B/op 0 allocs/op 120.3 ns/op 0 B/op 0 allocs/op 2.09
Benchmark_Middleware_Favicon 220.3 ns/op 3 B/op 1 allocs/op 93.64 ns/op 3 B/op 1 allocs/op 2.35
Benchmark_Limiter 792.7 ns/op 72 B/op 2 allocs/op 320.7 ns/op 8 B/op 1 allocs/op 2.47

This comment was automatically generated by workflow using github-action-benchmark.

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: b161f80 Previous: 1830fb6 Ratio
Benchmark_Cache 14768 ns/op 49368 B/op 6 allocs/op 310.5 ns/op 16 B/op 2 allocs/op 47.56
Benchmark_Cache_AdditionalHeaders 1241 ns/op 592 B/op 9 allocs/op 410.2 ns/op 16 B/op 2 allocs/op 3.03
Benchmark_Limiter 675.7 ns/op 72 B/op 2 allocs/op 320.7 ns/op 8 B/op 1 allocs/op 2.11

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.