Skip to content

Commit 3d3ba00

Browse files
authored
Merge pull request #42 from matdev83/jules-9246238034975807275-24d1cd48
⚡ perf: preallocate slice capacity in anthropic messages loops
2 parents e90b57f + 054cb0e commit 3d3ba00

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

internal/plugins/backends/protocols/anthropicmessages/invoke.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func assistantMessageParam(m lipapi.Message) (anthropic.MessageParam, error) {
184184
}
185185

186186
func userPartsToBlocks(parts []lipapi.Part) ([]anthropic.ContentBlockParamUnion, error) {
187-
var out []anthropic.ContentBlockParamUnion
187+
out := make([]anthropic.ContentBlockParamUnion, 0, len(parts))
188188
for _, p := range parts {
189189
switch p.Kind {
190190
case lipapi.PartText:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package anthropicmessages
2+
3+
import (
4+
"testing"
5+
6+
"github.com/matdev83/go-llm-interactive-proxy/pkg/lipapi"
7+
)
8+
9+
func BenchmarkUserPartsToBlocks(b *testing.B) {
10+
parts := make([]lipapi.Part, 100)
11+
for i := range 100 {
12+
parts[i] = lipapi.Part{
13+
Kind: lipapi.PartText,
14+
Text: "Hello, world!",
15+
}
16+
}
17+
18+
b.ResetTimer()
19+
for i := 0; i < b.N; i++ {
20+
_, _ = userPartsToBlocks(parts)
21+
}
22+
}

0 commit comments

Comments
 (0)