Skip to content

Commit

Permalink
Improve shield sync time (#472)
Browse files Browse the repository at this point in the history
* fix: Edge case in which reader is empty

* feat: Use a smaller batch size and measure the actual time
  • Loading branch information
panleone authored Nov 19, 2024
1 parent c95fe65 commit a44f5f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions scripts/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export class Reader {
}
if (done) {
this.#done = true;
if (this.#awaiter) {
this.#awaiter();
}
break;
}
}
Expand Down
19 changes: 17 additions & 2 deletions scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ import { guiToggleReceiveType } from './contacts-book.js';
import { TransactionBuilder } from './transaction_builder.js';
import { createAlert } from './alerts/alert.js';
import { AsyncInterval } from './async_interval.js';
import { debugError, debugLog, DebugTopics } from './debug.js';
import {
debugError,
debugLog,
debugTimerEnd,
debugTimerStart,
DebugTopics,
} from './debug.js';
import { OrderedArray } from './ordered_array.js';

/**
Expand Down Expand Up @@ -735,7 +741,9 @@ export class Wallet {
this.#lastProcessedBlock = blockCount - 5;
await this.#transparentSync();
if (this.hasShield()) {
debugTimerStart(DebugTopics.WALLET, 'syncShield');
await this.#syncShield();
debugTimerEnd(DebugTopics.WALLET, 'syncShield');
}
this.#isSynced = true;
// At this point download the last missing blocks in the range (blockCount -5, blockCount]
Expand Down Expand Up @@ -806,11 +814,14 @@ export class Wallet {
* @type {{txs: string[]; height: number; time: number}[]}
*/
let blocksArray = [];
let handleBlocksTime = 0;
const handleAllBlocks = async () => {
const start = performance.now();
// Process the current batch of blocks before starting to parse the next one
if (blocksArray.length) {
await this.#shield.handleBlocks(blocksArray);
}
handleBlocksTime += performance.now() - start;
blocksArray = [];
// Emit status update
getEventEmitter().emit(
Expand Down Expand Up @@ -844,11 +855,15 @@ export class Wallet {
// This is neither a block or a tx.
throw new Error('Failed to parse shield binary');
}
if (blocksArray.length > 1000) {
if (blocksArray.length >= 10) {
await handleAllBlocks();
}
}
await handleAllBlocks();
debugLog(
DebugTopics.WALLET,
`syncShield rust internal ${handleBlocksTime} ms`
);
// At this point it should be safe to assume that shield is ready to use
await this.saveShieldOnDisk();
} catch (e) {
Expand Down

0 comments on commit a44f5f0

Please sign in to comment.