mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 23:06:30 -05:00
use [16]byte instead of string for faster hashing
This commit is contained in:
parent
bed5235772
commit
48ff2a5ae8
@ -44,7 +44,7 @@ type UserSet interface {
|
|||||||
|
|
||||||
type TimedUserSet struct {
|
type TimedUserSet struct {
|
||||||
validUsers []vmess.User
|
validUsers []vmess.User
|
||||||
userHash map[string]indexTimePair
|
userHash map[[16]byte]indexTimePair
|
||||||
ids []*idEntry
|
ids []*idEntry
|
||||||
access sync.RWMutex
|
access sync.RWMutex
|
||||||
}
|
}
|
||||||
@ -57,7 +57,7 @@ type indexTimePair struct {
|
|||||||
func NewTimedUserSet() UserSet {
|
func NewTimedUserSet() UserSet {
|
||||||
tus := &TimedUserSet{
|
tus := &TimedUserSet{
|
||||||
validUsers: make([]vmess.User, 0, 16),
|
validUsers: make([]vmess.User, 0, 16),
|
||||||
userHash: make(map[string]indexTimePair, 512),
|
userHash: make(map[[16]byte]indexTimePair, 512),
|
||||||
access: sync.RWMutex{},
|
access: sync.RWMutex{},
|
||||||
ids: make([]*idEntry, 0, 512),
|
ids: make([]*idEntry, 0, 512),
|
||||||
}
|
}
|
||||||
@ -66,21 +66,21 @@ func NewTimedUserSet() UserSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (us *TimedUserSet) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
|
func (us *TimedUserSet) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
|
||||||
|
var hashValue [16]byte
|
||||||
|
idHash := IDHash(entry.id.Bytes())
|
||||||
for entry.lastSec <= nowSec {
|
for entry.lastSec <= nowSec {
|
||||||
idHash := IDHash(entry.id.Bytes())
|
|
||||||
idHash.Write(entry.lastSec.Bytes())
|
idHash.Write(entry.lastSec.Bytes())
|
||||||
idHashSlice := idHash.Sum(nil)
|
idHash.Sum(hashValue[:0])
|
||||||
hashValue := string(idHashSlice)
|
idHash.Reset()
|
||||||
us.access.Lock()
|
|
||||||
us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
|
|
||||||
us.access.Unlock()
|
|
||||||
|
|
||||||
hash2Remove := entry.hashes.Put(hashValue)
|
hash2Remove := entry.hashes.Put(hashValue)
|
||||||
|
|
||||||
|
us.access.Lock()
|
||||||
|
us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
|
||||||
if hash2Remove != nil {
|
if hash2Remove != nil {
|
||||||
us.access.Lock()
|
delete(us.userHash, hash2Remove.([16]byte))
|
||||||
delete(us.userHash, hash2Remove.(string))
|
|
||||||
us.access.Unlock()
|
|
||||||
}
|
}
|
||||||
|
us.access.Unlock()
|
||||||
entry.lastSec++
|
entry.lastSec++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,7 +125,9 @@ func (us *TimedUserSet) AddUser(user vmess.User) error {
|
|||||||
func (us *TimedUserSet) GetUser(userHash []byte) (vmess.User, Timestamp, bool) {
|
func (us *TimedUserSet) GetUser(userHash []byte) (vmess.User, Timestamp, bool) {
|
||||||
defer us.access.RUnlock()
|
defer us.access.RUnlock()
|
||||||
us.access.RLock()
|
us.access.RLock()
|
||||||
pair, found := us.userHash[string(userHash)]
|
var fixedSizeHash [16]byte
|
||||||
|
copy(fixedSizeHash[:], userHash)
|
||||||
|
pair, found := us.userHash[fixedSizeHash]
|
||||||
if found {
|
if found {
|
||||||
return us.validUsers[pair.index], pair.timeSec, true
|
return us.validUsers[pair.index], pair.timeSec, true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user