Skip to content

Commit

Permalink
Use golang.org/x/exp/slices.SortFunc to avoid an allocation.
Browse files Browse the repository at this point in the history
goos: darwin
goarch: arm64
pkg: github.com/munnerz/goautoneg
                                                                                                          │ original.txt │       step-2-slices-sort.txt       │
                                                                                                          │    sec/op    │   sec/op     vs base               │
ParseAccept/#00-10                                                                                          24.445n ± 0%   6.524n ± 0%  -73.31% (p=0.002 n=6)
ParseAccept/application/json-10                                                                              90.67n ± 1%   65.66n ± 0%  -27.58% (p=0.002 n=6)
ParseAccept/application/json,text/plain-10                                                                   152.7n ± 1%   125.3n ± 0%  -17.94% (p=0.002 n=6)
ParseAccept/application/json;q=0.9,text/plain-10                                                             214.5n ± 0%   188.1n ± 0%  -12.28% (p=0.002 n=6)
ParseAccept/application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5-10    636.6n ± 1%   615.6n ± 1%   -3.30% (p=0.002 n=6)
geomean                                                                                                      135.8n        90.93n       -33.05%

                                                                                                          │ original.txt │         step-2-slices-sort.txt         │
                                                                                                          │     B/op     │    B/op     vs base                    │
ParseAccept/#00-10                                                                                            24.00 ± 0%    0.00 ± 0%  -100.00% (p=0.002 n=6)
ParseAccept/application/json-10                                                                               72.00 ± 0%   48.00 ± 0%   -33.33% (p=0.002 n=6)
ParseAccept/application/json,text/plain-10                                                                   120.00 ± 0%   96.00 ± 0%   -20.00% (p=0.002 n=6)
ParseAccept/application/json;q=0.9,text/plain-10                                                              168.0 ± 0%   144.0 ± 0%   -14.29% (p=0.002 n=6)
ParseAccept/application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5-10     456.0 ± 0%   432.0 ± 0%    -5.26% (p=0.002 n=6)
geomean                                                                                                       109.7                    ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

                                                                                                          │ original.txt │         step-2-slices-sort.txt         │
                                                                                                          │  allocs/op   │ allocs/op   vs base                    │
ParseAccept/#00-10                                                                                            1.000 ± 0%   0.000 ± 0%  -100.00% (p=0.002 n=6)
ParseAccept/application/json-10                                                                               2.000 ± 0%   1.000 ± 0%   -50.00% (p=0.002 n=6)
ParseAccept/application/json,text/plain-10                                                                    2.000 ± 0%   1.000 ± 0%   -50.00% (p=0.002 n=6)
ParseAccept/application/json;q=0.9,text/plain-10                                                              3.000 ± 0%   2.000 ± 0%   -33.33% (p=0.002 n=6)
ParseAccept/application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5-10     5.000 ± 0%   4.000 ± 0%   -20.00% (p=0.002 n=6)
geomean                                                                                                       2.268                    ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean
  • Loading branch information
charleskorn committed Mar 3, 2023
1 parent f638b73 commit 9895b45
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
39 changes: 15 additions & 24 deletions autoneg.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package goautoneg

import (
"sort"
"strconv"
"strings"

"golang.org/x/exp/slices"
)

// Structure to represent a clause in an HTTP Accept Header
Expand All @@ -55,28 +56,6 @@ type Accept struct {
// acceptSlice is defined to implement sort interface.
type acceptSlice []Accept

func (slice acceptSlice) Len() int {
return len(slice)
}

func (slice acceptSlice) Less(i, j int) bool {
ai, aj := slice[i], slice[j]
if ai.Q > aj.Q {
return true
}
if ai.Type != "*" && aj.Type == "*" {
return true
}
if ai.SubType != "*" && aj.SubType == "*" {
return true
}
return false
}

func (slice acceptSlice) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}

func stringTrimSpaceCutset(r rune) bool {
return r == ' '
}
Expand Down Expand Up @@ -158,7 +137,19 @@ func ParseAccept(header string) acceptSlice {
accept = append(accept, a)
}

sort.Sort(accept)
slices.SortFunc(accept, func(a, b Accept) bool {
if a.Q > b.Q {
return true
}
if a.Type != "*" && b.Type == "*" {
return true
}
if a.SubType != "*" && b.SubType == "*" {
return true
}
return false
})

return accept
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/munnerz/goautoneg

go 1.18

require golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI=
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=

0 comments on commit 9895b45

Please sign in to comment.