@@ -13,8 +13,11 @@ import (
1313 "strings"
1414 "testing"
1515 "time"
16+ _ "unsafe"
1617
18+ "github.com/gobwas/ws"
1719 "github.com/google/go-cmp/cmp"
20+ _ "github.com/gorilla/websocket"
1821
1922 "nhooyr.io/websocket/internal/assert"
2023)
@@ -325,14 +328,17 @@ func Test_mask(t *testing.T) {
325328 }
326329}
327330
328- func basixMask (maskKey [4 ]byte , pos int , b []byte ) int {
331+ func basicMask (maskKey [4 ]byte , pos int , b []byte ) int {
329332 for i := range b {
330333 b [i ] ^= maskKey [pos & 3 ]
331334 pos ++
332335 }
333336 return pos & 3
334337}
335338
339+ //go:linkname gorillaMaskBytes github.com/gorilla/websocket.maskBytes
340+ func gorillaMaskBytes (key [4 ]byte , pos int , b []byte ) int
341+
336342func Benchmark_mask (b * testing.B ) {
337343 sizes := []int {
338344 2 ,
@@ -355,12 +361,13 @@ func Benchmark_mask(b *testing.B) {
355361 name : "basic" ,
356362 fn : func (b * testing.B , key [4 ]byte , p []byte ) {
357363 for i := 0 ; i < b .N ; i ++ {
358- basixMask (key , 0 , p )
364+ basicMask (key , 0 , p )
359365 }
360366 },
361367 },
368+
362369 {
363- name : "fast " ,
370+ name : "nhooyr " ,
364371 fn : func (b * testing.B , key [4 ]byte , p []byte ) {
365372 key32 := binary .LittleEndian .Uint32 (key [:])
366373 b .ResetTimer ()
@@ -370,6 +377,22 @@ func Benchmark_mask(b *testing.B) {
370377 }
371378 },
372379 },
380+ {
381+ name : "gorilla" ,
382+ fn : func (b * testing.B , key [4 ]byte , p []byte ) {
383+ for i := 0 ; i < b .N ; i ++ {
384+ gorillaMaskBytes (key , 0 , p )
385+ }
386+ },
387+ },
388+ {
389+ name : "gobwas" ,
390+ fn : func (b * testing.B , key [4 ]byte , p []byte ) {
391+ for i := 0 ; i < b .N ; i ++ {
392+ ws .Cipher (p , key , 0 )
393+ }
394+ },
395+ },
373396 }
374397
375398 var key [4 ]byte
0 commit comments