Skip to content

Commit

Permalink
Better clone performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ajthinking committed Oct 1, 2024
1 parent 207a143 commit 31eeda6
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions packages/core/src/computers/Clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,43 @@ export const Clone: Computer = {
],

async *run({ input, output, params }) {
while(true) {
const incoming = input.pull()
output.pushTo('original', incoming)
while (true) {
const startTime = Date.now();

const count = Number(params.count)
const incoming = input.pull();
output.pushTo('original', incoming);

const count = Number(params.count);
const clones = [];
const BATCH_SIZE = 10000; // Adjust based on your memory constraints

for (let i = 0; i < count; i++) {
output.pushTo('clones', incoming.map(item => ({
...item.value,
_clone_id: i,
})))
for (const item of incoming) {
// Efficient object cloning without spread operator
const clonedItem = Object.assign({}, item.value, { _clone_id: i });
clones.push(clonedItem);

// Push in batches to manage memory
if (clones.length >= BATCH_SIZE) {
await output.pushTo('clones', clones.splice(0, BATCH_SIZE));
}
}
}

// Push any remaining clones
if (clones.length > 0) {
await output.pushTo('clones', clones);
}

// Clear large arrays to free memory
incoming.length = 0;
clones.length = 0;

const endTime = Date.now();
console.log('Clone time:', endTime - startTime, 'ms');

yield;
}
},

};

0 comments on commit 31eeda6

Please sign in to comment.