1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

reduce VMess memory usage

This commit is contained in:
Darien Raymond 2017-04-06 23:11:36 +02:00
parent 954587cdc9
commit 1b244bf3a1
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -29,23 +29,25 @@ type TimedUserValidator struct {
sync.RWMutex
ctx context.Context
validUsers []*protocol.User
userHash map[[16]byte]*indexTimePair
userHash map[[16]byte]indexTimePair
ids []*idEntry
hasher protocol.IDHash
baseTime protocol.Timestamp
}
type indexTimePair struct {
index int
timeSec protocol.Timestamp
timeInc uint32
}
func NewTimedUserValidator(ctx context.Context, hasher protocol.IDHash) protocol.UserValidator {
tus := &TimedUserValidator{
ctx: ctx,
validUsers: make([]*protocol.User, 0, 16),
userHash: make(map[[16]byte]*indexTimePair, 512),
userHash: make(map[[16]byte]indexTimePair, 512),
ids: make([]*idEntry, 0, 512),
hasher: hasher,
baseTime: protocol.Timestamp(time.Now().Unix() - cacheDurationSec*3),
}
go tus.updateUserHash(updateIntervalSec * time.Second)
return tus
@ -65,7 +67,10 @@ func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx in
idHash.Reset()
delete(v.userHash, hashValueRemoval)
v.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
v.userHash[hashValue] = indexTimePair{
index: idx,
timeInc: uint32(entry.lastSec - v.baseTime),
}
entry.lastSec++
entry.lastSecRemoval++
@ -132,7 +137,7 @@ func (v *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Time
copy(fixedSizeHash[:], userHash)
pair, found := v.userHash[fixedSizeHash]
if found {
return v.validUsers[pair.index], pair.timeSec, true
return v.validUsers[pair.index], protocol.Timestamp(pair.timeInc) + v.baseTime, true
}
return nil, 0, false
}