perf(agent): cache ListAgentIDs() result before MCP tool registration loop

ListAgentIDs() was called on every iteration of the inner tool loop,
causing repeated allocations. Capture the slice once and reuse it for
both agentCount and the registration loop.
This commit is contained in:
yuchou87 2026-02-21 13:48:41 +08:00
parent 0f611790af
commit a0d66fa5cd

View file

@ -185,12 +185,13 @@ func (al *AgentLoop) Run(ctx context.Context) error {
servers := mcpManager.GetServers() servers := mcpManager.GetServers()
uniqueTools := 0 uniqueTools := 0
totalRegistrations := 0 totalRegistrations := 0
agentCount := len(al.registry.ListAgentIDs()) agentIDs := al.registry.ListAgentIDs()
agentCount := len(agentIDs)
for serverName, conn := range servers { for serverName, conn := range servers {
uniqueTools += len(conn.Tools) uniqueTools += len(conn.Tools)
for _, tool := range conn.Tools { for _, tool := range conn.Tools {
for _, agentID := range al.registry.ListAgentIDs() { for _, agentID := range agentIDs {
agent, ok := al.registry.GetAgent(agentID) agent, ok := al.registry.GetAgent(agentID)
if !ok { if !ok {
continue continue