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

Optimise allocations in ParseAccept #5

Open
wants to merge 6 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 15 additions & 28 deletions autoneg.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,21 @@ 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
type Accept struct {
Type, SubType string
Q float64
Params map[string]string
}

// 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 @@ -135,7 +113,6 @@ func ParseAccept(header string) acceptSlice {
continue
}

a.Params = make(map[string]string)
for len(remainingPart) > 0 {
sp, remainingPart = nextSplitElement(remainingPart, ";")
sp0, spRemaining = nextSplitElement(sp, "=")
Expand All @@ -150,15 +127,25 @@ func ParseAccept(header string) acceptSlice {
token := strings.TrimFunc(sp0, stringTrimSpaceCutset)
if token == "q" {
a.Q, _ = strconv.ParseFloat(sp1, 32)
} else {
a.Params[token] = strings.TrimFunc(sp1, stringTrimSpaceCutset)
}
}

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
95 changes: 82 additions & 13 deletions autoneg_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,102 @@
package goautoneg

import (
"math"
"testing"
)

var chrome = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"

func TestParseAccept(t *testing.T) {
func TestNegotiate(t *testing.T) {
alternatives := []string{"text/html", "image/png"}
content_type := Negotiate(chrome, alternatives)
if content_type != "image/png" {
t.Errorf("got %s expected image/png", content_type)
contentType := Negotiate(chrome, alternatives)
if contentType != "image/png" {
t.Errorf("got %s expected image/png", contentType)
}

alternatives = []string{"text/html", "text/plain", "text/n3"}
content_type = Negotiate(chrome, alternatives)
if content_type != "text/html" {
t.Errorf("got %s expected text/html", content_type)
contentType = Negotiate(chrome, alternatives)
if contentType != "text/html" {
t.Errorf("got %s expected text/html", contentType)
}

alternatives = []string{"text/n3", "text/plain"}
content_type = Negotiate(chrome, alternatives)
if content_type != "text/plain" {
t.Errorf("got %s expected text/plain", content_type)
contentType = Negotiate(chrome, alternatives)
if contentType != "text/plain" {
t.Errorf("got %s expected text/plain", contentType)
}

alternatives = []string{"text/n3", "application/rdf+xml"}
content_type = Negotiate(chrome, alternatives)
if content_type != "text/n3" {
t.Errorf("got %s expected text/n3", content_type)
contentType = Negotiate(chrome, alternatives)
if contentType != "text/n3" {
t.Errorf("got %s expected text/n3", contentType)
}
}

func TestParseAccept(t *testing.T) {
actual := ParseAccept("application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8;otherParam=blah,image/png,*/*;q=0.5")
expected := []Accept{
{
Type: "application",
SubType: "xml",
Q: 1,
},
{
Type: "application",
SubType: "xhtml+xml",
Q: 1,
},
{
Type: "image",
SubType: "png",
Q: 1,
},
{
Type: "text",
SubType: "html",
Q: 0.9,
},
{
Type: "text",
SubType: "plain",
Q: 0.8,
},
{
Type: "*",
SubType: "*",
Q: 0.5,
},
}

if len(actual) != len(expected) {
t.Fatalf("expected %d entries, but got %d in %v", len(expected), len(actual), actual)
}

for i, expectedEntry := range expected {
actualEntry := actual[i]

qDiff := math.Abs(actualEntry.Q - expectedEntry.Q)

if actualEntry.Type != expectedEntry.Type || actualEntry.SubType != expectedEntry.SubType || qDiff > 0.0001 {
t.Fatalf("expected: %v\nactual: %v\nat position %d in %v", expectedEntry, actualEntry, i, actual)
}
}
}

func BenchmarkParseAccept(b *testing.B) {
scenarios := []string{
"",
"application/json",
"application/json,text/plain",
"application/json;q=0.9,text/plain",
chrome,
}

for _, scenario := range scenarios {
b.Run(scenario, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ParseAccept(scenario)
}
})
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +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=