1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 09:56:18 -05:00

use [16]byte instead of string for faster hashing

This commit is contained in:
v2ray 2016-01-12 13:02:05 +01:00
parent bed5235772
commit 48ff2a5ae8

View File

@ -44,7 +44,7 @@ type UserSet interface {
type TimedUserSet struct {
validUsers []vmess.User
userHash map[string]indexTimePair
userHash map[[16]byte]indexTimePair
ids []*idEntry
access sync.RWMutex
}
@ -57,7 +57,7 @@ type indexTimePair struct {
func NewTimedUserSet() UserSet {
tus := &TimedUserSet{
validUsers: make([]vmess.User, 0, 16),
userHash: make(map[string]indexTimePair, 512),
userHash: make(map[[16]byte]indexTimePair, 512),
access: sync.RWMutex{},
ids: make([]*idEntry, 0, 512),
}
@ -66,21 +66,21 @@ func NewTimedUserSet() UserSet {
}
func (us *TimedUserSet) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
for entry.lastSec <= nowSec {
var hashValue [16]byte
idHash := IDHash(entry.id.Bytes())
for entry.lastSec <= nowSec {
idHash.Write(entry.lastSec.Bytes())
idHashSlice := idHash.Sum(nil)
hashValue := string(idHashSlice)
us.access.Lock()
us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
us.access.Unlock()
idHash.Sum(hashValue[:0])
idHash.Reset()
hash2Remove := entry.hashes.Put(hashValue)
if hash2Remove != nil {
us.access.Lock()
delete(us.userHash, hash2Remove.(string))
us.access.Unlock()
us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
if hash2Remove != nil {
delete(us.userHash, hash2Remove.([16]byte))
}
us.access.Unlock()
entry.lastSec++
}
}
@ -125,7 +125,9 @@ func (us *TimedUserSet) AddUser(user vmess.User) error {
func (us *TimedUserSet) GetUser(userHash []byte) (vmess.User, Timestamp, bool) {
defer us.access.RUnlock()
us.access.RLock()
pair, found := us.userHash[string(userHash)]
var fixedSizeHash [16]byte
copy(fixedSizeHash[:], userHash)
pair, found := us.userHash[fixedSizeHash]
if found {
return us.validUsers[pair.index], pair.timeSec, true
}