dcrd/blockchain/prune.go
Dave Collins 3f366fbc17
chain: Remove memory block node pruning.
This removes the memory block node (header) pruning.  It should be noted
that this does not apply to stake node pruning.

This is being done for two primary reasons:

- The goal is to ultimately have all block nodes in memory in the same
  way the upstream code has done since it provides significant
  optimization and code simplification opportunities
- Upcoming code that deals with calculating sequence locks on inputs
  far in the past requires the ability to quickly calculate the median
  time for arbitrarily old nodes and consequently pruning the memory
  block nodes could lead to significant performance implications under
  those conditions
2017-09-18 13:43:48 -05:00

40 lines
1.1 KiB
Go

package blockchain
import (
"time"
)
// pruningIntervalInMinutes is the interval in which to prune the blockchain's
// nodes and restore memory to the garbage collector.
const pruningIntervalInMinutes = 5
// chainPruner is used to occasionally prune the blockchain of old nodes that
// can be freed to the garbage collector.
type chainPruner struct {
chain *BlockChain
lastNodeInsertTime time.Time
}
// newChainPruner returns a new chain pruner.
func newChainPruner(chain *BlockChain) *chainPruner {
return &chainPruner{
chain: chain,
lastNodeInsertTime: time.Now(),
}
}
// pruneChainIfNeeded checks the current time versus the time of the last pruning.
// If the blockchain hasn't been pruned in this time, it initiates a new pruning.
//
// pruneChainIfNeeded must be called with the chainLock held for writes.
func (c *chainPruner) pruneChainIfNeeded() {
now := time.Now()
duration := now.Sub(c.lastNodeInsertTime)
if duration < time.Minute*pruningIntervalInMinutes {
return
}
c.lastNodeInsertTime = now
c.chain.pruneStakeNodes()
}