clean up cache logic in addTs / fix a bug where the loop was incorrectly releasing the lock 2x
This commit is contained in:
parent
831f8a499d
commit
2fa95a09be
@ -763,28 +763,26 @@ func (mp *MessagePool) Add(ctx context.Context, m *types.SignedMessage) error {
|
|||||||
<-mp.addSema
|
<-mp.addSema
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//Ensure block calculation is cached without holding the write lock
|
|
||||||
mp.curTsLk.RLock()
|
mp.curTsLk.RLock()
|
||||||
tmpCurTs := mp.curTs
|
tmpCurTs := mp.curTs
|
||||||
mp.curTsLk.RUnlock()
|
mp.curTsLk.RUnlock()
|
||||||
|
|
||||||
|
//ensures computations are cached without holding lack
|
||||||
_, _ = mp.api.GetActorAfter(m.Message.From, tmpCurTs)
|
_, _ = mp.api.GetActorAfter(m.Message.From, tmpCurTs)
|
||||||
_, _ = mp.getStateNonce(ctx, m.Message.From, tmpCurTs)
|
_, _ = mp.getStateNonce(ctx, m.Message.From, tmpCurTs)
|
||||||
|
|
||||||
cacheSecondTime := true
|
mp.curTsLk.Lock()
|
||||||
//if the newly acquired Ts is not the one we just cached, let go of the lock, cache it and open the lock again and repeat....
|
if tmpCurTs == mp.curTs {
|
||||||
for cacheSecondTime {
|
//with the lock enabled, mp.curTs is the same Ts as we just had, so we know that our computations are cached
|
||||||
mp.curTsLk.Lock()
|
} else {
|
||||||
writeCurTs := mp.curTs
|
//curTs has been updated so we want to cache the new one:
|
||||||
|
tmpCurTs = mp.curTs
|
||||||
if writeCurTs == tmpCurTs {
|
//we want to release the lock, cache the computations then grab it again
|
||||||
break // we have this cached we can skip
|
|
||||||
}
|
|
||||||
mp.curTsLk.Unlock()
|
mp.curTsLk.Unlock()
|
||||||
tmpCurTs = writeCurTs
|
|
||||||
_, _ = mp.api.GetActorAfter(m.Message.From, tmpCurTs)
|
_, _ = mp.api.GetActorAfter(m.Message.From, tmpCurTs)
|
||||||
_, _ = mp.getStateNonce(ctx, m.Message.From, tmpCurTs)
|
_, _ = mp.getStateNonce(ctx, m.Message.From, tmpCurTs)
|
||||||
cacheSecondTime = false
|
mp.curTsLk.Lock()
|
||||||
|
//now that we have the lock, we continue, we could do this as a loop forever, but that's bad to loop forever, and this was added as an optimization and it seems once is enough because the computation < block time
|
||||||
}
|
}
|
||||||
|
|
||||||
defer mp.curTsLk.Unlock()
|
defer mp.curTsLk.Unlock()
|
||||||
|
Loading…
Reference in New Issue
Block a user