Skip to content

Replace O(n²) sync algorithms with HashMap lookups #241

@schjonhaug

Description

@schjonhaug

Parent: #236 (Phase 1)

Problem

CPFP detection and RBF conflict detection in sync.rs use .find() inside loops over all BDK transactions, resulting in O(n²) complexity. At 50K+ transactions, sync takes minutes instead of milliseconds.

What to change

CPFP detection

Replace:

all_bdk_txs.iter().find(|tx| tx.txid.to_string() == *txid)

With a pre-built HashMap<String, &CanonicalTx> keyed by txid, built once before the loop.

RBF conflict detection

Same pattern — the nested loop searches all_bdk_txs linearly for each conflicted and canonical txid. Build the HashMap once, do O(1) lookups.

Expected result

  • O(N) total instead of O(N²)
  • 50K tx sync: <100ms instead of minutes
  • Memory: one HashMap allocation (~50K entries) instead of repeated linear scans

Key files

  • backend/src/sync.rs — CPFP detection and RBF conflict detection

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions