Skip to content

Commit

Permalink
Merge 6401b99 into bbc3ec1
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jan 14, 2020
2 parents bbc3ec1 + 6401b99 commit 30477da
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 13 deletions.
19 changes: 19 additions & 0 deletions bytesconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package httprouter

import (
"reflect"
"unsafe"
)

// StringToBytes converts string to byte slice without a memory allocation.
func StringToBytes(s string) (b []byte) {
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len
return b
}

// BytesToString converts byte slice to string without a memory allocation.
func BytesToString(s []byte) string {
return *(*string)(unsafe.Pointer(&s))
}
96 changes: 96 additions & 0 deletions bytesconv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package httprouter

import (
"bytes"
"math/rand"
"testing"
"time"
)

var testString = "Albert Einstein: Logic will get you from A to B. Imagination will take you everywhere."
var testBytes = []byte(testString)

func rawBytesToStr(b []byte) string {
return string(b)
}

func rawStrToBytes(s string) []byte {
return []byte(s)
}

// go test -v

func TestBytesToString(t *testing.T) {
data := make([]byte, 1024)
for i := 0; i < 100; i++ {
rand.Read(data)
if rawBytesToStr(data) != BytesToString(data) {
t.Fatal("don't match")
}
}
}

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)

var src = rand.NewSource(time.Now().UnixNano())

func RandStringBytesMaskImprSrc(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}

func TestStringToBytes(t *testing.T) {
for i := 0; i < 100; i++ {
s := RandStringBytesMaskImprSrc(64)
if !bytes.Equal(rawStrToBytes(s), StringToBytes(s)) {
t.Fatal("don't match")
}
}
}

// go test -v -run=none -bench=^BenchmarkBytesConv -benchmem=true

func BenchmarkBytesConvBytesToStrRaw(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
rawBytesToStr(testBytes)
}
}

func BenchmarkBytesConvBytesToStr(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
BytesToString(testBytes)
}
}

func BenchmarkBytesConvStrToBytesRaw(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
rawStrToBytes(testString)
}
}

func BenchmarkBytesConvStrToBytes(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
StringToBytes(testString)
}
}
26 changes: 13 additions & 13 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ func longestCommonPrefix(a, b string) int {
// Returns -1 as index, if no wildcard was found.
func findWildcard(path string) (wilcard string, i int, valid bool) {
// Find start
for start, c := range []byte(path) {
for start, c := range StringToBytes(path) {
// A wildcard starts with ':' (param) or '*' (catch-all)
if c != ':' && c != '*' {
continue
}

// Find end and check for invalid characters
valid = true
for end, c := range []byte(path[start+1:]) {
for end, c := range StringToBytes(path[start+1:]) {
switch c {
case '/':
return path[start : start+1+end], start, valid
Expand All @@ -53,7 +53,7 @@ func findWildcard(path string) (wilcard string, i int, valid bool) {

func countParams(path string) uint16 {
var n uint
for i := range []byte(path) {
for i := range StringToBytes(path) {
switch path[i] {
case ':', '*':
n++
Expand Down Expand Up @@ -139,7 +139,7 @@ walk:

n.children = []*node{&child}
// []byte for proper unicode char conversion, see #65
n.indices = string([]byte{n.path[i]})
n.indices = BytesToString([]byte{n.path[i]})
n.path = path[:i]
n.handle = nil
n.wildChild = false
Expand Down Expand Up @@ -185,7 +185,7 @@ walk:
}

// Check if a child with the next path byte exists
for i, c := range []byte(n.indices) {
for i, c := range StringToBytes(n.indices) {
if c == idxc {
i = n.incrementChildPrio(i)
n = n.children[i]
Expand All @@ -196,7 +196,7 @@ walk:
// Otherwise insert it
if idxc != ':' && idxc != '*' {
// []byte for proper unicode char conversion, see #65
n.indices += string([]byte{idxc})
n.indices += BytesToString([]byte{idxc})
child := &node{}
n.children = append(n.children, child)
n.incrementChildPrio(len(n.indices) - 1)
Expand Down Expand Up @@ -336,7 +336,7 @@ walk: // Outer loop for walking the tree
// to walk down the tree
if !n.wildChild {
idxc := path[0]
for i, c := range []byte(n.indices) {
for i, c := range StringToBytes(n.indices) {
if c == idxc {
n = n.children[i]
continue walk
Expand Down Expand Up @@ -438,7 +438,7 @@ walk: // Outer loop for walking the tree

// No handle found. Check if a handle for this path + a
// trailing slash exists for trailing slash recommendation
for i, c := range []byte(n.indices) {
for i, c := range StringToBytes(n.indices) {
if c == '/' {
n = n.children[i]
tsr = (len(n.path) == 1 && n.handle != nil) ||
Expand Down Expand Up @@ -479,7 +479,7 @@ func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (fixe
fixTrailingSlash,
)

return string(ciPath), ciPath != nil
return BytesToString(ciPath), ciPath != nil
}

// Shift bytes in array by n bytes left
Expand Down Expand Up @@ -520,7 +520,7 @@ walk: // Outer loop for walking the tree
if rb[0] != 0 {
// Old rune not finished
idxc := rb[0]
for i, c := range []byte(n.indices) {
for i, c := range StringToBytes(n.indices) {
if c == idxc {
// continue with child node
n = n.children[i]
Expand Down Expand Up @@ -552,7 +552,7 @@ walk: // Outer loop for walking the tree
rb = shiftNRuneBytes(rb, off)

idxc := rb[0]
for i, c := range []byte(n.indices) {
for i, c := range StringToBytes(n.indices) {
// Lowercase matches
if c == idxc {
// must use a recursive approach since both the
Expand All @@ -574,7 +574,7 @@ walk: // Outer loop for walking the tree
rb = shiftNRuneBytes(rb, off)

idxc := rb[0]
for i, c := range []byte(n.indices) {
for i, c := range StringToBytes(n.indices) {
// Uppercase matches
if c == idxc {
// Continue with child node
Expand Down Expand Up @@ -651,7 +651,7 @@ walk: // Outer loop for walking the tree
// No handle found.
// Try to fix the path by adding a trailing slash
if fixTrailingSlash {
for i, c := range []byte(n.indices) {
for i, c := range StringToBytes(n.indices) {
if c == '/' {
n = n.children[i]
if (len(n.path) == 1 && n.handle != nil) ||
Expand Down

0 comments on commit 30477da

Please sign in to comment.