@@ -13,8 +13,11 @@ import (
13
13
"strings"
14
14
"testing"
15
15
"time"
16
+ _ "unsafe"
16
17
18
+ "github.com/gobwas/ws"
17
19
"github.com/google/go-cmp/cmp"
20
+ _ "github.com/gorilla/websocket"
18
21
19
22
"nhooyr.io/websocket/internal/assert"
20
23
)
@@ -325,14 +328,17 @@ func Test_mask(t *testing.T) {
325
328
}
326
329
}
327
330
328
- func basixMask (maskKey [4 ]byte , pos int , b []byte ) int {
331
+ func basicMask (maskKey [4 ]byte , pos int , b []byte ) int {
329
332
for i := range b {
330
333
b [i ] ^= maskKey [pos & 3 ]
331
334
pos ++
332
335
}
333
336
return pos & 3
334
337
}
335
338
339
+ //go:linkname gorillaMaskBytes github.com/gorilla/websocket.maskBytes
340
+ func gorillaMaskBytes (key [4 ]byte , pos int , b []byte ) int
341
+
336
342
func Benchmark_mask (b * testing.B ) {
337
343
sizes := []int {
338
344
2 ,
@@ -355,12 +361,13 @@ func Benchmark_mask(b *testing.B) {
355
361
name : "basic" ,
356
362
fn : func (b * testing.B , key [4 ]byte , p []byte ) {
357
363
for i := 0 ; i < b .N ; i ++ {
358
- basixMask (key , 0 , p )
364
+ basicMask (key , 0 , p )
359
365
}
360
366
},
361
367
},
368
+
362
369
{
363
- name : "fast " ,
370
+ name : "nhooyr " ,
364
371
fn : func (b * testing.B , key [4 ]byte , p []byte ) {
365
372
key32 := binary .LittleEndian .Uint32 (key [:])
366
373
b .ResetTimer ()
@@ -370,6 +377,22 @@ func Benchmark_mask(b *testing.B) {
370
377
}
371
378
},
372
379
},
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
+ },
373
396
}
374
397
375
398
var key [4 ]byte
0 commit comments