@@ -21,6 +21,7 @@ import (
2121 "errors"
2222 "fmt"
2323 "io"
24+ "reflect"
2425 "sync"
2526 "time"
2627
@@ -84,7 +85,8 @@ type Database struct {
8485 flushnodes uint64 // Nodes flushed since last commit
8586 flushsize common.StorageSize // Data storage flushed since last commit
8687
87- dirtiesSize common.StorageSize // Storage size of the dirty node cache (exc. flushlist)
88+ dirtiesSize common.StorageSize // Storage size of the dirty node cache (exc. metadata)
89+ childrenSize common.StorageSize // Storage size of the external children tracking
8890 preimagesSize common.StorageSize // Storage size of the preimages cache
8991
9092 lock sync.RWMutex
@@ -146,6 +148,15 @@ type cachedNode struct {
146148 flushNext common.Hash // Next node in the flush-list
147149}
148150
151+ // cachedNodeSize is the raw size of a cachedNode data structure without any
152+ // node data included. It's an approximate size, but should be a lot better
153+ // than not counting them.
154+ var cachedNodeSize = int (reflect .TypeOf (cachedNode {}).Size ())
155+
156+ // cachedNodeChildrenSize is the raw size of an initialized but empty external
157+ // reference map.
158+ const cachedNodeChildrenSize = 48
159+
149160// rlp returns the raw rlp encoded blob of the cached node, either directly from
150161// the cache, or by regenerating it from the collapsed node.
151162func (n * cachedNode ) rlp () []byte {
@@ -300,9 +311,11 @@ func NewDatabaseWithCache(diskdb ethdb.KeyValueStore, cache int) *Database {
300311 })
301312 }
302313 return & Database {
303- diskdb : diskdb ,
304- cleans : cleans ,
305- dirties : map [common.Hash ]* cachedNode {{}: {}},
314+ diskdb : diskdb ,
315+ cleans : cleans ,
316+ dirties : map [common.Hash ]* cachedNode {{}: {
317+ children : make (map [common.Hash ]uint16 ),
318+ }},
306319 preimages : make (map [common.Hash ][]byte ),
307320 }
308321}
@@ -491,11 +504,15 @@ func (db *Database) reference(child common.Hash, parent common.Hash) {
491504 // If the reference already exists, only duplicate for roots
492505 if db .dirties [parent ].children == nil {
493506 db .dirties [parent ].children = make (map [common.Hash ]uint16 )
507+ db .childrenSize += cachedNodeChildrenSize
494508 } else if _ , ok = db .dirties [parent ].children [child ]; ok && parent != (common.Hash {}) {
495509 return
496510 }
497511 node .parents ++
498512 db .dirties [parent ].children [child ]++
513+ if db .dirties [parent ].children [child ] == 1 {
514+ db .childrenSize += common .HashLength + 2 // uint16 counter
515+ }
499516}
500517
501518// Dereference removes an existing reference from a root node.
@@ -532,6 +549,7 @@ func (db *Database) dereference(child common.Hash, parent common.Hash) {
532549 node .children [child ]--
533550 if node .children [child ] == 0 {
534551 delete (node .children , child )
552+ db .childrenSize -= (common .HashLength + 2 ) // uint16 counter
535553 }
536554 }
537555 // If the child does not exist, it's a previously committed node.
@@ -566,6 +584,9 @@ func (db *Database) dereference(child common.Hash, parent common.Hash) {
566584 }
567585 delete (db .dirties , child )
568586 db .dirtiesSize -= common .StorageSize (common .HashLength + int (node .size ))
587+ if node .children != nil {
588+ db .childrenSize -= cachedNodeChildrenSize
589+ }
569590 }
570591}
571592
@@ -584,8 +605,9 @@ func (db *Database) Cap(limit common.StorageSize) error {
584605
585606 // db.dirtiesSize only contains the useful data in the cache, but when reporting
586607 // the total memory consumption, the maintenance metadata is also needed to be
587- // counted. For every useful node, we track 2 extra hashes as the flushlist.
588- size := db .dirtiesSize + common .StorageSize ((len (db .dirties )- 1 )* 2 * common .HashLength )
608+ // counted.
609+ size := db .dirtiesSize + common .StorageSize ((len (db .dirties )- 1 )* cachedNodeSize )
610+ size += db .childrenSize - common .StorageSize (len (db .dirties [common.Hash {}].children )* (common .HashLength + 2 ))
589611
590612 // If the preimage cache got large enough, push to disk. If it's still small
591613 // leave for later to deduplicate writes.
@@ -621,10 +643,12 @@ func (db *Database) Cap(limit common.StorageSize) error {
621643 batch .Reset ()
622644 }
623645 // Iterate to the next flush item, or abort if the size cap was achieved. Size
624- // is the total size, including both the useful cached data (hash -> blob), as
625- // well as the flushlist metadata (2*hash). When flushing items from the cache,
626- // we need to reduce both.
627- size -= common .StorageSize (3 * common .HashLength + int (node .size ))
646+ // is the total size, including the useful cached data (hash -> blob), the
647+ // cache item metadata, as well as external children mappings.
648+ size -= common .StorageSize (common .HashLength + int (node .size ) + cachedNodeSize )
649+ if node .children != nil {
650+ size -= common .StorageSize (cachedNodeChildrenSize + len (node .children )* (common .HashLength + 2 ))
651+ }
628652 oldest = node .flushNext
629653 }
630654 // Flush out any remainder data from the last batch
@@ -646,6 +670,9 @@ func (db *Database) Cap(limit common.StorageSize) error {
646670 db .oldest = node .flushNext
647671
648672 db .dirtiesSize -= common .StorageSize (common .HashLength + int (node .size ))
673+ if node .children != nil {
674+ db .childrenSize -= common .StorageSize (cachedNodeChildrenSize + len (node .children )* (common .HashLength + 2 ))
675+ }
649676 }
650677 if db .oldest != (common.Hash {}) {
651678 db .dirties [db .oldest ].flushPrev = common.Hash {}
@@ -803,7 +830,9 @@ func (c *cleaner) Put(key []byte, rlp []byte) error {
803830 // Remove the node from the dirty cache
804831 delete (c .db .dirties , hash )
805832 c .db .dirtiesSize -= common .StorageSize (common .HashLength + int (node .size ))
806-
833+ if node .children != nil {
834+ c .db .dirtiesSize -= common .StorageSize (cachedNodeChildrenSize + len (node .children )* (common .HashLength + 2 ))
835+ }
807836 // Move the flushed node into the clean cache to prevent insta-reloads
808837 if c .db .cleans != nil {
809838 c .db .cleans .Set (string (hash [:]), rlp )
@@ -823,9 +852,10 @@ func (db *Database) Size() (common.StorageSize, common.StorageSize) {
823852
824853 // db.dirtiesSize only contains the useful data in the cache, but when reporting
825854 // the total memory consumption, the maintenance metadata is also needed to be
826- // counted. For every useful node, we track 2 extra hashes as the flushlist.
827- var flushlistSize = common .StorageSize ((len (db .dirties ) - 1 ) * 2 * common .HashLength )
828- return db .dirtiesSize + flushlistSize , db .preimagesSize
855+ // counted.
856+ var metadataSize = common .StorageSize ((len (db .dirties ) - 1 ) * cachedNodeSize )
857+ var metarootRefs = common .StorageSize (len (db .dirties [common.Hash {}].children ) * (common .HashLength + 2 ))
858+ return db .dirtiesSize + db .childrenSize + metadataSize - metarootRefs , db .preimagesSize
829859}
830860
831861// verifyIntegrity is a debug method to iterate over the entire trie stored in
0 commit comments