Skip to content

Commit

Permalink
Removed 1 alloc in channel validation (#251)
Browse files Browse the repository at this point in the history
More work towards reducing the number of heap allocations in the hot path.
  • Loading branch information
kelindar authored Jun 5, 2019
1 parent c33d480 commit 7ff71cb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ deploy:
- provider: GitHub
auth_token:
secure: ImwOgsH/e1F+reDfqNIvoQ773FZHsjQt/4znrFdxUVrs1VNpFK9IUaW4hIL/yl4c
release: 'master'
release: 'edge'
description: 'This is v$(appveyor_build_version) pre-release which is automatically built on every commit to master.'
draft: false
prerelease: true
Expand Down
4 changes: 2 additions & 2 deletions internal/broker/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

var benchInit sync.Once

// BenchmarkSerial-8 100 10084435 ns/op 2183 B/op 19 allocs/op
// BenchmarkSerial-8 100 10007938 ns/op 2172 B/op 17 allocs/op
func BenchmarkSerial(b *testing.B) {
const port = 9995
benchInit.Do(func() {
Expand Down Expand Up @@ -64,7 +64,7 @@ func BenchmarkSerial(b *testing.B) {
}
}

// BenchmarkParallel-8 200000 6801 ns/op 1488 B/op 16 allocs/op
// BenchmarkParallel-8 200000 8068 ns/op 1331 B/op 13 allocs/op
func BenchmarkParallel(b *testing.B) {
const port = 9995
benchInit.Do(func() {
Expand Down
8 changes: 4 additions & 4 deletions internal/security/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ import (
)

func BenchmarkParseChannelWithOptions(b *testing.B) {
in := "xm54Sj0srWlSEctra-yU6ZA6Z2e6pp7c/a/roman/is/da/best/?opt1=true&opt2=false"
in := []byte("xm54Sj0srWlSEctra-yU6ZA6Z2e6pp7c/a/roman/is/da/best/?opt1=true&opt2=false")

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = ParseChannel([]byte(in))
_ = ParseChannel(in)
}
}

func BenchmarkParseChannelStatic(b *testing.B) {
in := "xm54Sj0srWlSEctra-yU6ZA6Z2e6pp7c/a/roman/is/da/best/"
in := []byte("xm54Sj0srWlSEctra-yU6ZA6Z2e6pp7c/a/roman/is/da/best/")

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = ParseChannel([]byte(in))
_ = ParseChannel(in)
}
}

Expand Down
13 changes: 11 additions & 2 deletions internal/security/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (k Key) SetPermissions(value uint8) {

// ValidateChannel validates the channel string.
func (k Key) ValidateChannel(ch *Channel) bool {
topic := ch.Channel
if len(topic) == 0 {
return false
}

// Bytes 16-17-18-19 contains target hash
target := uint32(k[16])<<24 | uint32(k[17])<<16 | uint32(k[18])<<8 | uint32(k[19])
Expand All @@ -130,8 +134,13 @@ func (k Key) ValidateChannel(ch *Channel) bool {
return target == ch.Target()
}

channel := string(ch.Channel)
channel = strings.TrimRight(channel, "/")
// Trim right `/`
if topic[len(topic)-1] == '/' {
topic = topic[:len(topic)-1]
}

// Split by `/`
channel := binaryToString(&topic)
parts := strings.Split(channel, "/")
wc := parts[len(parts)-1] == "#"
if wc {
Expand Down
13 changes: 13 additions & 0 deletions internal/security/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ func TestKeyIsEmpty(t *testing.T) {
assert.True(t, true, key.IsEmpty())
}

// BenchmarkValidateChannel-8 10000000 126 ns/op 48 B/op 1 allocs/op
func BenchmarkValidateChannel(b *testing.B) {
key := Key(make([]byte, 24))
key.SetTarget("a/")
v := ParseChannel([]byte(string(key) + "a/b/c/d/"))

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
key.ValidateChannel(v)
}
}

func validateChannel(k Key, c string) bool {
return k.ValidateChannel(&Channel{Channel: []byte(c)})
}
Expand Down

0 comments on commit 7ff71cb

Please sign in to comment.