mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-03-18 00:55:37 +08:00
采用更快的Hash算法
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/dchest/siphash"
|
||||
"github.com/cespare/xxhash"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -170,7 +170,7 @@ func (this *MemoryStorage) AddToList(item *Item) {
|
||||
|
||||
// 计算Key Hash
|
||||
func (this *MemoryStorage) hash(key string) uint64 {
|
||||
return siphash.Hash(0, 0, []byte(key))
|
||||
return xxhash.Sum64String(key)
|
||||
}
|
||||
|
||||
// 清理任务
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package caches
|
||||
|
||||
import (
|
||||
"github.com/dchest/siphash"
|
||||
"github.com/cespare/xxhash"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -80,5 +80,5 @@ func (this *MemoryWriter) ExpiredAt() int64 {
|
||||
|
||||
// 计算Key Hash
|
||||
func (this *MemoryWriter) hash(key string) uint64 {
|
||||
return siphash.Hash(0, 0, []byte(key))
|
||||
return xxhash.Sum64String(key)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package grids
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"github.com/dchest/siphash"
|
||||
"github.com/cespare/xxhash"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
@@ -18,7 +18,7 @@ const (
|
||||
)
|
||||
|
||||
func HashKey(key []byte) uint64 {
|
||||
return siphash.Hash(0, 0, key)
|
||||
return xxhash.Sum64(key)
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
|
||||
@@ -2,7 +2,7 @@ package grids
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"github.com/dchest/siphash"
|
||||
"github.com/cespare/xxhash"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
@@ -22,7 +22,7 @@ func BenchmarkItem_Size(b *testing.B) {
|
||||
Key: []byte("123"),
|
||||
ValueBytes: []byte("Hello, World"),
|
||||
}
|
||||
for i := 0; i < b.N; i ++ {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = item.Size()
|
||||
}
|
||||
}
|
||||
@@ -31,16 +31,16 @@ func TestItem_HashKey(t *testing.T) {
|
||||
t.Log(HashKey([]byte("2")))
|
||||
}
|
||||
|
||||
func TestItem_siphash(t *testing.T) {
|
||||
result := siphash.Hash(0, 0, []byte("123456"))
|
||||
func TestItem_xxHash(t *testing.T) {
|
||||
result := xxhash.Sum64([]byte("123456"))
|
||||
t.Log(result)
|
||||
}
|
||||
|
||||
func TestItem_unique(t *testing.T) {
|
||||
m := map[uint64]bool{}
|
||||
for i := 0; i < 1000*10000; i ++ {
|
||||
for i := 0; i < 1000*10000; i++ {
|
||||
s := "Hello,World,LONG KEY,LONG KEY,LONG KEY,LONG KEY" + strconv.Itoa(i)
|
||||
result := siphash.Hash(0, 0, []byte(s))
|
||||
result := xxhash.Sum64([]byte(s))
|
||||
_, ok := m[result]
|
||||
if ok {
|
||||
t.Log("found same", i)
|
||||
@@ -50,20 +50,20 @@ func TestItem_unique(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
t.Log(siphash.Hash(0, 0, []byte("01")))
|
||||
t.Log(siphash.Hash(0, 0, []byte("10")))
|
||||
t.Log(xxhash.Sum64([]byte("01")))
|
||||
t.Log(xxhash.Sum64([]byte("10")))
|
||||
}
|
||||
|
||||
func BenchmarkItem_HashKeyMd5(b *testing.B) {
|
||||
for i := 0; i < b.N; i ++ {
|
||||
for i := 0; i < b.N; i++ {
|
||||
h := md5.New()
|
||||
h.Write([]byte("HELLO_KEY_" + strconv.Itoa(i)))
|
||||
_ = h.Sum(nil)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkItem_siphash(b *testing.B) {
|
||||
for i := 0; i < b.N; i ++ {
|
||||
_ = siphash.Hash(0, 0, []byte("HELLO_KEY_"+strconv.Itoa(i)))
|
||||
func BenchmarkItem_xxHash(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = xxhash.Sum64([]byte("HELLO_KEY_" + strconv.Itoa(i)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package nodes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dchest/siphash"
|
||||
"github.com/cespare/xxhash"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"io"
|
||||
@@ -189,7 +189,7 @@ func (this *HTTPRequest) doRoot() (isBreak bool) {
|
||||
}
|
||||
|
||||
// 支持 ETag
|
||||
eTag := "\"et" + fmt.Sprintf("%0x", siphash.Hash(0, 0, []byte(filename+strconv.FormatInt(stat.ModTime().UnixNano(), 10)+strconv.FormatInt(stat.Size(), 10)))) + "\""
|
||||
eTag := "\"et" + fmt.Sprintf("%0x", xxhash.Sum64String(filename+strconv.FormatInt(stat.ModTime().UnixNano(), 10)+strconv.FormatInt(stat.Size(), 10))) + "\""
|
||||
if len(respHeader.Get("ETag")) == 0 {
|
||||
respHeader.Set("ETag", eTag)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package ttlcache
|
||||
|
||||
import "github.com/dchest/siphash"
|
||||
import "github.com/cespare/xxhash"
|
||||
|
||||
func HashKey(key []byte) uint64 {
|
||||
return siphash.Hash(0, 0, key)
|
||||
return xxhash.Sum64(key)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package checkpoints
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
// just a sample checkpoint, copy and change it for your new checkpoint
|
||||
@@ -9,11 +10,11 @@ type SampleRequestCheckpoint struct {
|
||||
Checkpoint
|
||||
}
|
||||
|
||||
func (this *SampleRequestCheckpoint) RequestValue(req *requests.Request, param string, options map[string]string) (value interface{}, sysErr error, userErr error) {
|
||||
func (this *SampleRequestCheckpoint) RequestValue(req *requests.Request, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (this *SampleRequestCheckpoint) ResponseValue(req *requests.Request, resp *requests.Response, param string, options map[string]string) (value interface{}, sysErr error, userErr error) {
|
||||
func (this *SampleRequestCheckpoint) ResponseValue(req *requests.Request, resp *requests.Response, param string, options maps.Map) (value interface{}, sysErr error, userErr error) {
|
||||
if this.IsRequest() {
|
||||
return this.RequestValue(req, param, options)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package waf
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
|
||||
"github.com/dchest/siphash"
|
||||
"github.com/cespare/xxhash"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"net/http"
|
||||
"regexp"
|
||||
@@ -174,7 +174,9 @@ func BenchmarkRuleSet_MatchRequest_Regexp3(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkHash(b *testing.B) {
|
||||
runtime.GOMAXPROCS(1)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = siphash.Hash(0, 0, bytes.Repeat([]byte("HELLO"), 10240))
|
||||
_ = xxhash.Sum64(bytes.Repeat([]byte("HELLO"), 10240))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,7 +318,7 @@ func testTemplate20001(a *assert.Assertion, t *testing.T, template *WAF) {
|
||||
// enable bot rule set
|
||||
for _, g := range template.Inbound {
|
||||
if g.Code == "bot" {
|
||||
g.On = true
|
||||
g.IsOn = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@ package utils
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/dchest/siphash"
|
||||
"github.com/cespare/xxhash"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -21,7 +20,7 @@ func MatchStringCache(regex *regexp.Regexp, s string) bool {
|
||||
return regex.MatchString(s)
|
||||
}
|
||||
|
||||
hash := siphash.Hash(0, 0, utils.UnsafeStringToBytes(s))
|
||||
hash := xxhash.Sum64String(s)
|
||||
key := fmt.Sprintf("%p_", regex) + strconv.FormatUint(hash, 10)
|
||||
item := cache.Read(key)
|
||||
if item != nil {
|
||||
@@ -43,7 +42,7 @@ func MatchBytesCache(regex *regexp.Regexp, byteSlice []byte) bool {
|
||||
return regex.Match(byteSlice)
|
||||
}
|
||||
|
||||
hash := siphash.Hash(0, 0, byteSlice)
|
||||
hash := xxhash.Sum64(byteSlice)
|
||||
key := fmt.Sprintf("%p_", regex) + strconv.FormatUint(hash, 10)
|
||||
item := cache.Read(key)
|
||||
if item != nil {
|
||||
|
||||
Reference in New Issue
Block a user