Skip to content

[perf] async/await parallelization opportunities — plan loop + chunk batch + file ops #785

@jlin53882

Description

@jlin53882

Analysis Summary

This issue documents async/await parallelization opportunities found in memory-lancedb-pro.

Unit Test Proof Results

Test Sequential Parallel Speedup Priority
memory-compactor plan loop 943ms 94ms 10.0x HIGH
store.ts doFlush chunk 156ms 62ms 2.5x MEDIUM
self-improvement-files ensureFile 94ms 46ms 2.0x LOW

Test file: test/async-parallel-simple.mjs

Related Issues

Recommended Fixes

1. memory-compactor.ts plan loop (HIGH priority)

Location: src/memory-compactor.ts:196-230

Current:

for (const plan of plans) {
  const vector = await embedder.embedPassage(plan.merged.text);
  await store.store({...});
  for (const m of members) {
    await store.delete(m.id);
  }
}

Proposed:

await Promise.all(plans.map(async (plan) => {
  const vector = await embedder.embedPassage(plan.merged.text);
  await store.store({...});
  await Promise.all(plan.memberIndices.map(m => store.delete(m.id)));
}));

2. store.ts doFlush chunk (MEDIUM priority)

Location: src/store.ts:647-654

Current:

for (const chunk of chunks) {
  await this.runWithFileLock(async () => {
    await this.table!.add(chunk);
  });
}

Proposed (batch parallel):

const BATCH_SIZE = 3;
for (let i = 0; i < chunks.length; i += BATCH_SIZE) {
  const batch = chunks.slice(i, i + BATCH_SIZE);
  await Promise.all(batch.map(chunk => 
    this.runWithFileLock(() => this.table!.add(chunk))
  );
}

3. self-improvement-files.ts ensureFile (LOW priority)

Location: src/self-improvement-files.ts:69-70

Current:

await ensureFile(join(learningsDir, "LEARNINGS.md"), DEFAULT_LEARNINGS_TEMPLATE);
await ensureFile(join(learningsDir, "ERRORS.md"), DEFAULT_ERRORS_TEMPLATE);

Proposed:

await Promise.all([
  ensureFile(join(learningsDir, "LEARNINGS.md"), DEFAULT_LEARNINGS_TEMPLATE),
  ensureFile(join(learningsDir, "ERRORS.md"), DEFAULT_ERRORS_TEMPLATE)
]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions