@@ -641,6 +641,7 @@ pub struct TrieDBMutBuilder<'db, L: TrieLayout> {
641641 root : & ' db mut TrieHash < L > ,
642642 cache : Option < & ' db mut dyn TrieCache < L :: Codec > > ,
643643 recorder : Option < & ' db mut dyn TrieRecorder < TrieHash < L > > > ,
644+ commit_on_drop : bool ,
644645}
645646
646647impl < ' db , L : TrieLayout > TrieDBMutBuilder < ' db , L > {
@@ -649,7 +650,7 @@ impl<'db, L: TrieLayout> TrieDBMutBuilder<'db, L> {
649650 pub fn new ( db : & ' db mut dyn HashDB < L :: Hash , DBValue > , root : & ' db mut TrieHash < L > ) -> Self {
650651 * root = L :: Codec :: hashed_null_node ( ) ;
651652
652- Self { root, db, cache : None , recorder : None }
653+ Self { root, db, cache : None , recorder : None , commit_on_drop : true }
653654 }
654655
655656 /// Create a builder for constructing a new trie with the backing database `db` and `root`.
@@ -660,7 +661,7 @@ impl<'db, L: TrieLayout> TrieDBMutBuilder<'db, L> {
660661 db : & ' db mut dyn HashDB < L :: Hash , DBValue > ,
661662 root : & ' db mut TrieHash < L > ,
662663 ) -> Self {
663- Self { db, root, cache : None , recorder : None }
664+ Self { db, root, cache : None , recorder : None , commit_on_drop : true }
664665 }
665666
666667 /// Use the given `cache` for the db.
@@ -695,7 +696,24 @@ impl<'db, L: TrieLayout> TrieDBMutBuilder<'db, L> {
695696 self
696697 }
697698
699+ /// Disable automatic commit on drop.
700+ ///
701+ /// By default, [`TrieDBMut`] automatically commits changes when dropped. Calling this method
702+ /// disables that behavior, requiring explicit calls to [`TrieDBMut::commit`] to persist
703+ /// changes to the database.
704+ ///
705+ /// This is useful when you want fine-grained control over when changes are committed, or when
706+ /// you want to avoid the performance cost of committing if you're just doing temporary
707+ /// operations.
708+ pub fn disable_commit_on_drop ( mut self ) -> Self {
709+ self . commit_on_drop = false ;
710+ self
711+ }
712+
698713 /// Build the [`TrieDBMut`].
714+ ///
715+ /// By default, the returned trie will automatically commit changes when dropped. Use
716+ /// [`disable_commit_on_drop`](Self::disable_commit_on_drop) to disable this behavior.
699717 pub fn build ( self ) -> TrieDBMut < ' db , L > {
700718 let root_handle = NodeHandle :: Hash ( * self . root ) ;
701719
@@ -707,17 +725,19 @@ impl<'db, L: TrieLayout> TrieDBMutBuilder<'db, L> {
707725 storage : NodeStorage :: empty ( ) ,
708726 death_row : Default :: default ( ) ,
709727 root_handle,
728+ commit_on_drop : self . commit_on_drop ,
710729 }
711730 }
712731}
713732
714733/// A `Trie` implementation using a generic `HashDB` backing database.
715734///
716- /// Use it as a `TrieMut` trait object. You can use `db()` to get the backing database object.
735+ /// Use it as a [ `TrieMut`] trait object. You can use `db()` to get the backing database object.
717736/// Note that changes are not committed to the database until `commit` is called.
718737///
719- /// Querying the root or dropping the trie will commit automatically.
738+ /// Querying the root of the trie will commit automatically.
720739///
740+ /// Dropping the instance may or may not commit depending on [Self::commit_on_drop] flag.
721741///
722742/// # Example
723743/// ```ignore
@@ -751,6 +771,8 @@ where
751771 cache : Option < & ' a mut dyn TrieCache < L :: Codec > > ,
752772 /// Optional trie recorder for recording trie accesses.
753773 recorder : Option < core:: cell:: RefCell < & ' a mut dyn TrieRecorder < TrieHash < L > > > > ,
774+ /// Whether to commit on drop.
775+ commit_on_drop : bool ,
754776}
755777
756778impl < ' a , L > TrieDBMut < ' a , L >
@@ -2103,7 +2125,9 @@ where
21032125 L : TrieLayout ,
21042126{
21052127 fn drop ( & mut self ) {
2106- self . commit ( ) ;
2128+ if self . commit_on_drop {
2129+ self . commit ( ) ;
2130+ }
21072131 }
21082132}
21092133
0 commit comments