From c9499d839cd9a87850fa68df4126f86e4d9c6836 Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Mon, 21 Jul 2025 16:13:14 -0700 Subject: [PATCH] Improve performance of update comparison in cache lookup Replaces the `Compare-Object` cmdlet with a faster, manual loop comparison on pre-sorted arrays to optimize the VHDX cache check for matching updates. This change also refactors the extraction of update filenames for better readability and adds checks for empty collections to improve script robustness. --- FFUDevelopment/BuildFFUVM.ps1 | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index c9c6fa9..24ebe7f 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -4607,11 +4607,9 @@ try { # Extract file names from URLs for comparison $requiredUpdateFileNames = @() - foreach ($update in $requiredUpdates) { - $fileName = ($update.Url -split '/')[-1] - $requiredUpdateFileNames += $fileName + if ($requiredUpdates.Count -gt 0) { + $requiredUpdateFileNames = @(($requiredUpdates.Url | ForEach-Object { ($_ -split '/')[-1] }) | Sort-Object) } - $requiredUpdateFileNames = $requiredUpdateFileNames | Sort-Object foreach ($vhdxJson in $vhdxJsons) { try { @@ -4625,11 +4623,23 @@ try { if ($vhdxCacheItem.OptionalFeatures -ne $OptionalFeatures) { WriteLog 'OptionalFeatures mismatch, continuing'; continue } $cachedUpdateNames = @() - if ($vhdxCacheItem.IncludedUpdates) { - $cachedUpdateNames = $vhdxCacheItem.IncludedUpdates.Name | Sort-Object + if ($vhdxCacheItem.IncludedUpdates -and $vhdxCacheItem.IncludedUpdates.Count -gt 0) { + $cachedUpdateNames = @($vhdxCacheItem.IncludedUpdates.Name | Sort-Object) } - if ((Compare-Object -ReferenceObject $requiredUpdateFileNames -DifferenceObject $cachedUpdateNames).Length -gt 0) { + # Manually compare the two sorted arrays of update names + $updatesMatch = $false + if ($requiredUpdateFileNames.Count -eq $cachedUpdateNames.Count) { + $updatesMatch = $true # Assume true and prove false + for ($i = 0; $i -lt $requiredUpdateFileNames.Count; $i++) { + if ($requiredUpdateFileNames[$i] -ne $cachedUpdateNames[$i]) { + $updatesMatch = $false + break + } + } + } + + if (-not $updatesMatch) { WriteLog 'IncludedUpdates mismatch, continuing' continue }