perf: amortize handle iterator (#10002)

This commit is contained in:
jrandolf 2023-04-11 15:36:03 +02:00 committed by GitHub
parent dba1b2d9cc
commit ab27f738c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,7 +29,7 @@ const DEFAULT_BATCH_SIZE = 20;
*/ */
async function* fastTransposeIteratorHandle<T>( async function* fastTransposeIteratorHandle<T>(
iterator: JSHandle<AwaitableIterator<T>>, iterator: JSHandle<AwaitableIterator<T>>,
size = DEFAULT_BATCH_SIZE size: number
) { ) {
const array = await iterator.evaluateHandle(async (iterator, size) => { const array = await iterator.evaluateHandle(async (iterator, size) => {
const results = []; const results = [];
@ -56,8 +56,11 @@ async function* fastTransposeIteratorHandle<T>(
async function* transposeIteratorHandle<T>( async function* transposeIteratorHandle<T>(
iterator: JSHandle<AwaitableIterator<T>> iterator: JSHandle<AwaitableIterator<T>>
) { ) {
let size = DEFAULT_BATCH_SIZE;
try { try {
while (!(yield* fastTransposeIteratorHandle(iterator))) {} while (!(yield* fastTransposeIteratorHandle(iterator, size))) {
size <<= 1;
}
} finally { } finally {
await iterator.dispose(); await iterator.dispose();
} }