Skip to content

Commit d45f0e1

Browse files
Return pointers from Materialize to avoid struct copies
Change Materialize() to return []*ServerTool instead of []ServerTool. This avoids copying the entire ServerTool struct (which includes the JSON schema) for each tool in the result. Benchmark improvement: - Materialize: 6199ns → 1114ns (5.6x faster) - Memory: 12584B → 472B (27x less) - Allocations: 4 → 3 The returned pointers reference the cached tools in the index. Callers must not modify them.
1 parent 2556d72 commit d45f0e1

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

pkg/inventory/tool_index.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,15 @@ func (idx *ToolIndex) GetToolByName(name string) (*ServerTool, int, bool) {
216216

217217
// Materialize converts a QueryResult into actual tools, running dynamic checks as needed.
218218
// Only tools in NeedsDynamicCheck have their Enabled() function called.
219-
func (idx *ToolIndex) Materialize(ctx context.Context, qr QueryResult) []ServerTool {
219+
// Returns pointers to the cached tools - callers should NOT modify them.
220+
func (idx *ToolIndex) Materialize(ctx context.Context, qr QueryResult) []*ServerTool {
220221
// Pre-allocate with capacity = guaranteed + potential dynamic
221222
capacity := qr.Guaranteed.PopCount() + qr.NeedsDynamicCheck.PopCount()
222-
result := make([]ServerTool, 0, capacity)
223+
result := make([]*ServerTool, 0, capacity)
223224

224225
// Add all guaranteed tools (no dynamic check needed)
225226
qr.Guaranteed.Iterate(func(pos int) bool {
226-
result = append(result, idx.tools[pos])
227+
result = append(result, &idx.tools[pos])
227228
return true
228229
})
229230

@@ -236,7 +237,7 @@ func (idx *ToolIndex) Materialize(ctx context.Context, qr QueryResult) []ServerT
236237
return true // skip this tool, continue iteration
237238
}
238239
}
239-
result = append(result, *tool)
240+
result = append(result, tool)
240241
return true
241242
})
242243

0 commit comments

Comments
 (0)