sm
/
cache
1
0
Fork 0

Back to UnixNano(), syscall dependency isn't worth a few nanoseconds better performance

This commit is contained in:
Patrick Mylund Nielsen 2015-11-30 15:12:19 -05:00
parent f6cdd07cbb
commit afadf13f9f
1 changed files with 5 additions and 16 deletions

View File

@ -7,7 +7,6 @@ import (
"os" "os"
"runtime" "runtime"
"sync" "sync"
"syscall"
"time" "time"
) )
@ -21,9 +20,7 @@ func (item Item) Expired() bool {
if item.Expiration == 0 { if item.Expiration == 0 {
return false return false
} }
var tv syscall.Timeval return time.Now().UnixNano() > item.Expiration
syscall.Gettimeofday(&tv)
return tv.Nano() > item.Expiration
} }
const ( const (
@ -112,9 +109,7 @@ func (c *cache) Get(k string) (interface{}, bool) {
return nil, false return nil, false
} }
if item.Expiration > 0 { if item.Expiration > 0 {
var tv syscall.Timeval if time.Now().UnixNano() > item.Expiration {
syscall.Gettimeofday(&tv)
if tv.Nano() > item.Expiration {
c.mu.RUnlock() c.mu.RUnlock()
return nil, false return nil, false
} }
@ -130,9 +125,7 @@ func (c *cache) get(k string) (interface{}, bool) {
} }
// "Inlining" of Expired // "Inlining" of Expired
if item.Expiration > 0 { if item.Expiration > 0 {
var tv syscall.Timeval if time.Now().UnixNano() > item.Expiration {
syscall.Gettimeofday(&tv)
if tv.Nano() > item.Expiration {
c.mu.RUnlock() c.mu.RUnlock()
return nil, false return nil, false
} }
@ -890,12 +883,8 @@ type keyAndValue struct {
// Delete all expired items from the cache. // Delete all expired items from the cache.
func (c *cache) DeleteExpired() { func (c *cache) DeleteExpired() {
var ( var evictedItems []keyAndValue
evictedItems []keyAndValue now := time.Now().UnixNano()
tv syscall.Timeval
)
syscall.Gettimeofday(&tv)
now := tv.Nano()
c.mu.Lock() c.mu.Lock()
for k, v := range c.items { for k, v := range c.items {
// "Inlining" of expired // "Inlining" of expired