-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reward: Prealod StakingInfo during state regen
- Loading branch information
Showing
6 changed files
with
152 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package common | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// RefCntMap is a map with reference counting. | ||
type RefCountingMap struct { | ||
m sync.Map | ||
} | ||
|
||
type refCountedElem struct { | ||
value interface{} | ||
count int | ||
} | ||
|
||
func NewRefCountingMap() *RefCountingMap { | ||
return &RefCountingMap{} | ||
} | ||
|
||
// Inc adds an element and increments its reference count. | ||
// If the key already exists, it only increments the reference count. | ||
func (r *RefCountingMap) Inc(key interface{}, value interface{}) { | ||
// Could be loaded or stored. Increment the count in either case. | ||
mVal, _ := r.m.LoadOrStore(key, &refCountedElem{value: value, count: 0}) | ||
elem := mVal.(*refCountedElem) | ||
elem.count++ | ||
} | ||
|
||
// Dec decrements the reference count of the element with the given key. | ||
// If the reference count reaches 0, the element is removed from the map. | ||
func (r *RefCountingMap) Dec(key interface{}) { | ||
if mVal, ok := r.m.Load(key); ok { | ||
elem := mVal.(*refCountedElem) | ||
elem.count-- | ||
if elem.count == 0 { | ||
r.m.Delete(key) | ||
} | ||
} | ||
} | ||
|
||
// Get returns the value of the element with the given key. It does not incre | ||
func (r *RefCountingMap) Get(key interface{}) (interface{}, bool) { | ||
if mVal, ok := r.m.Load(key); ok { | ||
return mVal.(*refCountedElem).value, true | ||
} | ||
return nil, false | ||
} | ||
|
||
// sync.Map does not have a method to get the length of the map. | ||
// This function is a slow way to get the length; so use it only for testing. | ||
func (r *RefCountingMap) Len() int { | ||
size := 0 | ||
r.m.Range(func(key, value interface{}) bool { | ||
size++ | ||
return true | ||
}) | ||
return size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters