Skip to content

Commit

Permalink
Merge pull request #6248 from Agoric/gibson-6225-makeSnapshot-perform…
Browse files Browse the repository at this point in the history
…ance

Improve the efficiency of writing snapshots
  • Loading branch information
mergify[bot] authored Sep 17, 2022
2 parents df83bf2 + 7bac0b9 commit 26f482b
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 100 deletions.
11 changes: 8 additions & 3 deletions packages/SwingSet/misc-tools/replay-transcript.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import path from 'path';
import { promisify } from 'util';
import { createHash } from 'crypto';
import { pipeline } from 'stream';
import { performance } from 'perf_hooks';
// eslint-disable-next-line import/no-extraneous-dependencies
import { tmpName } from 'tmp';
import { file as tmpFile, tmpName } from 'tmp';
import bundleSource from '@endo/bundle-source';
import { makeMeasureSeconds } from '@agoric/internal';
import { makeSnapStore } from '@agoric/swing-store';
import { waitUntilQuiescent } from '../src/lib-nodejs/waitUntilQuiescent.js';
import { makeStartXSnap } from '../src/controller/controller.js';
Expand Down Expand Up @@ -59,14 +61,17 @@ async function fileHash(filename) {

function makeSnapStoreIO() {
return {
tmpName,
createReadStream: fs.createReadStream,
createWriteStream: fs.createWriteStream,
fsync: fs.fsync,
measureSeconds: makeMeasureSeconds(performance.now),
open: fs.promises.open,
rename: fs.promises.rename,
resolve: path.resolve,
stat: fs.promises.stat,
tmpFile,
tmpName,
unlink: fs.promises.unlink,
resolve: path.resolve,
};
}

Expand Down
2 changes: 0 additions & 2 deletions packages/SwingSet/src/kernel/state/vatKeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ export function makeVatKeeper(
newFile,
rawByteCount,
rawSaveSeconds,
hashSeconds,
compressedByteCount,
compressSeconds,
} = info;
Expand All @@ -617,7 +616,6 @@ export function makeVatKeeper(
newFile,
rawByteCount,
rawSaveSeconds,
hashSeconds,
compressedByteCount,
compressSeconds,
endPosition,
Expand Down
3 changes: 0 additions & 3 deletions packages/SwingSet/test/snapshots/test-xsnap-store.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Generated by [AVA](https://avajs.dev).
{
compressSeconds: 0,
hash: '8a0e3873976c50462d1b1dac59c912152b0e5cad5eeb9deca0ca64a087b4a873',
hashSeconds: 0,
newFile: true,
rawByteCount: 167887,
rawSaveSeconds: 0,
Expand All @@ -22,7 +21,6 @@ Generated by [AVA](https://avajs.dev).
{
compressSeconds: 0,
hash: 'bab1971161e5ca90d78e84daaf5622312e73f87aa2ca67a7ed186bb6bc7ea732',
hashSeconds: 0,
newFile: true,
rawByteCount: 775391,
rawSaveSeconds: 0,
Expand All @@ -33,7 +31,6 @@ Generated by [AVA](https://avajs.dev).
{
compressSeconds: 0,
hash: '803f50cf88b9f1870dbd69f418644661a3f21a87b44feaa0c66c7660b31aa46f',
hashSeconds: 0,
newFile: true,
rawByteCount: 777295,
rawSaveSeconds: 0,
Expand Down
Binary file modified packages/SwingSet/test/snapshots/test-xsnap-store.js.snap
Binary file not shown.
56 changes: 56 additions & 0 deletions packages/internal/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,59 @@ export const makeMeasureSeconds = currentTimeMillisec => {
};
return measureSeconds;
};

/**
* @param {Error[]} errors
* @param {string} [message]
*/
const makeAggregateError = (errors, message) => {
const err = new Error(message);
Object.defineProperties(err, {
name: {
value: 'AggregateError',
},
errors: {
value: errors,
},
});
return err;
};

/**
* @template T
* @param {readonly (T | PromiseLike<T>)[]} values
* @returns {Promise<T[]>}
*/
export const PromiseAllOrErrors = async values => {
return Promise.allSettled(values).then(results => {
const errors = /** @type {PromiseRejectedResult[]} */ (
results.filter(({ status }) => status === 'rejected')
).map(result => result.reason);
if (!errors.length) {
return /** @type {PromiseFulfilledResult<T>[]} */ (results).map(
result => result.value,
);
} else if (errors.length === 1) {
throw errors[0];
} else {
throw makeAggregateError(errors);
}
});
};

/**
* @type {<T>(
* trier: () => Promise<T>,
* finalizer: (error?: unknown) => Promise<void>,
* ) => Promise<T>}
*/ export const aggregateTryFinally = async (trier, finalizer) =>
trier().then(
async result => finalizer().then(() => result),
async tryError =>
finalizer(tryError)
.then(
() => tryError,
finalizeError => makeAggregateError([tryError, finalizeError]),
)
.then(error => Promise.reject(error)),
);
Loading

0 comments on commit 26f482b

Please sign in to comment.