From c9026df68727cfc31178ca6232a52eb0d8d24b0a Mon Sep 17 00:00:00 2001 From: Yixin Luo <18810541851@163.com> Date: Mon, 1 Aug 2022 21:47:57 +0800 Subject: [PATCH] refactor: Statistics check for nil in its methods (#529) --- nodedb.go | 16 ++++------------ options.go | 12 ++++++++++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/nodedb.go b/nodedb.go index 45548d3a0..c64b89ac1 100644 --- a/nodedb.go +++ b/nodedb.go @@ -116,15 +116,11 @@ func (ndb *nodeDB) GetNode(hash []byte) (*Node, error) { // Check the cache. if cachedNode := ndb.nodeCache.Get(hash); cachedNode != nil { - if ndb.opts.Stat != nil { - ndb.opts.Stat.IncCacheHitCnt() - } + ndb.opts.Stat.IncCacheHitCnt() return cachedNode.(*Node), nil } - if ndb.opts.Stat != nil { - ndb.opts.Stat.IncCacheMissCnt() - } + ndb.opts.Stat.IncCacheMissCnt() // Doesn't exist, load. buf, err := ndb.db.Get(ndb.nodeKey(hash)) @@ -160,15 +156,11 @@ func (ndb *nodeDB) GetFastNode(key []byte) (*FastNode, error) { } if cachedFastNode := ndb.fastNodeCache.Get(key); cachedFastNode != nil { - if ndb.opts.Stat != nil { - ndb.opts.Stat.IncFastCacheHitCnt() - } + ndb.opts.Stat.IncFastCacheHitCnt() return cachedFastNode.(*FastNode), nil } - if ndb.opts.Stat != nil { - ndb.opts.Stat.IncFastCacheMissCnt() - } + ndb.opts.Stat.IncFastCacheMissCnt() // Doesn't exist, load. buf, err := ndb.db.Get(ndb.fastNodeKey(key)) diff --git a/options.go b/options.go index 49a68cf11..7d5d24653 100644 --- a/options.go +++ b/options.go @@ -18,18 +18,30 @@ type Statistics struct { } func (stat *Statistics) IncCacheHitCnt() { + if stat == nil { + return + } atomic.AddUint64(&stat.cacheHitCnt, 1) } func (stat *Statistics) IncCacheMissCnt() { + if stat == nil { + return + } atomic.AddUint64(&stat.cacheMissCnt, 1) } func (stat *Statistics) IncFastCacheHitCnt() { + if stat == nil { + return + } atomic.AddUint64(&stat.fastCacheHitCnt, 1) } func (stat *Statistics) IncFastCacheMissCnt() { + if stat == nil { + return + } atomic.AddUint64(&stat.fastCacheMissCnt, 1) }