Skip to content

Commit

Permalink
feat: use atomics to perform batch inserts (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva authored Dec 10, 2024
1 parent c9e0393 commit efcf188
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/orama/src/methods/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,16 +316,18 @@ async function innerInsertMultipleAsync<T extends AnyOrama>(

const processAllBatches = async (): Promise<void> => {
let currentIndex = 0
const sab = new SharedArrayBuffer(4)
const ia = new Int32Array(sab)

while (currentIndex < docs.length) {
const startTime = Date.now()

currentIndex = await processNextBatch(currentIndex)

if (timeout > 0) {
const elapsedTime = Date.now() - startTime
if (elapsedTime < timeout) {
await new Promise((resolve) => setTimeout(resolve, timeout - elapsedTime))
const waitTime = timeout - elapsedTime
if (waitTime > 0) {
Atomics.wait(ia, 0, 0, waitTime)
}
}
}
Expand Down Expand Up @@ -367,6 +369,8 @@ function innerInsertMultipleSync<T extends AnyOrama>(

function processAllBatches() {
const startTime = Date.now()
const sab = new SharedArrayBuffer(4)
const ia = new Int32Array(sab)

// eslint-disable-next-line no-constant-condition
while (true) {
Expand All @@ -376,11 +380,9 @@ function innerInsertMultipleSync<T extends AnyOrama>(
if (timeout > 0) {
const elapsedTime = Date.now() - startTime
if (elapsedTime >= timeout) {
// Synchronously wait for the remaining time
const remainingTime = timeout - (elapsedTime % timeout)
const endTime = Date.now() + remainingTime
while (Date.now() < endTime) {
// Do nothing, just wait
if (remainingTime > 0) {
Atomics.wait(ia, 0, 0, remainingTime)
}
}
}
Expand Down

0 comments on commit efcf188

Please sign in to comment.