-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved ctx.UserValue from standard map to custom userData. This should…
… improve its' performance for common case when ctx contains up to 10 user values
- Loading branch information
Showing
4 changed files
with
171 additions
and
16 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
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,59 @@ | ||
package fasthttp | ||
|
||
type userDataKV struct { | ||
key []byte | ||
value interface{} | ||
} | ||
|
||
type userData []userDataKV | ||
|
||
func (d *userData) Set(key string, value interface{}) { | ||
args := *d | ||
n := len(args) | ||
for i := 0; i < n; i++ { | ||
kv := &args[i] | ||
if string(kv.key) == key { | ||
kv.value = value | ||
return | ||
} | ||
} | ||
|
||
c := cap(args) | ||
if c > n { | ||
args = args[:n+1] | ||
kv := &args[n] | ||
kv.key = append(kv.key[:0], key...) | ||
kv.value = value | ||
*d = args | ||
return | ||
} | ||
|
||
kv := userDataKV{} | ||
kv.key = append(kv.key[:0], key...) | ||
kv.value = value | ||
*d = append(args, kv) | ||
} | ||
|
||
func (d *userData) SetBytes(key []byte, value interface{}) { | ||
d.Set(unsafeBytesToStr(key), value) | ||
} | ||
|
||
func (d *userData) Get(key string) interface{} { | ||
args := *d | ||
n := len(args) | ||
for i := 0; i < n; i++ { | ||
kv := &args[i] | ||
if string(kv.key) == key { | ||
return kv.value | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (d *userData) GetBytes(key []byte) interface{} { | ||
return d.Get(unsafeBytesToStr(key)) | ||
} | ||
|
||
func (d *userData) Reset() { | ||
*d = (*d)[:0] | ||
} |
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,41 @@ | ||
package fasthttp | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestUserData(t *testing.T) { | ||
var u userData | ||
|
||
for i := 0; i < 10; i++ { | ||
key := []byte(fmt.Sprintf("key_%d", i)) | ||
u.SetBytes(key, i+5) | ||
testUserDataGet(t, &u, key, i+5) | ||
u.SetBytes(key, i) | ||
testUserDataGet(t, &u, key, i) | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
key := []byte(fmt.Sprintf("key_%d", i)) | ||
testUserDataGet(t, &u, key, i) | ||
} | ||
|
||
u.Reset() | ||
|
||
for i := 0; i < 10; i++ { | ||
key := []byte(fmt.Sprintf("key_%d", i)) | ||
testUserDataGet(t, &u, key, nil) | ||
} | ||
} | ||
|
||
func testUserDataGet(t *testing.T, u *userData, key []byte, value interface{}) { | ||
v := u.GetBytes(key) | ||
if v == nil && value != nil { | ||
t.Fatalf("cannot obtain value for key=%q", key) | ||
} | ||
if !reflect.DeepEqual(v, value) { | ||
t.Fatalf("unexpected value for key=%q: %d. Expecting %d", v, value) | ||
} | ||
} |
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,48 @@ | ||
package fasthttp | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func BenchmarkUserDataCustom(b *testing.B) { | ||
keys := []string{"foobar", "baz", "aaa", "bsdfs"} | ||
b.RunParallel(func(pb *testing.PB) { | ||
var u userData | ||
var v interface{} = u | ||
for pb.Next() { | ||
for _, key := range keys { | ||
u.Set(key, v) | ||
} | ||
for _, key := range keys { | ||
vv := u.Get(key) | ||
if _, ok := vv.(userData); !ok { | ||
b.Fatalf("unexpected value %v for key %q", vv, key) | ||
} | ||
} | ||
u.Reset() | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkUserDataStdMap(b *testing.B) { | ||
keys := []string{"foobar", "baz", "aaa", "bsdfs"} | ||
b.RunParallel(func(pb *testing.PB) { | ||
u := make(map[string]interface{}) | ||
var v interface{} = u | ||
for pb.Next() { | ||
for _, key := range keys { | ||
u[key] = v | ||
} | ||
for _, key := range keys { | ||
vv := u[key] | ||
if _, ok := vv.(map[string]interface{}); !ok { | ||
b.Fatalf("unexpected value %v for key %q", vv, key) | ||
} | ||
} | ||
|
||
for k := range u { | ||
delete(u, k) | ||
} | ||
} | ||
}) | ||
} |