Skip to content

Commit 9c9b595

Browse files
committed
[patch] use named return for more effective inline
Signed-off-by: kpango <kpango@vdaas.org>
1 parent 829f6a3 commit 9c9b595

File tree

4 files changed

+87
-69
lines changed

4 files changed

+87
-69
lines changed

.circleci/config.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
alias:
22
default: &default
3-
working_directory: /go/src/github.com/kpango/fastime
43
docker:
54
- image: cimg/go:1.20.2
65
environment:
7-
GOPATH: "/go"
86
GO111MODULE: "on"
97
REPO_NAME: "kpango"
108
IMAGE_NAME: "fastime"
119
GITHUB_API: "https://api.github.com/"
1210
DOCKER_USER: "kpango"
1311
setup_remote_docker: &setup_remote_docker
14-
version: 18.06.0-ce
12+
version: 20.10.18
1513
docker_layer_caching: true
1614

1715
version: 2

Makefile

+32-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ GO_VERSION:=$(shell go version)
22

33
.PHONY: all clean bench bench-all profile lint test contributors update install
44

5+
GOPATH := $(eval GOPATH := $(shell go env GOPATH))$(GOPATH)
6+
GOLINES_MAX_WIDTH ?= 200
7+
58
all: clean install lint test bench
69

710
clean:
@@ -27,34 +30,49 @@ profile: clean
2730
go test -count=10 -run=NONE -bench=BenchmarkFastime -benchmem -o pprof/fastime-test.bin -cpuprofile pprof/cpu-fastime.out -memprofile pprof/mem-fastime.out
2831
go tool pprof --svg pprof/fastime-test.bin pprof/cpu-fastime.out > cpu-fastime.svg
2932
go tool pprof --svg pprof/fastime-test.bin pprof/mem-fastime.out > mem-fastime.svg
30-
go-torch -f bench/cpu-fastime-graph.svg pprof/fastime-test.bin pprof/cpu-fastime.out
31-
go-torch --alloc_objects -f bench/mem-fastime-graph.svg pprof/fastime-test.bin pprof/mem-fastime.out
3233
\
3334
go test -count=10 -run=NONE -bench=BenchmarkTime -benchmem -o pprof/default-test.bin -cpuprofile pprof/cpu-default.out -memprofile pprof/mem-default.out
3435
go tool pprof --svg pprof/default-test.bin pprof/mem-default.out > mem-default.svg
3536
go tool pprof --svg pprof/default-test.bin pprof/cpu-default.out > cpu-default.svg
36-
go-torch -f bench/cpu-default-graph.svg pprof/default-test.bin pprof/cpu-default.out
37-
go-torch --alloc_objects -f bench/mem-default-graph.svg pprof/default-test.bin pprof/mem-default.out
3837
\
3938
mv ./*.svg bench/
4039

41-
cpu:
42-
go tool pprof pprof/fastime-test.bin pprof/cpu-fastime.out
40+
profile-web-cpu:
41+
go tool pprof -http=":6061" \
42+
pprof/fastime-test.bin \
43+
pprof/cpu-fastime.out
44+
45+
profile-web-mem:
46+
go tool pprof -http=":6062" \
47+
pprof/fastime-test.bin \
48+
pprof/mem-fastime.out
49+
50+
profile-web-cpu-default:
51+
go tool pprof -http=":6063" \
52+
pprof/default-test.bin \
53+
pprof/cpu-default.out
54+
55+
profile-web-mem-default:
56+
go tool pprof -http=":6064" \
57+
pprof/default-test.bin \
58+
pprof/mem-default.out
59+
4360

44-
mem:
45-
go tool pprof --alloc_space pprof/fastime-test.bin pprof/mem-fastime.out
4661

4762
lint:
4863
gometalinter --enable-all . | rg -v comment
4964

50-
format:
51-
find ./ -type d -name .git -prune -o -type f -regex '.*[^\.pb]\.go' -print | xargs golines -w -m 200
52-
find ./ -type d -name .git -prune -o -type f -regex '.*[^\.pb]\.go' -print | xargs gofumpt -w
53-
find ./ -type d -name .git -prune -o -type f -regex '.*\.go' -print | xargs strictgoimports -w
54-
find ./ -type d -name .git -prune -o -type f -regex '.*\.go' -print | xargs goimports -w
55-
5665
test: clean
5766
GO111MODULE=on go test --race -v $(go list ./... | rg -v vendor)
5867

5968
contributors:
6069
git log --format='%aN <%aE>' | sort -fu > CONTRIBUTORS
70+
71+
run:
72+
go run example/main.go
73+
74+
format:
75+
find ./ -type d -name .git -prune -o -type f -regex '.*[^\.pb]\.go' -print | xargs $(GOPATH)/bin/golines -w -m $(GOLINES_MAX_WIDTH)
76+
find ./ -type d -name .git -prune -o -type f -regex '.*[^\.pb]\.go' -print | xargs $(GOPATH)/bin/gofumpt -w
77+
find ./ -type d -name .git -prune -o -type f -regex '.*[^\.pb]\.go' -print | xargs $(GOPATH)/bin/strictgoimports -w
78+
find ./ -type d -name .git -prune -o -type f -regex '.*\.go' -print | xargs $(GOPATH)/bin/goimports -w

fastime.go

+41-39
Original file line numberDiff line numberDiff line change
@@ -45,50 +45,52 @@ type fastime struct {
4545
location atomic.Pointer[time.Location]
4646
}
4747

48-
const bufSize = 64
48+
const (
49+
bufSize = 64
50+
bufMargin = 10
51+
)
4952

5053
// New returns Fastime
51-
func New() Fastime {
54+
func New() (f Fastime) {
5255
return newFastime()
5356
}
5457

55-
func newFastime() *fastime {
56-
f := &fastime{
57-
ut: math.MaxInt64,
58-
unt: math.MaxInt64,
59-
uut: math.MaxUint32,
60-
uunt: math.MaxUint32,
58+
func newFastime() (f *fastime) {
59+
f = &fastime{
60+
ut: math.MaxInt64,
61+
unt: math.MaxInt64,
62+
uut: math.MaxUint32,
63+
uunt: math.MaxUint32,
6164
correctionDur: time.Millisecond * 100,
6265
}
6366

6467
form := time.RFC3339
6568
f.format.Store(&form)
6669
loc := func() (loc *time.Location) {
67-
tz, ok := syscall.Getenv("TZ")
68-
if ok && tz != "" {
69-
var err error
70-
loc, err = time.LoadLocation(tz)
71-
if err == nil {
72-
return loc
73-
}
70+
tz, ok := syscall.Getenv("TZ")
71+
if ok && tz != "" {
72+
var err error
73+
loc, err = time.LoadLocation(tz)
74+
if err == nil {
75+
return loc
7476
}
75-
return new(time.Location)
76-
}()
77+
}
78+
return new(time.Location)
79+
}()
7780

7881
f.location.Store(loc)
7982

80-
81-
buf := f.newBuffer(len(f.GetFormat()) + 10)
83+
buf := f.newBuffer(len(form) + bufMargin)
8284
f.ft.Store(&buf)
8385

8486
return f.refresh()
8587
}
8688

87-
func (f *fastime) update() *fastime {
89+
func (f *fastime) update() (ft *fastime) {
8890
return f.store(f.Now().Add(time.Duration(atomic.LoadInt64(&f.dur))))
8991
}
9092

91-
func (f *fastime) refresh() *fastime {
93+
func (f *fastime) refresh() (ft *fastime) {
9294
return f.store(f.now())
9395
}
9496

@@ -102,7 +104,7 @@ func (f *fastime) newBuffer(max int) (b []byte) {
102104
return b
103105
}
104106

105-
func (f *fastime) store(t time.Time) *fastime {
107+
func (f *fastime) store(t time.Time) (ft *fastime) {
106108
f.t.Store(&t)
107109
f.formatValid.Store(false)
108110
ut := t.Unix()
@@ -114,42 +116,42 @@ func (f *fastime) store(t time.Time) *fastime {
114116
return f
115117
}
116118

117-
func (f *fastime) IsDaemonRunning() bool {
119+
func (f *fastime) IsDaemonRunning() (running bool) {
118120
return f.running.Load()
119121
}
120122

121-
func (f *fastime) GetLocation() *time.Location {
122-
loc := f.location.Load()
123+
func (f *fastime) GetLocation() (loc *time.Location) {
124+
loc = f.location.Load()
123125
if loc == nil {
124126
return nil
125127
}
126128
return loc
127129
}
128130

129-
func (f *fastime) GetFormat() string {
131+
func (f *fastime) GetFormat() (form string) {
130132
return *f.format.Load()
131133
}
132134

133135
// SetLocation replaces time location
134-
func (f *fastime) SetLocation(location *time.Location) Fastime {
135-
if location == nil {
136+
func (f *fastime) SetLocation(loc *time.Location) (ft Fastime) {
137+
if loc == nil {
136138
return f
137139
}
138-
f.location.Store(location)
140+
f.location.Store(loc)
139141
f.refresh()
140142
return f
141143
}
142144

143145
// SetFormat replaces time format
144-
func (f *fastime) SetFormat(format string) Fastime {
146+
func (f *fastime) SetFormat(format string) (ft Fastime) {
145147
f.format.Store(&format)
146148
f.formatValid.Store(false)
147149
f.refresh()
148150
return f
149151
}
150152

151153
// Now returns current time
152-
func (f *fastime) Now() time.Time {
154+
func (f *fastime) Now() (t time.Time) {
153155
return *f.t.Load()
154156
}
155157

@@ -167,43 +169,43 @@ func (f *fastime) stop() {
167169
f.wg.Wait()
168170
}
169171

170-
func (f *fastime) Since(t time.Time) time.Duration {
172+
func (f *fastime) Since(t time.Time) (dur time.Duration) {
171173
return f.Now().Sub(t)
172174
}
173175

174176
// UnixNow returns current unix time
175-
func (f *fastime) UnixNow() int64 {
177+
func (f *fastime) UnixNow() (now int64) {
176178
return atomic.LoadInt64(&f.ut)
177179
}
178180

179181
// UnixNow returns current unix time
180-
func (f *fastime) UnixUNow() uint32 {
182+
func (f *fastime) UnixUNow() (now uint32) {
181183
return atomic.LoadUint32(&f.uut)
182184
}
183185

184186
// UnixNanoNow returns current unix nano time
185-
func (f *fastime) UnixNanoNow() int64 {
187+
func (f *fastime) UnixNanoNow() (now int64) {
186188
return atomic.LoadInt64(&f.unt)
187189
}
188190

189191
// UnixNanoNow returns current unix nano time
190-
func (f *fastime) UnixUNanoNow() uint32 {
192+
func (f *fastime) UnixUNanoNow() (now uint32) {
191193
return atomic.LoadUint32(&f.uunt)
192194
}
193195

194196
// FormattedNow returns formatted byte time
195-
func (f *fastime) FormattedNow() []byte {
197+
func (f *fastime) FormattedNow() (now []byte) {
196198
// only update formatted value on swap
197199
if f.formatValid.CompareAndSwap(false, true) {
198200
form := f.GetFormat()
199-
buf := f.Now().AppendFormat(f.newBuffer(len(form)+10), form)
201+
buf := f.Now().AppendFormat(f.newBuffer(len(form)+bufMargin), form)
200202
f.ft.Store(&buf)
201203
}
202204
return *f.ft.Load()
203205
}
204206

205207
// StartTimerD provides time refresh daemon
206-
func (f *fastime) StartTimerD(ctx context.Context, dur time.Duration) Fastime {
208+
func (f *fastime) StartTimerD(ctx context.Context, dur time.Duration) (ft Fastime) {
207209
f.mu.Lock()
208210
defer f.mu.Unlock()
209211
// if the daemon was already running, restart

global.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,36 @@ func init() {
1717
})
1818
}
1919

20-
func IsDaemonRunning() bool {
20+
func IsDaemonRunning() (running bool) {
2121
return instance.IsDaemonRunning()
2222
}
2323

24-
func GetLocation() *time.Location {
24+
func GetLocation() (loc *time.Location) {
2525
return instance.GetLocation()
2626
}
2727

28-
func GetFormat() string {
28+
func GetFormat() (form string) {
2929
return instance.GetFormat()
3030
}
3131

3232
// SetLocation replaces time location
33-
func SetLocation(location *time.Location) Fastime {
33+
func SetLocation(location *time.Location) (ft Fastime) {
3434
return instance.SetLocation(location)
3535
}
3636

3737
// SetFormat replaces time format
38-
func SetFormat(format string) Fastime {
38+
func SetFormat(format string) (ft Fastime) {
3939
return instance.SetFormat(format)
4040
}
4141

4242
// Now returns current time
43-
func Now() time.Time {
43+
func Now() (now time.Time) {
4444
return instance.Now()
4545
}
4646

4747
// Since returns the time elapsed since t.
4848
// It is shorthand for fastime.Now().Sub(t).
49-
func Since(t time.Time) time.Duration {
49+
func Since(t time.Time) (dur time.Duration) {
5050
return instance.Since(t)
5151
}
5252

@@ -56,31 +56,31 @@ func Stop() {
5656
}
5757

5858
// UnixNow returns current unix time
59-
func UnixNow() int64 {
59+
func UnixNow() (now int64) {
6060
return instance.UnixNow()
6161
}
6262

6363
// UnixUNow returns current unix time
64-
func UnixUNow() uint32 {
64+
func UnixUNow() (now uint32) {
6565
return instance.UnixUNow()
6666
}
6767

6868
// UnixNanoNow returns current unix nano time
69-
func UnixNanoNow() int64 {
69+
func UnixNanoNow() (now int64) {
7070
return instance.UnixNanoNow()
7171
}
7272

7373
// UnixUNanoNow returns current unix nano time
74-
func UnixUNanoNow() uint32 {
74+
func UnixUNanoNow() (now uint32) {
7575
return instance.UnixUNanoNow()
7676
}
7777

7878
// FormattedNow returns formatted byte time
79-
func FormattedNow() []byte {
79+
func FormattedNow() (now []byte) {
8080
return instance.FormattedNow()
8181
}
8282

8383
// StartTimerD provides time refresh daemon
84-
func StartTimerD(ctx context.Context, dur time.Duration) Fastime {
84+
func StartTimerD(ctx context.Context, dur time.Duration) (ft Fastime) {
8585
return instance.StartTimerD(ctx, dur)
8686
}

0 commit comments

Comments
 (0)