Skip to content

Commit

Permalink
[patch] refactor
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <kpango@vdaas.org>
  • Loading branch information
kpango committed Feb 8, 2022
1 parent 5f82ec0 commit 8de6d01
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions fastime.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ type fastime struct {
format *atomic.Value
location *atomic.Value
cancel context.CancelFunc
pool sync.Pool
}

const bufSize = 64

// New returns Fastime
func New() Fastime {
return newFastime()
Expand Down Expand Up @@ -106,13 +107,15 @@ func (f *fastime) store(t time.Time) *fastime {
atomic.StoreUint32(&f.uut, *(*uint32)(unsafe.Pointer(&ut)))
atomic.StoreUint32(&f.uunt, *(*uint32)(unsafe.Pointer(&unt)))
form := f.GetFormat()
buf := f.pool.Get().(*bytes.Buffer)
if buf == nil || len(form) > buf.Cap() {
buf.Grow(len(form))
var b []byte
max := len(form) + 10
if max < bufSize {
var buf [bufSize]byte
b = buf[:0]
} else {
b = make([]byte, 0, max)
}
f.ft.Store(t.AppendFormat(buf.Bytes(), form))
buf.Reset()
f.pool.Put(buf)
f.ft.Store(t.AppendFormat(b, form))
return f
}

Expand Down

0 comments on commit 8de6d01

Please sign in to comment.