Skip to content

Commit

Permalink
Merge pull request #7 from issmirnov/optimizations
Browse files Browse the repository at this point in the history
convert loop to map lookup
  • Loading branch information
issmirnov committed Mar 11, 2017
2 parents 3ae6256 + cdca9d5 commit fa74e06
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 14 additions & 15 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,23 @@ func expand(c *gabs.Container, token *list.Element, res *bytes.Buffer) {
}
res.WriteString("/")
children, _ := c.ChildrenMap()
for key, child := range children {
if key == token.Value {
p, skip, err := getPrefix(child)
if err != nil {
fmt.Println(err.Error())
child, ok := children[token.Value.(string)]
if ok {
p, skip, err := getPrefix(child)
if err != nil {
fmt.Println(err.Error())
return
}
res.WriteString(p)
if skip {
token = token.Next()
if token == nil {
return
}
res.WriteString(p)
if skip {
token = token.Next()
if token == nil {
return
}
res.WriteString(token.Value.(string))
}
expand(child, token.Next(), res)
return
res.WriteString(token.Value.(string))
}
expand(child, token.Next(), res)
return
}
// handle base case if no keys matched
res.WriteString(token.Value.(string))
Expand Down
4 changes: 2 additions & 2 deletions web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func BenchmarkIndexHandler(b *testing.B) {
context := &context{config: c}
appHandler := &ctxWrapper{context, IndexHandler}
handler := http.Handler(appHandler)
req, _ := http.NewRequest("GET", "/p", nil)
req.Host = "sd"
req, _ := http.NewRequest("GET", "/z", nil)
req.Host = "g"
rr := httptest.NewRecorder()
for n := 0; n < b.N; n++ {
handler.ServeHTTP(rr, req)
Expand Down

0 comments on commit fa74e06

Please sign in to comment.