Skip to content

Commit 0621ca8

Browse files
committed
Merge #579: Faster sync by collecting esplora ureq thread handles
adef166 Create vector of thread handles to spawn threads (nickfarrow) Pull request description: ### Description Speeds up esplora ureq syncing. Taken from #560 The current sync just creates a map of scripts to joinhandles which doesn't yet spawn the sync threads due to lazy evaluation. In the following `handles.map()`, the thread handles are *sequentially* evaluated and joined. With the fix, the handles are collected so that the threads spawn in parallel, and then joined ### Notes to the reviewers I had to add a `#[allow(clippy::needless_collect)]` so that it wouldn't complain about collecting and then iterating. (Perhaps clippy is partially responsible for this issue!) Tested sync performance by doing a fresh sync on an existing [gun](https://gun.fun) wallet. ``` ---- Before fix --- real0m13.121s real0m13.367s real0m13.211s ---- After fix ---- real0m5.516s real0m5.251s real0m5.594s ``` ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### Bugfixes: * [ ] This pull request breaks the existing API * [ ] I've added tests to reproduce the issue which are now passing * [ ] I'm linking the issue being fixed by this PR ACKs for top commit: notmandatory: ACK adef166 Tree-SHA512: 47c617117afde9b4706bfa63759bf06d1ec60ff95d8a80931e5b7e40e3293c855d2f7dac0c681173d43aecf77201a842e739b82291da09ac81909cf526a51c8d
2 parents 213f18f + adef166 commit 0621ca8

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/blockchain/esplora/ureq.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ impl WalletSync for EsploraBlockchain {
127127
.take(self.concurrency as usize)
128128
.cloned();
129129

130-
let handles = scripts.map(move |script| {
130+
let mut handles = vec![];
131+
for script in scripts {
131132
let client = self.url_client.clone();
132133
// make each request in its own thread.
133-
std::thread::spawn(move || {
134+
handles.push(std::thread::spawn(move || {
134135
let mut related_txs: Vec<Tx> = client._scripthash_txs(&script, None)?;
135136

136137
let n_confirmed =
@@ -152,10 +153,11 @@ impl WalletSync for EsploraBlockchain {
152153
}
153154
}
154155
Result::<_, Error>::Ok(related_txs)
155-
})
156-
});
156+
}));
157+
}
157158

158159
let txs_per_script: Vec<Vec<Tx>> = handles
160+
.into_iter()
159161
.map(|handle| handle.join().unwrap())
160162
.collect::<Result<_, _>>()?;
161163
let mut satisfaction = vec![];

0 commit comments

Comments
 (0)