Skip to content

Commit 3e41d2c

Browse files
authored
Merge pull request #115 from alexanderwiederin/chain-iter-last-docs
Docs(core): Add performance warning for Chain iterator
2 parents 12df04b + 0e1e260 commit 3e41d2c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/state/chain.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ use super::ChainstateManager;
2626
/// chain, starting from the genesis block (height 0) and continuing
2727
/// through to the chain tip.
2828
///
29+
/// # Performance Note
30+
/// This iterator traverses blocks sequentially from genesis. Common operations
31+
/// have the following complexity:
32+
/// - `.next()`: O(1) - retrieves next block by height
33+
/// - `.last()`: O(N) - iterates through entire chain
34+
/// - `.nth(n)`: O(N) - skips N blocks
35+
///
36+
/// For direct access, prefer using [`Chain`] methods:
37+
/// - [`Chain::tip()`] - O(1) access to chain tip (instead of `.last()`)
38+
/// - [`Chain::at_height()`] - O(1) access to specific height (instead of `.nth()`)
39+
///
2940
/// # Lifetime
3041
/// The iterator is tied to the lifetime of the [`Chain`] it was created from,
3142
/// which in turn is tied to the [`ChainstateManager`]. The iterator becomes
@@ -227,6 +238,11 @@ impl<'a> Chain<'a> {
227238
/// for each block in the chain, starting from the genesis block (height 0) and
228239
/// continuing sequentially to the current tip.
229240
///
241+
/// # Performance
242+
/// **Warning:** Avoid calling `.last()` on this iterator, as it requires O(N)
243+
/// iteration through all blocks. Use [`chain.tip()`](Chain::tip) instead,
244+
/// which is O(1).
245+
///
230246
/// # Returns
231247
/// A [`ChainIterator`] that traverses the entire chain.
232248
///

0 commit comments

Comments
 (0)