From 39e23a927fe33b9d705b7e29370d5ad39ae9a202 Mon Sep 17 00:00:00 2001 From: Lukas Rieger Date: Sun, 30 Apr 2017 06:19:53 +0200 Subject: [PATCH] make findMatchingStep faster and slightly more readable --- src/Paket.Core/Common/Utils.fs | 14 ++++++++++++ .../Dependencies/PackageResolver.fs | 22 ++++++------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/Paket.Core/Common/Utils.fs b/src/Paket.Core/Common/Utils.fs index 4e34b49039..e27bd5d007 100644 --- a/src/Paket.Core/Common/Utils.fs +++ b/src/Paket.Core/Common/Utils.fs @@ -999,3 +999,17 @@ module Seq = ) |> fun (xs,ys) -> List.rev xs :> seq<_>, List.rev ys :> seq<_> +[] +module List = + // try to find an element in a list. + // If found, return the element and the list WITHOUT the element. + // If not found, return None. + let tryExtractOne fn values = + match List.tryFindIndex fn values with + | Some i -> + let rest = [ + for i2 in 0 .. values.Length - 1 do + if i <> i2 then yield values.[i2] + ] + Some values.[i], rest + | None -> None, values diff --git a/src/Paket.Core/Dependencies/PackageResolver.fs b/src/Paket.Core/Dependencies/PackageResolver.fs index 9ba098ff7b..53cd026904 100644 --- a/src/Paket.Core/Dependencies/PackageResolver.fs +++ b/src/Paket.Core/Dependencies/PackageResolver.fs @@ -653,8 +653,8 @@ let Resolve (getVersionsF, getPackageDetailsF, groupName:GroupName, globalStrate let inline fuseConflicts currentConflict priorConflictSteps conflicts = let findMatchingStep priorConflictSteps = - priorConflictSteps // row - |> Seq.tryFindIndex (fun (_,_,lastRequirement:PackageRequirement,_,_) -> + priorConflictSteps + |> List.tryExtractOne (fun (_,_,lastRequirement:PackageRequirement,_,_) -> let currentNames = conflicts |> Seq.collect (fun c -> let graphNameList = @@ -662,24 +662,16 @@ let Resolve (getVersionsF, getPackageDetailsF, groupName:GroupName, globalStrate c.Name :: graphNameList) |> Seq.toArray currentNames |> Array.contains lastRequirement.Name) - |> Option.map (fun i -> - let row = priorConflictSteps |> List.item i - let priorExceptRow = - priorConflictSteps - |> List.mapi (fun i2 v -> i2,v) - |> List.filter (fun (i2, _) -> i2 <> i) - |> List.map snd - row, priorExceptRow) - - match priorConflictSteps, findMatchingStep priorConflictSteps with - | [], _ -> currentConflict - | _, Some (head, priorConflictSteps) -> + + match findMatchingStep priorConflictSteps with + | None, [] -> currentConflict + | (Some head), priorConflictSteps -> let (lastConflict, lastStep, lastRequirement, lastCompatibleVersions, lastFlags) = head let continueConflict = { currentConflict with VersionsToExplore = lastConflict.VersionsToExplore } step (Inner((continueConflict,lastStep,lastRequirement), priorConflictSteps)) stackpack lastCompatibleVersions lastFlags // could not find a specific package - go back one step - | head :: priorConflictSteps, None -> + | None, head :: priorConflictSteps -> let (lastConflict, lastStep, lastRequirement, lastCompatibleVersions, lastFlags) = head let continueConflict = { currentConflict with VersionsToExplore = lastConflict.VersionsToExplore }