Skip to content

Commit 90dc692

Browse files
Optimize web store slice reconstruction (#8)
Fuse normalized slice construction into single-pass loops and avoid flatMap allocation when rebuilding derived project/thread lists. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6f34004 commit 90dc692

1 file changed

Lines changed: 54 additions & 30 deletions

File tree

apps/web/src/store.ts

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -479,62 +479,86 @@ function buildMessageSlice(thread: Thread): {
479479
ids: MessageId[];
480480
byId: Record<MessageId, ChatMessage>;
481481
} {
482-
return {
483-
ids: thread.messages.map((message) => message.id),
484-
byId: Object.fromEntries(
485-
thread.messages.map((message) => [message.id, message] as const),
486-
) as Record<MessageId, ChatMessage>,
487-
};
482+
const ids: MessageId[] = [];
483+
const byId = {} as Record<MessageId, ChatMessage>;
484+
485+
for (const message of thread.messages) {
486+
ids.push(message.id);
487+
byId[message.id] = message;
488+
}
489+
490+
return { ids, byId };
488491
}
489492

490493
function buildActivitySlice(thread: Thread): {
491494
ids: string[];
492495
byId: Record<string, OrchestrationThreadActivity>;
493496
} {
494-
return {
495-
ids: thread.activities.map((activity) => activity.id),
496-
byId: Object.fromEntries(
497-
thread.activities.map((activity) => [activity.id, activity] as const),
498-
) as Record<string, OrchestrationThreadActivity>,
499-
};
497+
const ids: string[] = [];
498+
const byId: Record<string, OrchestrationThreadActivity> = {};
499+
500+
for (const activity of thread.activities) {
501+
ids.push(activity.id);
502+
byId[activity.id] = activity;
503+
}
504+
505+
return { ids, byId };
500506
}
501507

502508
function buildProposedPlanSlice(thread: Thread): {
503509
ids: string[];
504510
byId: Record<string, ProposedPlan>;
505511
} {
506-
return {
507-
ids: thread.proposedPlans.map((plan) => plan.id),
508-
byId: Object.fromEntries(
509-
thread.proposedPlans.map((plan) => [plan.id, plan] as const),
510-
) as Record<string, ProposedPlan>,
511-
};
512+
const ids: string[] = [];
513+
const byId: Record<string, ProposedPlan> = {};
514+
515+
for (const plan of thread.proposedPlans) {
516+
ids.push(plan.id);
517+
byId[plan.id] = plan;
518+
}
519+
520+
return { ids, byId };
512521
}
513522

514523
function buildTurnDiffSlice(thread: Thread): {
515524
ids: TurnId[];
516525
byId: Record<TurnId, TurnDiffSummary>;
517526
} {
518-
return {
519-
ids: thread.turnDiffSummaries.map((summary) => summary.turnId),
520-
byId: Object.fromEntries(
521-
thread.turnDiffSummaries.map((summary) => [summary.turnId, summary] as const),
522-
) as Record<TurnId, TurnDiffSummary>,
523-
};
527+
const ids: TurnId[] = [];
528+
const byId = {} as Record<TurnId, TurnDiffSummary>;
529+
530+
for (const summary of thread.turnDiffSummaries) {
531+
ids.push(summary.turnId);
532+
byId[summary.turnId] = summary;
533+
}
534+
535+
return { ids, byId };
524536
}
525537

526538
function getProjects(state: EnvironmentState): Project[] {
527-
return state.projectIds.flatMap((projectId) => {
539+
const projects: Project[] = [];
540+
541+
for (const projectId of state.projectIds) {
528542
const project = state.projectById[projectId];
529-
return project ? [project] : [];
530-
});
543+
if (project) {
544+
projects.push(project);
545+
}
546+
}
547+
548+
return projects;
531549
}
532550

533551
function getThreads(state: EnvironmentState): Thread[] {
534-
return state.threadIds.flatMap((threadId) => {
552+
const threads: Thread[] = [];
553+
554+
for (const threadId of state.threadIds) {
535555
const thread = getThreadFromEnvironmentState(state, threadId);
536-
return thread ? [thread] : [];
537-
});
556+
if (thread) {
557+
threads.push(thread);
558+
}
559+
}
560+
561+
return threads;
538562
}
539563

540564
/**

0 commit comments

Comments
 (0)