Skip to content

Commit afadf13

Browse files
committed
Back to UnixNano(), syscall dependency isn't worth a few nanoseconds better performance
1 parent f6cdd07 commit afadf13

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

cache.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"runtime"
99
"sync"
10-
"syscall"
1110
"time"
1211
)
1312

@@ -21,9 +20,7 @@ func (item Item) Expired() bool {
2120
if item.Expiration == 0 {
2221
return false
2322
}
24-
var tv syscall.Timeval
25-
syscall.Gettimeofday(&tv)
26-
return tv.Nano() > item.Expiration
23+
return time.Now().UnixNano() > item.Expiration
2724
}
2825

2926
const (
@@ -112,9 +109,7 @@ func (c *cache) Get(k string) (interface{}, bool) {
112109
return nil, false
113110
}
114111
if item.Expiration > 0 {
115-
var tv syscall.Timeval
116-
syscall.Gettimeofday(&tv)
117-
if tv.Nano() > item.Expiration {
112+
if time.Now().UnixNano() > item.Expiration {
118113
c.mu.RUnlock()
119114
return nil, false
120115
}
@@ -130,9 +125,7 @@ func (c *cache) get(k string) (interface{}, bool) {
130125
}
131126
// "Inlining" of Expired
132127
if item.Expiration > 0 {
133-
var tv syscall.Timeval
134-
syscall.Gettimeofday(&tv)
135-
if tv.Nano() > item.Expiration {
128+
if time.Now().UnixNano() > item.Expiration {
136129
c.mu.RUnlock()
137130
return nil, false
138131
}
@@ -890,12 +883,8 @@ type keyAndValue struct {
890883

891884
// Delete all expired items from the cache.
892885
func (c *cache) DeleteExpired() {
893-
var (
894-
evictedItems []keyAndValue
895-
tv syscall.Timeval
896-
)
897-
syscall.Gettimeofday(&tv)
898-
now := tv.Nano()
886+
var evictedItems []keyAndValue
887+
now := time.Now().UnixNano()
899888
c.mu.Lock()
900889
for k, v := range c.items {
901890
// "Inlining" of expired

0 commit comments

Comments
 (0)