Backporting a bunch of optimizations from db-only branch

This commit is contained in:
Zef Hemel
2024-01-13 17:30:15 +01:00
parent 509ece91f0
commit bf1eb03129
24 changed files with 264 additions and 104 deletions
+18 -2
View File
@@ -1,5 +1,5 @@
import { assertEquals } from "../../test_deps.ts";
import { PromiseQueue, sleep } from "./async.ts";
import { assert, assertEquals } from "../../test_deps.ts";
import { batchRequests, PromiseQueue, sleep } from "./async.ts";
Deno.test("PromiseQueue test", async () => {
const q = new PromiseQueue();
@@ -24,3 +24,19 @@ Deno.test("PromiseQueue test", async () => {
});
assertEquals(wasRun, true);
});
Deno.test("Batch test", async () => {
// Generate an array with numbers up to 100
const elements = Array.from(Array(100).keys());
const multiplied = await batchRequests(elements, async (batch) => {
await sleep(2);
// Batches should be 9 or smaller (last batch will be smaller)
assert(batch.length <= 9);
return batch.map((e) => e * 2);
}, 9);
assertEquals(multiplied, elements.map((e) => e * 2));
const multiplied2 = await batchRequests(elements, async (batch) => {
return batch.map((e) => e * 2);
}, 10000);
assertEquals(multiplied2, elements.map((e) => e * 2));
});
+22
View File
@@ -67,3 +67,25 @@ export class PromiseQueue {
this.run(); // Continue processing the next promise in the queue
}
}
export async function batchRequests<I, O>(
values: I[],
fn: (batch: I[]) => Promise<O[]>,
batchSize: number,
): Promise<O[]> {
const results: O[] = [];
// Split values into batches of batchSize
const batches: I[][] = [];
for (let i = 0; i < values.length; i += batchSize) {
batches.push(values.slice(i, i + batchSize));
}
// Run fn on them in parallel
const batchResults = await Promise.all(batches.map(fn));
// Flatten the results
for (const batchResult of batchResults) {
if (Array.isArray(batchResult)) { // If fn returns an array, collect them
results.push(...batchResult);
}
}
return results;
}
+5
View File
@@ -73,6 +73,11 @@ export type Query = {
render?: string;
renderAll?: boolean;
distinct?: boolean;
/**
* When set, the DS implementation _may_ cache the result for the given number of seconds.
*/
cacheSecs?: number;
};
export type KvQuery = Omit<Query, "querySource"> & {