Skip to content

Conversation

@wojtek-t
Copy link
Contributor

@wojtek-t wojtek-t commented Oct 8, 2019

wojtekt@wojtekt-work:~/go/src/github.com/munnerz/goautoneg$ go test .
ok  	github.com/munnerz/goautoneg	

@wojtek-t
Copy link
Contributor Author

wojtek-t commented Oct 8, 2019

@munnerz - please take a look

@smarterclayton - FYI

Copy link
Owner

@munnerz munnerz left a comment

Choose a reason for hiding this comment

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

This seems good to me, although I'm not too well-versed in this code. Tracing through, the changes look to be functionally the same (and the unit tests pass!).

I'm not sure of the original author's github details, but the repository this was forked from is here: https://bitbucket.org/ww/goautoneg

autoneg.go Outdated
// Parse an Accept Header string returning a sorted list
// of clauses
func ParseAccept(header string) (accept []Accept) {
func ParseAccept(header string) (accept AcceptSlice) {
Copy link
Owner

Choose a reason for hiding this comment

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

Does it make sense to expose AcceptSlice to consumers of this package given that ParseAccept already sorts the slice anyway? Can this be kept as []Accept to avoid leaking the implementation details to the consumer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SGTM - fixed

@wojtek-t wojtek-t force-pushed the improve_performance_parse_header branch from c1a3fe0 to c4ea822 Compare October 8, 2019 14:30
Copy link
Owner

@munnerz munnerz left a comment

Choose a reason for hiding this comment

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

@smarterclayton if you think this looks good, I'll hit merge 😄

autoneg.go Outdated
a.Q = 1.0

mrp := strings.Split(part, ";")
accept := make(acceptSlice, 0, len(parts))

Choose a reason for hiding this comment

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

You can add another fast path here by not splitting (which allocates) if there's no comma.

Choose a reason for hiding this comment

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

It would actually be better to make this a loop that tokenizes parts on , if you want to avoid allocations. I did that in go-restful to bypass https://github.com/emicklei/go-restful/blob/95fec025ab35f1d26407d1a4ef1542a3bf4eb8a0/route.go#L79

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was optimizing the k8s usage TBH. And for all calls from k8s client (vast majority) there is a comma actually:
https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/client-go/rest/request.go#L140

It would actually be better to make this a loop that tokenizes parts on , if you want to avoid allocations. I did that in go-restful to bypass https://github.com/emicklei/go-restful/blob/95fec025ab35f1d26407d1a4ef1542a3bf4eb8a0/route.go#L79

I did part of it for part, but it probably makes sense to go further. Will change that.

@wojtek-t wojtek-t force-pushed the improve_performance_parse_header branch from c4ea822 to 873bfd1 Compare October 8, 2019 19:36
@wojtek-t wojtek-t force-pushed the improve_performance_parse_header branch from 873bfd1 to da14def Compare October 8, 2019 19:39
@wojtek-t
Copy link
Contributor Author

wojtek-t commented Oct 8, 2019

@smarterclayton @munnerz - PTAL

@wojtek-t
Copy link
Contributor Author

wojtek-t commented Oct 8, 2019

Also benchmark results are better now (only 2 allocations vs 5 and vs 18 without any changes)

@smarterclayton
Copy link

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants