Skip to content

Commit

Permalink
[patch] refactor
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <[email protected]>
  • Loading branch information
kpango committed Feb 8, 2022
1 parent 8de6d01 commit 89c226d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
32 changes: 12 additions & 20 deletions fastime.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package fastime

import (
"bytes"
"context"
"math"
"sync"
"sync/atomic"
"time"
"unsafe"
Expand Down Expand Up @@ -73,17 +71,9 @@ func newFastime() *fastime {
}(),
correctionDur: time.Millisecond * 100,
}
f.pool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, len(f.GetFormat())))
},
}
f.ft = func() *atomic.Value {
av := new(atomic.Value)
buf := f.pool.Get().(*bytes.Buffer)
buf.Reset()
av.Store(buf.Bytes())
f.pool.Put(buf)
av.Store(f.newBuffer(len(f.GetFormat()) + 10))
return av
}()

Expand All @@ -98,6 +88,16 @@ func (f *fastime) refresh() *fastime {
return f.store(f.now())
}

func (f *fastime) newBuffer(max int) (b []byte) {
if max < bufSize {
var buf [bufSize]byte
b = buf[:0]
} else {
b = make([]byte, 0, max)
}
return b
}

func (f *fastime) store(t time.Time) *fastime {
f.t.Store(t)
ut := t.Unix()
Expand All @@ -107,15 +107,7 @@ 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()
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(b, form))
f.ft.Store(t.AppendFormat(f.newBuffer(len(form)+10), form))
return f
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/kpango/fastime

go 1.17
go 1.18

0 comments on commit 89c226d

Please sign in to comment.