Skip to content

Commit 05f64d5

Browse files
committed
perf: improve parallel await on init
This fixes several instances in the code during the initialization process for `xud` where we're unnecessarily awaiting for promises to resolve serially, instead awaiting for them in parallel to shave a bit of time off the startup process.
1 parent 31a5b80 commit 05f64d5

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

lib/db/DB.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class DB {
5858
if (newDb && initDb) {
5959
// populate new databases with default data
6060
// TODO: make seed peers configurable
61-
await Node.bulkCreate(<db.NodeAttributes[]>[
61+
const promises: Bluebird<any>[] = [];
62+
promises.push(Node.bulkCreate(<db.NodeAttributes[]>[
6263
{
6364
nodePubKey: '02b66438730d1fcdf4a4ae5d3d73e847a272f160fee2938e132b52cab0a0d9cfc6',
6465
addresses: [{ host: 'xud1.test.exchangeunion.com', port: 8885 }],
@@ -71,14 +72,16 @@ class DB {
7172
nodePubKey: '03fd337659e99e628d0487e4f87acf93e353db06f754dccc402f2de1b857a319d0',
7273
addresses: [{ host: 'xud3.test.exchangeunion.com', port: 8885 }],
7374
},
74-
]);
75+
]));
7576

76-
await Currency.bulkCreate(<db.CurrencyAttributes[]>[
77+
promises.push(Currency.bulkCreate(<db.CurrencyAttributes[]>[
7778
{ id: 'BTC', swapClient: SwapClients.Lnd, decimalPlaces: 8 },
7879
{ id: 'LTC', swapClient: SwapClients.Lnd, decimalPlaces: 8 },
7980
{ id: 'ZRX', swapClient: SwapClients.Raiden, decimalPlaces: 18 },
8081
{ id: 'GNT', swapClient: SwapClients.Raiden, decimalPlaces: 18 },
81-
]);
82+
]));
83+
84+
await Promise.all(promises);
8285

8386
await Pair.bulkCreate(<db.PairAttributes[]>[
8487
{ baseCurrency: 'LTC', quoteCurrency: 'BTC' },

lib/orderbook/OrderBook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class OrderBook extends EventEmitter {
7575

7676
/** Loads the supported pairs and currencies from the database. */
7777
public init = async () => {
78-
const promises = [await this.repository.getPairs(), await this.repository.getCurrencies()];
78+
const promises: PromiseLike<any>[] = [this.repository.getPairs(), this.repository.getCurrencies()];
7979
const results = await Promise.all(promises);
8080
const pairs = results[0] as db.PairInstance[];
8181
const currencies = results[1] as db.CurrencyInstance[];

0 commit comments

Comments
 (0)