Skip to content

Commit

Permalink
Optimize lookup_txos using rocksdb's MultiGet
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed May 30, 2024
1 parent 672b785 commit 168e241
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
8 changes: 8 additions & 0 deletions src/new_index/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ impl DB {
self.db.get(key).unwrap().map(|v| v.to_vec())
}

pub fn multi_get<K, I>(&self, keys: I) -> Vec<Result<Option<Vec<u8>>, rocksdb::Error>>
where
K: AsRef<[u8]>,
I: IntoIterator<Item = K>,
{
self.db.multi_get(keys)
}

fn verify_compatibility(&self, config: &Config) {
let mut compatibility_bytes = bincode::serialize_little(&DB_VERSION).unwrap();

Expand Down
33 changes: 13 additions & 20 deletions src/new_index/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,26 +1038,19 @@ fn lookup_txos(
outpoints: &BTreeSet<OutPoint>,
allow_missing: bool,
) -> HashMap<OutPoint, TxOut> {
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(16) // we need to saturate SSD IOPS
.thread_name(|i| format!("lookup-txo-{}", i))
.build()
.unwrap();
pool.install(|| {
outpoints
.par_iter()
.filter_map(|outpoint| {
lookup_txo(&txstore_db, &outpoint)
.or_else(|| {
if !allow_missing {
panic!("missing txo {} in {:?}", outpoint, txstore_db);
}
None
})
.map(|txo| (*outpoint, txo))
})
.collect()
})
let mut remain_outpoints = outpoints.iter();
txstore_db
.multi_get(outpoints.iter().map(TxOutRow::key))
.into_iter()
.filter_map(|res| {
let outpoint = remain_outpoints.next().unwrap();
match res.unwrap() {
Some(txo) => Some((*outpoint, deserialize(&txo).expect("failed to parse TxOut"))),
None if allow_missing => None,
None => panic!("missing txo {}", outpoint),
}
})
.collect()
}

fn lookup_txo(txstore_db: &DB, outpoint: &OutPoint) -> Option<TxOut> {
Expand Down

0 comments on commit 168e241

Please sign in to comment.