Skip to content

Commit b6781d6

Browse files
committed
perf(codex): avoid repeated output patch writes
1 parent bb8408c commit b6781d6

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

internal/runtime/executor/codex_executor.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,43 @@ func patchCodexCompletedOutput(eventData []byte, outputItemsByIndex map[int64][]
5959
return eventData
6060
}
6161

62-
completedDataPatched := eventData
63-
completedDataPatched, _ = sjson.SetRawBytes(completedDataPatched, "response.output", []byte(`[]`))
64-
6562
indexes := make([]int64, 0, len(outputItemsByIndex))
6663
for idx := range outputItemsByIndex {
6764
indexes = append(indexes, idx)
6865
}
6966
sort.Slice(indexes, func(i, j int) bool {
7067
return indexes[i] < indexes[j]
7168
})
69+
70+
items := make([][]byte, 0, len(outputItemsByIndex)+len(outputItemsFallback))
7271
for _, idx := range indexes {
73-
completedDataPatched, _ = sjson.SetRawBytes(completedDataPatched, "response.output.-1", outputItemsByIndex[idx])
72+
items = append(items, outputItemsByIndex[idx])
7473
}
75-
for _, item := range outputItemsFallback {
76-
completedDataPatched, _ = sjson.SetRawBytes(completedDataPatched, "response.output.-1", item)
74+
items = append(items, outputItemsFallback...)
75+
76+
outputArray := []byte("[]")
77+
if len(items) > 0 {
78+
var buf bytes.Buffer
79+
totalLen := 2
80+
for _, item := range items {
81+
totalLen += len(item)
82+
}
83+
if len(items) > 1 {
84+
totalLen += len(items) - 1
85+
}
86+
buf.Grow(totalLen)
87+
buf.WriteByte('[')
88+
for i, item := range items {
89+
if i > 0 {
90+
buf.WriteByte(',')
91+
}
92+
buf.Write(item)
93+
}
94+
buf.WriteByte(']')
95+
outputArray = buf.Bytes()
7796
}
97+
98+
completedDataPatched, _ := sjson.SetRawBytes(eventData, "response.output", outputArray)
7899
return completedDataPatched
79100
}
80101

0 commit comments

Comments
 (0)