Skip to content

Commit

Permalink
futher reduce memory usage of uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 10, 2016
1 parent 7740955 commit adf5820
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions common/uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ var (
InvalidID = errors.New("Invalid ID.")
)

type UUID struct {
byteValue []byte
}
type UUID [16]byte

func (this *UUID) String() string {
return bytesToString(this.byteValue)
return bytesToString(this.Bytes())
}

func (this *UUID) Bytes() []byte {
return this.byteValue
return this[:]
}

func (this *UUID) Equals(another *UUID) bool {
Expand Down Expand Up @@ -63,19 +61,18 @@ func bytesToString(bytes []byte) string {
}

func New() *UUID {
bytes := make([]byte, 16)
rand.Read(bytes)
uuid, _ := ParseBytes(bytes)
uuid := new(UUID)
rand.Read(uuid.Bytes())
return uuid
}

func ParseBytes(bytes []byte) (*UUID, error) {
if len(bytes) != 16 {
func ParseBytes(b []byte) (*UUID, error) {
if len(b) != 16 {
return nil, InvalidID
}
return &UUID{
byteValue: bytes,
}, nil
uuid := new(UUID)
copy(uuid[:], b)
return uuid, nil
}

func ParseString(str string) (*UUID, error) {
Expand All @@ -84,10 +81,8 @@ func ParseString(str string) (*UUID, error) {
return nil, InvalidID
}

uuid := &UUID{
byteValue: make([]byte, 16),
}
b := uuid.byteValue[:]
uuid := new(UUID)
b := uuid.Bytes()

for _, byteGroup := range byteGroups {
if text[0] == '-' {
Expand Down

0 comments on commit adf5820

Please sign in to comment.