-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from pressly/zeroalloc
Reuse root context in a sync pool to reduce GC: zero-alloc routing
- Loading branch information
Showing
11 changed files
with
189 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Changelog | ||
|
||
## 0.9.0 (2016-03-31) | ||
|
||
- Reuse context objects via sync.Pool for zero-allocation routing [#33](https://github.com/pressly/chi/pull/33) | ||
- BREAKING NOTE: due to subtle API changes, previously `chi.URLParams(ctx)["id"]` used to access url parameters | ||
has changed to: `chi.URLParam(ctx, "id")` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package chi | ||
|
||
import "golang.org/x/net/context" | ||
|
||
var _ context.Context = &Context{} | ||
|
||
type ctxKey int | ||
|
||
const ( | ||
rootCtxKey ctxKey = iota | ||
) | ||
|
||
type Context struct { | ||
context.Context | ||
|
||
// URL parameter key and values | ||
pkeys, pvalues []string | ||
|
||
// Routing path override | ||
routePath string | ||
} | ||
|
||
func newContext() *Context { | ||
rctx := &Context{} | ||
ctx := context.WithValue(context.Background(), rootCtxKey, rctx) | ||
rctx.Context = ctx | ||
return rctx | ||
} | ||
|
||
func (x *Context) Param(key string) string { | ||
for i, k := range x.pkeys { | ||
if k == key { | ||
return x.pvalues[i] | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func (x *Context) addParam(key string, value string) { | ||
x.pkeys = append(x.pkeys, key) | ||
x.pvalues = append(x.pvalues, value) | ||
} | ||
|
||
func (x *Context) delParam(key string) string { | ||
for i, k := range x.pkeys { | ||
if k == key { | ||
v := x.pvalues[i] | ||
x.pkeys = append(x.pkeys[:i], x.pkeys[i+1:]...) | ||
x.pvalues = append(x.pvalues[:i], x.pvalues[i+1:]...) | ||
return v | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func (x *Context) reset() { | ||
x.pkeys = x.pkeys[:0] | ||
x.pvalues = x.pvalues[:0] | ||
x.routePath = "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.