From 49de771fe808d4b3135b20b4ea8b3ebf1d31df5a Mon Sep 17 00:00:00 2001 From: npal Date: Mon, 5 Oct 2026 20:14:51 -0500 Subject: [PATCH] perf(vendors): look up existing technicians by id in O(1) The roster reconcile matched each incoming technician with a linear FirstOrDefault over the existing list, O(n*m). Index the existing list by id once and use TryGetValue. Co-Authored-By: Claude Sonnet 5 --- .../Implementation/VendorCompanyRosterDataService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SeaHaven.DataServices/Implementation/VendorCompanyRosterDataService.cs b/SeaHaven.DataServices/Implementation/VendorCompanyRosterDataService.cs index cac9637..4b66614 100644 --- a/SeaHaven.DataServices/Implementation/VendorCompanyRosterDataService.cs +++ b/SeaHaven.DataServices/Implementation/VendorCompanyRosterDataService.cs @@ -131,6 +131,7 @@ namespace SeaHaven.DataServices.Implementation var existingTechnicians = await _context.Vendors .Where(v => v.CompanyId == company.Id && (v.IsDeleted == null || v.IsDeleted == false)) .ToListAsync(cancellationToken); + var existingTechniciansById = existingTechnicians.ToDictionary(v => v.Id); var snapshotIds = roster.Technicians .Where(t => t.Id.HasValue) @@ -169,8 +170,7 @@ namespace SeaHaven.DataServices.Implementation { if (technician.Id.HasValue) { - var existing = existingTechnicians.FirstOrDefault(e => e.Id == technician.Id.Value); - if (existing == null) + if (!existingTechniciansById.TryGetValue(technician.Id.Value, out var existing)) continue; existing.ContactName = technician.ContactName;