les: minor cleanup

This commit is contained in:
Zsolt Felfoldi 2020-03-05 01:00:22 +01:00
parent d0c20a92bd
commit 4423a1d13e
2 changed files with 12 additions and 9 deletions

View file

@ -226,9 +226,11 @@ func newClientPool(db ethdb.Database, minCap, freeClientCap uint64, clock mclock
for { for {
select { select {
case <-clock.After(persistExpirationRefresh): case <-clock.After(persistExpirationRefresh):
pool.lock.Lock()
now := pool.clock.Now() now := pool.clock.Now()
posExp := pool.posExpiration(now) posExp := pool.posExpiration(now)
negExp := pool.negExpiration(now) negExp := pool.negExpiration(now)
pool.lock.Unlock()
pool.ndb.setExpiration(posExp, negExp) pool.ndb.setExpiration(posExp, negExp)
case <-pool.stopCh: case <-pool.stopCh:
return return

View file

@ -43,13 +43,13 @@ type expiredValue struct {
// value calculates the value at the given moment. // value calculates the value at the given moment.
func (e expiredValue) value(logOffset fixed64) uint64 { func (e expiredValue) value(logOffset fixed64) uint64 {
offset := uint64ToFixed64(e.exp) - logOffset offset := uint64ToFixed64(e.exp) - logOffset
return uint64(float64(e.base) * offset.pow2Fixed()) return uint64(float64(e.base) * offset.pow2())
} }
// add adds a signed value at the given moment // add adds a signed value at the given moment
func (e *expiredValue) add(amount int64, logOffset fixed64) int64 { func (e *expiredValue) add(amount int64, logOffset fixed64) int64 {
integer, frac := logOffset.toUint64(), logOffset.fraction() integer, frac := logOffset.toUint64(), logOffset.fraction()
factor := frac.pow2Fixed() factor := frac.pow2()
base := factor * float64(amount) base := factor * float64(amount)
if integer < e.exp { if integer < e.exp {
base /= math.Pow(2, float64(e.exp-integer)) base /= math.Pow(2, float64(e.exp-integer))
@ -95,11 +95,10 @@ func (e *expiredValue) subExp(a expiredValue) {
} }
} }
// fixedFactor is the factor used by fixed64 // fixedFactor is the fixed point multiplier factor used by fixed64.
const fixedFactor = 0x1000000 const fixedFactor = 0x1000000
// fixed64 is a float64 wrapper that uses integer arithmetic // fixed64 implements 64-bit fixed point arithmetic functions.
// to avoid precision loss in floating-point arithmetic.
type fixed64 int64 type fixed64 int64
// uint64ToFixed64 converts uint64 integer to fixed64 format. // uint64ToFixed64 converts uint64 integer to fixed64 format.
@ -117,12 +116,14 @@ func (f64 fixed64) toUint64() uint64 {
return uint64(f64) / fixedFactor return uint64(f64) / fixedFactor
} }
// fraction returns the fraction of the fixed64. // fraction returns the fractional part of a fixed64 value.
func (f64 fixed64) fraction() fixed64 { func (f64 fixed64) fraction() fixed64 {
return f64 % fixedFactor return f64 % fixedFactor
} }
// pow2Fixed returns the 2 based pow of the fixed value. var fixedLogFactor = math.Log(2) / float64(fixedFactor)
func (f64 fixed64) pow2Fixed() float64 {
return math.Pow(2, float64(f64)/fixedFactor) // pow2Fixed returns the base 2 power of the fixed point value.
func (f64 fixed64) pow2() float64 {
return math.Exp(float64(f64) * fixedLogFactor)
} }