perf(main): reuse task snapshots for projections

This commit is contained in:
777genius 2026-05-30 23:30:39 +03:00
parent b32cc59b46
commit 172ddad18a
2 changed files with 18 additions and 10 deletions

View file

@ -1107,7 +1107,13 @@ export class TeamDataService {
}
async getAllTasks(): Promise<GlobalTask[]> {
const rawTasks = await this.taskReader.getAllTasks();
const taskReader = this.taskReader as TeamTaskReader & {
getAllTasksProjectionSnapshot?: () => Promise<readonly (TeamTask & { teamName: string })[]>;
};
const rawTasks =
typeof taskReader.getAllTasksProjectionSnapshot === 'function'
? await taskReader.getAllTasksProjectionSnapshot()
: await taskReader.getAllTasks();
const teamInfoMap = await this.readGlobalTaskTeamInfo(rawTasks);
const MAX_GLOBAL_TASKS_EXPORTED = 500;

View file

@ -566,28 +566,31 @@ export class TeamTaskReader {
}
async getAllTasks(): Promise<(TeamTask & { teamName: string })[]> {
const tasks = await this.getAllTasksProjectionSnapshot();
return cloneTasks(tasks);
}
async getAllTasksProjectionSnapshot(): Promise<readonly (TeamTask & { teamName: string })[]> {
const startedAt = Date.now();
const cached = TeamTaskReader.allTasksCache;
if (cached && cached.expiresAt > Date.now()) {
const cloned = cloneTasks(cached.value);
const ms = Date.now() - startedAt;
if (ms >= 1500) {
logger.warn(`[getAllTasks] cache clone slow ms=${ms} tasks=${cloned.length}`);
logger.warn(`[getAllTasks] cache read slow ms=${ms} tasks=${cached.value.length}`);
}
return cloned;
return cached.value;
}
if (TeamTaskReader.allTasksInFlight?.generationAtStart === TeamTaskReader.allTasksGeneration) {
const waitedAt = Date.now();
const tasks = await TeamTaskReader.allTasksInFlight.promise;
const cloned = cloneTasks(tasks);
const ms = Date.now() - startedAt;
if (ms >= 1500) {
logger.warn(
`[getAllTasks] in-flight wait slow ms=${ms} waitMs=${Date.now() - waitedAt} tasks=${cloned.length}`
`[getAllTasks] in-flight wait slow ms=${ms} waitMs=${Date.now() - waitedAt} tasks=${tasks.length}`
);
}
return cloned;
return tasks;
}
const request = this.readAllTasksUncached();
@ -604,12 +607,11 @@ export class TeamTaskReader {
expiresAt: Date.now() + ALL_TASKS_CACHE_TTL_MS,
};
}
const cloned = cloneTasks(tasks);
const ms = Date.now() - startedAt;
if (ms >= 1500) {
logger.warn(`[getAllTasks] total slow ms=${ms} tasks=${cloned.length}`);
logger.warn(`[getAllTasks] total slow ms=${ms} tasks=${tasks.length}`);
}
return cloned;
return tasks;
} finally {
if (TeamTaskReader.allTasksInFlight?.promise === request) {
TeamTaskReader.allTasksInFlight = null;