From 2e81766c92f9a0cd811096e9b4bbd684257135fc Mon Sep 17 00:00:00 2001 From: Folyd Date: Mon, 8 Jun 2026 15:55:42 +0000 Subject: [PATCH] perf(codex): preallocate results and skip empty index sort Apply review feedback on codexExtractImageResults: preallocate the results slice to its known maximum capacity to avoid growth reallocations, and guard the itemsByIndex index-build/sort with a length check so no empty slice is allocated or sorted when only the fallback items are present. --- .../runtime/executor/codex_openai_images.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/runtime/executor/codex_openai_images.go b/internal/runtime/executor/codex_openai_images.go index 2f3f7ced..f0cd217b 100644 --- a/internal/runtime/executor/codex_openai_images.go +++ b/internal/runtime/executor/codex_openai_images.go @@ -623,19 +623,23 @@ func codexExtractImageResults(completed []byte, itemsByIndex map[int64][]byte, f } if len(outputItems) > 0 { // Completed event already carries the output; extract from it in place. + results = make([]codexImageCallResult, 0, len(outputItems)) for _, item := range outputItems { appendItem(item) } } else if len(itemsByIndex) > 0 || len(fallback) > 0 { // Completed output was empty; extract directly from the collected items, // preserving their original output_index ordering. - indexes := make([]int64, 0, len(itemsByIndex)) - for idx := range itemsByIndex { - indexes = append(indexes, idx) - } - sort.Slice(indexes, func(i, j int) bool { return indexes[i] < indexes[j] }) - for _, idx := range indexes { - appendItem(gjson.ParseBytes(itemsByIndex[idx])) + results = make([]codexImageCallResult, 0, len(itemsByIndex)+len(fallback)) + if len(itemsByIndex) > 0 { + indexes := make([]int64, 0, len(itemsByIndex)) + for idx := range itemsByIndex { + indexes = append(indexes, idx) + } + sort.Slice(indexes, func(i, j int) bool { return indexes[i] < indexes[j] }) + for _, idx := range indexes { + appendItem(gjson.ParseBytes(itemsByIndex[idx])) + } } for _, raw := range fallback { appendItem(gjson.ParseBytes(raw))