Enhance blockForwardCache of lcimport
Optimize block schedule mechanism
Result in similar performance with --no_stored option
@@ -288,6 +288,7 @@ func (bp *blockForwardCache) runTasks() {
bp.lock.Lock()
defer bp.lock.Unlock()
bp.active -= 1
+ bp.e.log.Tracef("BFC.exitTask(active=%d)", bp.active)
}()
}()
@@ -297,6 +298,7 @@ func (bp *blockForwardCache) runTasks() {
if blk, err := bp.e.GetBlockByHeight(task.height); err != nil {
task.chn <- err
} else if blk != nil {
+ bp.e.log.Tracef("BFC.done(height=%d)", task.height)
bp.scheduleBlocksFor(task.height)
// preload transactions and receipts to the memory
txs := blk.Transactions()
@@ -322,22 +324,24 @@ func (bp *blockForwardCache) scheduleBlockInLock(height int64) bool {
return false
}
if bt, ok := bp.blocks[height]; !ok {
+ bp.e.log.Tracef("BFC.schedule(height=%d)", height)
bt = bp.allocBlockTask(height)
bp.blocks[height] = bt
bp.tasks <- bt
- }
return true
}
+ return false
+}
func (bp *blockForwardCache) scheduleBlocksFor(height int64) {
bp.lock.Lock()
defer bp.lock.Unlock()
- for h := height + 1; h < height+1+maxBlockWorkers; h += 1 {
+ for h := height + 1; h < height+1+maxBlockCache; h += 1 {
if bp.scheduleBlockInLock(h) {
bp.tryNewWorkerInLock()
} else {
- return
+ continue
}
}
}
@@ -345,6 +349,7 @@ func (bp *blockForwardCache) scheduleBlocksFor(height int64) {
func (bp *blockForwardCache) tryNewWorkerInLock() {
if bp.active < maxBlockWorkers {
bp.active += 1
+ bp.e.log.Tracef("BFC.newTask(active=%d)", bp.active)
go bp.runTasks()
}
}
@@ -370,6 +375,7 @@ func (bp *blockForwardCache) fetchBlockTask(height int64) *blockTask {
return bt
} else {
bt = bp.allocBlockTask(height)
+ bp.e.log.Tracef("BFC.schedule2(height=%d)", height)
bp.tasks <- bt
bp.tryNewWorkerInLock()
return bt
@@ -382,6 +388,7 @@ func (bp *blockForwardCache) GetBlock(height int64) (*Block, error){
bp.deallocBlockTask(bt)
switch obj := res.(type) {
case *Block:
+ bp.scheduleBlocksFor(height)
return obj, nil
case error:
return nil, obj
