mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-11-05 04:11:53 +00:00
Merge pull request #418 from PR0M3TH3AN/refactor/memory-coalescing-worker-7175585698335360965
Refactor memory store save logic to use CoalescingWorker
This commit is contained in:
1 file changed
+47
-24
@@ -38,9 +38,50 @@ function loadMemoryStore() {
|
||||
return new Map();
|
||||
}
|
||||
|
||||
let currentSavePromise = null;
|
||||
let pendingSavePromise = null;
|
||||
let pendingSaveResolve = null;
|
||||
class CoalescingWorker {
|
||||
constructor(workerFn) {
|
||||
this.workerFn = workerFn;
|
||||
this.currentPromise = null;
|
||||
this.pendingPromise = null;
|
||||
this.pendingResolve = null;
|
||||
this.pendingArg = null;
|
||||
}
|
||||
|
||||
enqueue(arg) {
|
||||
this.pendingArg = arg;
|
||||
|
||||
if (this.pendingPromise) {
|
||||
return this.pendingPromise;
|
||||
}
|
||||
|
||||
if (this.currentPromise) {
|
||||
this.pendingPromise = new Promise((resolve) => {
|
||||
this.pendingResolve = resolve;
|
||||
});
|
||||
return this.pendingPromise;
|
||||
}
|
||||
|
||||
this.currentPromise = this._execute(arg);
|
||||
return this.currentPromise;
|
||||
}
|
||||
|
||||
async _execute(arg) {
|
||||
try {
|
||||
await this.workerFn(arg);
|
||||
} finally {
|
||||
this.currentPromise = null;
|
||||
if (this.pendingPromise) {
|
||||
const resolve = this.pendingResolve;
|
||||
const nextArg = this.pendingArg;
|
||||
this.pendingPromise = null;
|
||||
this.pendingResolve = null;
|
||||
this.pendingArg = null;
|
||||
this.currentPromise = this._execute(nextArg).then(resolve);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let lastSaveTime = 0;
|
||||
const MIN_SAVE_INTERVAL_MS = 1000;
|
||||
|
||||
@@ -61,31 +102,13 @@ async function performSave(store) {
|
||||
lastSaveTime = Date.now();
|
||||
} catch (err) {
|
||||
console.error('Failed to save memory store:', err);
|
||||
} finally {
|
||||
currentSavePromise = null;
|
||||
if (pendingSavePromise) {
|
||||
const resolve = pendingSaveResolve;
|
||||
pendingSavePromise = null;
|
||||
pendingSaveResolve = null;
|
||||
currentSavePromise = performSave(store).then(() => resolve());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const saveWorker = new CoalescingWorker(performSave);
|
||||
|
||||
async function saveMemoryStore(store) {
|
||||
if (pendingSavePromise) {
|
||||
return pendingSavePromise;
|
||||
}
|
||||
|
||||
if (currentSavePromise) {
|
||||
pendingSavePromise = new Promise((resolve) => {
|
||||
pendingSaveResolve = resolve;
|
||||
});
|
||||
return pendingSavePromise;
|
||||
}
|
||||
|
||||
currentSavePromise = performSave(store);
|
||||
return currentSavePromise;
|
||||
return saveWorker.enqueue(store);
|
||||
}
|
||||
|
||||
const memoryStore = loadMemoryStore();
|
||||
|
||||
Reference in new issue
Block a user