@@ -62,17 +62,37 @@ func (t *Trie) Snapshot() (newTrie *Trie) {
62
62
}
63
63
}
64
64
65
+ func (t * Trie ) prepLeafForMutation (currentLeaf * node.Leaf ) (newLeaf * node.Leaf ) {
66
+ if currentLeaf .Generation == t .generation {
67
+ // no need to deep copy and update generation
68
+ // of current leaf.
69
+ newLeaf = currentLeaf
70
+ } else {
71
+ newNode := updateGeneration (currentLeaf , t .generation , t .deletedKeys )
72
+ newLeaf = newNode .(* node.Leaf )
73
+ }
74
+ newLeaf .SetDirty (true )
75
+ return newLeaf
76
+ }
77
+
78
+ func (t * Trie ) prepBranchForMutation (currentBranch * node.Branch ) (newBranch * node.Branch ) {
79
+ if currentBranch .Generation == t .generation {
80
+ // no need to deep copy and update generation
81
+ // of current branch.
82
+ newBranch = currentBranch
83
+ } else {
84
+ newNode := updateGeneration (currentBranch , t .generation , t .deletedKeys )
85
+ newBranch = newNode .(* node.Branch )
86
+ }
87
+ newBranch .SetDirty (true )
88
+ return newBranch
89
+ }
90
+
65
91
// updateGeneration is called when the currentNode is from
66
92
// an older trie generation (snapshot) so we deep copy the
67
93
// node and update the generation on the newer copy.
68
94
func updateGeneration (currentNode Node , trieGeneration uint64 ,
69
95
deletedHashes map [common.Hash ]struct {}) (newNode Node ) {
70
- if currentNode .GetGeneration () == trieGeneration {
71
- panic (fmt .Sprintf (
72
- "current node has the same generation %d as the trie generation, " +
73
- "make sure the caller properly checks for the node generation to " +
74
- "be smaller than the trie generation." , trieGeneration ))
75
- }
76
96
const copyChildren = false
77
97
newNode = currentNode .Copy (copyChildren )
78
98
newNode .SetGeneration (trieGeneration )
@@ -322,29 +342,26 @@ func (t *Trie) insert(parent Node, key, value []byte) (newParent Node) {
322
342
}
323
343
324
344
// TODO ensure all values have dirty set to true
325
- newParent = parent
326
- if parent .GetGeneration () < t .generation {
327
- newParent = updateGeneration (parent , t .generation , t .deletedKeys )
328
- }
329
345
330
- switch newParent .Type () {
346
+ switch parent .Type () {
331
347
case node .BranchType , node .BranchWithValueType :
332
- parentBranch := newParent .(* node.Branch )
348
+ parentBranch := parent .(* node.Branch )
333
349
return t .insertInBranch (parentBranch , key , value )
334
350
default :
335
- parentLeaf := newParent .(* node.Leaf )
351
+ parentLeaf := parent .(* node.Leaf )
336
352
return t .insertInLeaf (parentLeaf , key , value )
337
353
}
338
354
}
339
355
340
356
func (t * Trie ) insertInLeaf (parentLeaf * node.Leaf , key ,
341
357
value []byte ) (newParent Node ) {
342
358
if bytes .Equal (parentLeaf .Key , key ) {
343
- if ! bytes .Equal (value , parentLeaf .Value ) {
344
- parentLeaf .Value = value
345
- parentLeaf .Generation = t .generation
346
- parentLeaf .SetDirty (true )
359
+ if bytes .Equal (value , parentLeaf .Value ) {
360
+ return parentLeaf
347
361
}
362
+
363
+ parentLeaf = t .prepLeafForMutation (parentLeaf )
364
+ parentLeaf .Value = value
348
365
return parentLeaf
349
366
}
350
367
@@ -364,9 +381,9 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key,
364
381
365
382
if len (key ) < len (parentLeafKey ) {
366
383
// Move the current leaf parent as a child to the new branch.
384
+ parentLeaf = t .prepLeafForMutation (parentLeaf )
367
385
childIndex := parentLeafKey [commonPrefixLength ]
368
386
parentLeaf .Key = parentLeaf .Key [commonPrefixLength + 1 :]
369
- parentLeaf .SetDirty (true )
370
387
newBranchParent .Children [childIndex ] = parentLeaf
371
388
}
372
389
@@ -378,9 +395,9 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key,
378
395
newBranchParent .Value = parentLeaf .Value
379
396
} else {
380
397
// make the leaf a child of the new branch
398
+ parentLeaf = t .prepLeafForMutation (parentLeaf )
381
399
childIndex := parentLeafKey [commonPrefixLength ]
382
400
parentLeaf .Key = parentLeaf .Key [commonPrefixLength + 1 :]
383
- parentLeaf .SetDirty (true )
384
401
newBranchParent .Children [childIndex ] = parentLeaf
385
402
}
386
403
childIndex := key [commonPrefixLength ]
@@ -395,9 +412,9 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key,
395
412
}
396
413
397
414
func (t * Trie ) insertInBranch (parentBranch * node.Branch , key , value []byte ) (newParent Node ) {
415
+ parentBranch = t .prepBranchForMutation (parentBranch )
416
+
398
417
if bytes .Equal (key , parentBranch .Key ) {
399
- parentBranch .SetDirty (true )
400
- parentBranch .Generation = t .generation
401
418
parentBranch .Value = value
402
419
return parentBranch
403
420
}
@@ -418,12 +435,9 @@ func (t *Trie) insertInBranch(parentBranch *node.Branch, key, value []byte) (new
418
435
}
419
436
} else {
420
437
child = t .insert (child , remainingKey , value )
421
- child .SetDirty (true )
422
438
}
423
439
424
440
parentBranch .Children [childIndex ] = child
425
- parentBranch .SetDirty (true )
426
- parentBranch .Generation = t .generation
427
441
return parentBranch
428
442
}
429
443
@@ -435,13 +449,11 @@ func (t *Trie) insertInBranch(parentBranch *node.Branch, key, value []byte) (new
435
449
Generation : t .generation ,
436
450
Dirty : true ,
437
451
}
438
- parentBranch .SetDirty (true )
439
452
440
453
oldParentIndex := parentBranch .Key [commonPrefixLength ]
441
454
remainingOldParentKey := parentBranch .Key [commonPrefixLength + 1 :]
442
455
443
456
parentBranch .Key = remainingOldParentKey
444
- parentBranch .Generation = t .generation
445
457
newParentBranch .Children [oldParentIndex ] = parentBranch
446
458
447
459
if len (key ) <= commonPrefixLength {
@@ -653,36 +665,20 @@ func (t *Trie) clearPrefixLimit(parent Node, prefix []byte, limit uint32) (
653
665
return nil , 0 , true
654
666
}
655
667
656
- newParent = parent
657
- if parent .GetGeneration () < t .generation {
658
- newParent = updateGeneration (parent , t .generation , t .deletedKeys )
659
- }
660
-
661
- if newParent .Type () == node .LeafType {
662
- leaf := newParent .(* node.Leaf )
668
+ if parent .Type () == node .LeafType {
669
+ leaf := parent .(* node.Leaf )
663
670
// if prefix is not found, it's also all deleted.
664
671
// TODO check this is the same behaviour as in substrate
665
672
const allDeleted = true
666
673
if bytes .HasPrefix (leaf .Key , prefix ) {
667
674
valuesDeleted = 1
668
675
return nil , valuesDeleted , allDeleted
669
676
}
670
- // not modified so return the leaf of the original
671
- // trie generation. The copied leaf newParent will be
672
- // garbage collected.
673
677
return parent , 0 , allDeleted
674
678
}
675
679
676
- branch := newParent .(* node.Branch )
677
- newParent , valuesDeleted , allDeleted = t .clearPrefixLimitBranch (branch , prefix , limit )
678
- if valuesDeleted == 0 {
679
- // not modified so return the node of the original
680
- // trie generation. The copied newParent will be
681
- // garbage collected.
682
- newParent = parent
683
- }
684
-
685
- return newParent , valuesDeleted , allDeleted
680
+ branch := parent .(* node.Branch )
681
+ return t .clearPrefixLimitBranch (branch , prefix , limit )
686
682
}
687
683
688
684
func (t * Trie ) clearPrefixLimitBranch (branch * node.Branch , prefix []byte , limit uint32 ) (
@@ -714,13 +710,14 @@ func (t *Trie) clearPrefixLimitBranch(branch *node.Branch, prefix []byte, limit
714
710
childPrefix := prefix [len (branch .Key )+ 1 :]
715
711
child := branch .Children [childIndex ]
716
712
717
- newParent = branch // mostly just a reminder for the reader
718
- branch .Children [childIndex ], valuesDeleted , allDeleted = t .clearPrefixLimit (child , childPrefix , limit )
719
- if valuesDeleted > 0 {
720
- branch .SetDirty (true )
721
- newParent = handleDeletion (branch , prefix )
713
+ child , valuesDeleted , allDeleted = t .clearPrefixLimit (child , childPrefix , limit )
714
+ if valuesDeleted == 0 {
715
+ return branch , valuesDeleted , allDeleted
722
716
}
723
717
718
+ branch = t .prepBranchForMutation (branch )
719
+ branch .Children [childIndex ] = child
720
+ newParent = handleDeletion (branch , prefix )
724
721
return newParent , valuesDeleted , allDeleted
725
722
}
726
723
@@ -738,11 +735,16 @@ func (t *Trie) clearPrefixLimitChild(branch *node.Branch, prefix []byte, limit u
738
735
}
739
736
740
737
nilPrefix := ([]byte )(nil )
741
- branch .Children [childIndex ], valuesDeleted = t .deleteNodesLimit (child , nilPrefix , limit )
742
- branch .SetDirty (true )
738
+ child , valuesDeleted = t .deleteNodesLimit (child , nilPrefix , limit )
739
+ if valuesDeleted == 0 {
740
+ allDeleted = branch .Children [childIndex ] == nil
741
+ return branch , valuesDeleted , allDeleted
742
+ }
743
743
744
- newParent = handleDeletion (branch , prefix )
744
+ branch = t .prepBranchForMutation (branch )
745
+ branch .Children [childIndex ] = child
745
746
747
+ newParent = handleDeletion (branch , prefix )
746
748
allDeleted = branch .Children [childIndex ] == nil
747
749
return newParent , valuesDeleted , allDeleted
748
750
}
@@ -757,17 +759,12 @@ func (t *Trie) deleteNodesLimit(parent Node, prefix []byte, limit uint32) (
757
759
return nil , 0
758
760
}
759
761
760
- newParent = parent
761
- if parent .GetGeneration () < t .generation {
762
- newParent = updateGeneration (parent , t .generation , t .deletedKeys )
763
- }
764
-
765
- if newParent .Type () == node .LeafType {
762
+ if parent .Type () == node .LeafType {
766
763
valuesDeleted = 1
767
764
return nil , valuesDeleted
768
765
}
769
766
770
- branch := newParent .(* node.Branch )
767
+ branch := parent .(* node.Branch )
771
768
772
769
fullKey := concatenateSlices (prefix , branch .Key )
773
770
@@ -779,14 +776,14 @@ func (t *Trie) deleteNodesLimit(parent Node, prefix []byte, limit uint32) (
779
776
continue
780
777
}
781
778
779
+ branch = t .prepBranchForMutation (branch )
782
780
branch .Children [i ], newDeleted = t .deleteNodesLimit (child , fullKey , limit )
783
781
if branch .Children [i ] == nil {
784
782
nilChildren ++
785
783
}
786
784
limit -= newDeleted
787
785
valuesDeleted += newDeleted
788
786
789
- branch .SetDirty (true )
790
787
newParent = handleDeletion (branch , fullKey )
791
788
if nilChildren == node .ChildrenCapacity &&
792
789
branch .Value == nil {
@@ -825,23 +822,15 @@ func (t *Trie) clearPrefix(parent Node, prefix []byte) (
825
822
return nil , false
826
823
}
827
824
828
- newParent = parent
829
- if parent .GetGeneration () < t .generation {
830
- newParent = updateGeneration (parent , t .generation , t .deletedKeys )
831
- }
832
-
833
- if bytes .HasPrefix (newParent .GetKey (), prefix ) {
825
+ if bytes .HasPrefix (parent .GetKey (), prefix ) {
834
826
return nil , true
835
827
}
836
828
837
- if newParent .Type () == node .LeafType {
838
- // not modified so return the leaf of the original
839
- // trie generation. The copied newParent will be
840
- // garbage collected.
829
+ if parent .Type () == node .LeafType {
841
830
return parent , false
842
831
}
843
832
844
- branch := newParent .(* node.Branch )
833
+ branch := parent .(* node.Branch )
845
834
846
835
if len (prefix ) == len (branch .Key )+ 1 &&
847
836
bytes .HasPrefix (branch .Key , prefix [:len (prefix )- 1 ]) {
@@ -850,41 +839,32 @@ func (t *Trie) clearPrefix(parent Node, prefix []byte) (
850
839
child := branch .Children [childIndex ]
851
840
852
841
if child == nil {
853
- // child is already nil at the child index
854
- // node is not modified so return the branch of the original
855
- // trie generation. The copied newParent will be
856
- // garbage collected.
857
842
return parent , false
858
843
}
859
844
845
+ branch = t .prepBranchForMutation (branch )
860
846
branch .Children [childIndex ] = nil
861
- branch .SetDirty (true )
862
847
newParent = handleDeletion (branch , prefix )
863
848
return newParent , true
864
849
}
865
850
866
851
noPrefixForNode := len (prefix ) <= len (branch .Key ) ||
867
852
lenCommonPrefix (branch .Key , prefix ) < len (branch .Key )
868
853
if noPrefixForNode {
869
- // not modified so return the branch of the original
870
- // trie generation. The copied newParent will be
871
- // garbage collected.
872
854
return parent , false
873
855
}
874
856
875
857
childIndex := prefix [len (branch .Key )]
876
858
childPrefix := prefix [len (branch .Key )+ 1 :]
877
859
child := branch .Children [childIndex ]
878
860
879
- branch . Children [ childIndex ] , updated = t .clearPrefix (child , childPrefix )
861
+ child , updated = t .clearPrefix (child , childPrefix )
880
862
if ! updated {
881
- // branch not modified so return the branch of the original
882
- // trie generation. The copied newParent will be
883
- // garbage collected.
884
863
return parent , false
885
864
}
886
865
887
- branch .SetDirty (true )
866
+ branch = t .prepBranchForMutation (branch )
867
+ branch .Children [childIndex ] = child
888
868
newParent = handleDeletion (branch , prefix )
889
869
return newParent , true
890
870
}
@@ -902,32 +882,15 @@ func (t *Trie) delete(parent Node, key []byte) (newParent Node, deleted bool) {
902
882
return nil , false
903
883
}
904
884
905
- newParent = parent
906
- if parent .GetGeneration () < t .generation {
907
- newParent = updateGeneration (parent , t .generation , t .deletedKeys )
908
- }
909
-
910
- if newParent .Type () == node .LeafType {
911
- newParent = deleteLeaf (newParent , key )
912
- if newParent == nil {
885
+ if parent .Type () == node .LeafType {
886
+ if deleteLeaf (parent , key ) == nil {
913
887
return nil , true
914
888
}
915
- // The leaf was not deleted so return the original
916
- // parent without its generation updated.
917
- // The copied newParent will be garbage collected.
918
889
return parent , false
919
890
}
920
891
921
- branch := newParent .(* node.Branch )
922
- newParent , deleted = t .deleteBranch (branch , key )
923
- if ! deleted {
924
- // Nothing was deleted so return the original
925
- // parent without its generation updated.
926
- // The copied newParent will be garbage collected.
927
- return parent , false
928
- }
929
-
930
- return newParent , true
892
+ branch := parent .(* node.Branch )
893
+ return t .deleteBranch (branch , key )
931
894
}
932
895
933
896
func deleteLeaf (parent Node , key []byte ) (newParent Node ) {
@@ -939,8 +902,8 @@ func deleteLeaf(parent Node, key []byte) (newParent Node) {
939
902
940
903
func (t * Trie ) deleteBranch (branch * node.Branch , key []byte ) (newParent Node , deleted bool ) {
941
904
if len (key ) == 0 || bytes .Equal (branch .Key , key ) {
905
+ branch = t .prepBranchForMutation (branch )
942
906
branch .Value = nil
943
- branch .SetDirty (true )
944
907
return handleDeletion (branch , key ), true
945
908
}
946
909
@@ -954,8 +917,8 @@ func (t *Trie) deleteBranch(branch *node.Branch, key []byte) (newParent Node, de
954
917
return branch , false
955
918
}
956
919
920
+ branch = t .prepBranchForMutation (branch )
957
921
branch .Children [childIndex ] = newChild
958
- branch .SetDirty (true )
959
922
newParent = handleDeletion (branch , key )
960
923
return newParent , true
961
924
}
0 commit comments