mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-08 00:04:21 +01:00
⚡ Optimize asynchronous task deletion performance. (#16881)
This commit is contained in:
parent
d7f7ce60a8
commit
8f61082a07
1 changed files with 19 additions and 19 deletions
|
|
@ -19,7 +19,6 @@ package task
|
|||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
|
@ -381,31 +380,32 @@ func popAsyncTasks() (ret []*Task) {
|
|||
return
|
||||
}
|
||||
|
||||
var popedIndexes []int
|
||||
for i, task := range taskQueue {
|
||||
if !task.Async {
|
||||
continue
|
||||
}
|
||||
// writeIdx 指向下一个要写入的位置
|
||||
writeIdx := 0
|
||||
for readIdx := 0; readIdx < len(taskQueue); readIdx++ {
|
||||
task := taskQueue[readIdx]
|
||||
|
||||
if time.Since(task.Created) <= task.Delay {
|
||||
continue
|
||||
}
|
||||
// 判断是否应该弹出此任务
|
||||
shouldPop := task.Async && time.Since(task.Created) > task.Delay
|
||||
|
||||
if task.Async {
|
||||
if shouldPop {
|
||||
ret = append(ret, task)
|
||||
popedIndexes = append(popedIndexes, i)
|
||||
// 不写入 taskQueue,相当于删除
|
||||
} else {
|
||||
// 保留此任务,移动到 writeIdx 位置
|
||||
if writeIdx != readIdx {
|
||||
taskQueue[writeIdx] = task
|
||||
}
|
||||
writeIdx++
|
||||
}
|
||||
}
|
||||
|
||||
if 0 < len(popedIndexes) {
|
||||
var newQueue []*Task
|
||||
for i, task := range taskQueue {
|
||||
if !slices.Contains(popedIndexes, i) {
|
||||
newQueue = append(newQueue, task)
|
||||
}
|
||||
}
|
||||
taskQueue = newQueue
|
||||
// 清理队列尾部的引用,防止内存泄漏
|
||||
for i := writeIdx; i < len(taskQueue); i++ {
|
||||
taskQueue[i] = nil
|
||||
}
|
||||
taskQueue = taskQueue[:writeIdx]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue