perf: pre-allocate all known length arrays to avoid re-scaling (#5029)

Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
Ioannis Pinakoulakis
2025-08-08 19:03:25 +03:00
committed by GitHub
parent a2b98dbcf7
commit 130e1ba963
19 changed files with 29 additions and 28 deletions

View File

@@ -392,8 +392,8 @@ func (s *sseMcpServer) processListTools(ctx context.Context, client *mcpClient,
}
}
var toolsList []Tool
s.toolsLock.Lock()
toolsList := make([]Tool, 0, len(s.tools))
for _, tool := range s.tools {
if len(tool.InputSchema.Type) == 0 {
tool.InputSchema.Type = ContentTypeObject
@@ -437,8 +437,8 @@ func (s *sseMcpServer) processListPrompts(ctx context.Context, client *mcpClient
}
// Prepare prompt list
var promptsList []Prompt
s.promptsLock.Lock()
promptsList := make([]Prompt, 0, len(s.prompts))
for _, prompt := range s.prompts {
promptsList = append(promptsList, prompt)
}
@@ -475,8 +475,8 @@ func (s *sseMcpServer) processListResources(ctx context.Context, client *mcpClie
}
}
var resourcesList []Resource
s.resourcesLock.Lock()
resourcesList := make([]Resource, 0, len(s.resources))
for _, resource := range s.resources {
// Create a copy without the handler function which shouldn't be sent to clients
resourceCopy := Resource{