bdk icon indicating copy to clipboard operation
bdk copied to clipboard

The `TxGraph` spends field does not need to be a `BTreeMap`, could be a `HashMap` instead.

Open oleonardolima opened this issue 3 months ago • 1 comments

Describe the enhancement

I was discussing with @evanlinjin about the CanonicalIter, and we noticed a possible optimization on the TxGraph spends field.

It's currently a BTreeMap, but it could be updated to become a HashMap instead. However, it would require reworking the TxDescendants::populate_queue() method.

Use case

All fields in TxGraph are only either a HashMap, HashSet or BTreeSet.

Impact

  • [ ] Blocking production usage
  • [x] Nice-to-have / UX improvement
  • [x] Developer experience / maintainability

Are you using BDK in a production project?

  • [x] Yes
  • [ ] No
  • [ ] Not yet, but planning to

Which backend(s) are relevant (if any)?

  • [ ] Electrum
  • [ ] Esplora
  • [ ] Bitcoin Core RPC
  • [x] None / not backend-related (e.g. bdk_chain, bdk_core)
  • [ ] Other (please specify): ____

Project or organization (optional)

Additional context

oleonardolima avatar Sep 10 '25 07:09 oleonardolima

Yes, switching to HashMap will decrease the lookup time, since it provides average-case O(1) lookups compared to BTreeMap’s O(log n). This should make lookups in TxGraph faster, but the trade-off is that HashMap does not preserve key order, so if canonical iteration depends on sorted order, we may need to sort separately.

ShivamS-D avatar Sep 11 '25 14:09 ShivamS-D