1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-03 07:56:42 -05:00

futher reduce memory usage of uuid

This commit is contained in:
v2ray 2016-01-11 00:22:59 +01:00
parent 774095568f
commit adf5820286

View File

@ -14,16 +14,14 @@ var (
InvalidID = errors.New("Invalid ID.") InvalidID = errors.New("Invalid ID.")
) )
type UUID struct { type UUID [16]byte
byteValue []byte
}
func (this *UUID) String() string { func (this *UUID) String() string {
return bytesToString(this.byteValue) return bytesToString(this.Bytes())
} }
func (this *UUID) Bytes() []byte { func (this *UUID) Bytes() []byte {
return this.byteValue return this[:]
} }
func (this *UUID) Equals(another *UUID) bool { func (this *UUID) Equals(another *UUID) bool {
@ -63,19 +61,18 @@ func bytesToString(bytes []byte) string {
} }
func New() *UUID { func New() *UUID {
bytes := make([]byte, 16) uuid := new(UUID)
rand.Read(bytes) rand.Read(uuid.Bytes())
uuid, _ := ParseBytes(bytes)
return uuid return uuid
} }
func ParseBytes(bytes []byte) (*UUID, error) { func ParseBytes(b []byte) (*UUID, error) {
if len(bytes) != 16 { if len(b) != 16 {
return nil, InvalidID return nil, InvalidID
} }
return &UUID{ uuid := new(UUID)
byteValue: bytes, copy(uuid[:], b)
}, nil return uuid, nil
} }
func ParseString(str string) (*UUID, error) { func ParseString(str string) (*UUID, error) {
@ -84,10 +81,8 @@ func ParseString(str string) (*UUID, error) {
return nil, InvalidID return nil, InvalidID
} }
uuid := &UUID{ uuid := new(UUID)
byteValue: make([]byte, 16), b := uuid.Bytes()
}
b := uuid.byteValue[:]
for _, byteGroup := range byteGroups { for _, byteGroup := range byteGroups {
if text[0] == '-' { if text[0] == '-' {