1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-04 17:27:23 -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 sync.RWMutex
ctx context.Context ctx context.Context
validUsers []*protocol.User validUsers []*protocol.User
userHash map[[16]byte]*indexTimePair userHash map[[16]byte]indexTimePair
ids []*idEntry ids []*idEntry
hasher protocol.IDHash hasher protocol.IDHash
baseTime protocol.Timestamp
} }
type indexTimePair struct { type indexTimePair struct {
index int index int
timeSec protocol.Timestamp timeInc uint32
} }
func NewTimedUserValidator(ctx context.Context, hasher protocol.IDHash) protocol.UserValidator { func NewTimedUserValidator(ctx context.Context, hasher protocol.IDHash) protocol.UserValidator {
tus := &TimedUserValidator{ tus := &TimedUserValidator{
ctx: ctx, ctx: ctx,
validUsers: make([]*protocol.User, 0, 16), 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), ids: make([]*idEntry, 0, 512),
hasher: hasher, hasher: hasher,
baseTime: protocol.Timestamp(time.Now().Unix() - cacheDurationSec*3),
} }
go tus.updateUserHash(updateIntervalSec * time.Second) go tus.updateUserHash(updateIntervalSec * time.Second)
return tus return tus
@ -65,7 +67,10 @@ func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx in
idHash.Reset() idHash.Reset()
delete(v.userHash, hashValueRemoval) 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.lastSec++
entry.lastSecRemoval++ entry.lastSecRemoval++
@ -132,7 +137,7 @@ func (v *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Time
copy(fixedSizeHash[:], userHash) copy(fixedSizeHash[:], userHash)
pair, found := v.userHash[fixedSizeHash] pair, found := v.userHash[fixedSizeHash]
if found { 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 return nil, 0, false
} }