Skip to content

Commit

Permalink
make TargetProfile.Equals and CompareTo faster by caching the string …
Browse files Browse the repository at this point in the history
…representation.
  • Loading branch information
matthid committed Oct 1, 2017
1 parent 750a2f9 commit cdf2982
Show file tree
Hide file tree
Showing 24 changed files with 364 additions and 338 deletions.
4 changes: 2 additions & 2 deletions src/Paket.Core/Installation/RestoreProcess.fs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ let createProjectReferencesFiles (lockFile:LockFile) (projectFile:ProjectFile) (
newFileName.Directory.Create()

elif not newFileName.Exists || File.ReadAllText(newFileName.FullName) <> output then
if not (File.Exists(oldReferencesFile.FullName)) || targetProfile = SinglePlatform (FrameworkIdentifier.DotNetStandard DotNetStandardVersion.V1_6) then
if not (File.Exists(oldReferencesFile.FullName)) || targetProfile = TargetProfile.SinglePlatform (FrameworkIdentifier.DotNetStandard DotNetStandardVersion.V1_6) then
// compat with old targets and fable - always write but prefer netstandard16.
File.WriteAllText(oldReferencesFile.FullName,output)
File.WriteAllText(newFileName.FullName,output)
Expand Down Expand Up @@ -492,7 +492,7 @@ let Restore(dependenciesFileName,projectFile,force,group,referencesFileNames,ign
match resolvedPackage.Settings.FrameworkRestrictions with
| Requirements.ExplicitRestriction restrictions ->
targets
|> Array.exists (fun target -> Requirements.isTargetMatchingRestrictions(restrictions, SinglePlatform target))
|> Array.exists (fun target -> Requirements.isTargetMatchingRestrictions(restrictions, TargetProfile.SinglePlatform target))
| _ -> true)


Expand Down
2 changes: 1 addition & 1 deletion src/Paket.Core/PaketConfigFiles/DependenciesFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ type DependenciesFile(fileName,groups:Map<GroupName,DependenciesGroup>, textRepr
match restrictions with
| Paket.Requirements.AutoDetectFramework -> failwithf "couldn't detect framework"
| Paket.Requirements.ExplicitRestriction list ->
list.RepresentedFrameworks |> Seq.choose (function SinglePlatform tf -> Some tf | _ -> None)
list.RepresentedFrameworks |> Seq.choose (function TargetProfile.SinglePlatform tf -> Some tf | _ -> None)
)
|> Seq.concat
)
Expand Down
4 changes: 2 additions & 2 deletions src/Paket.Core/PaketConfigFiles/DependencyCache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type DependencyCache (dependencyFile:DependenciesFile, lockFile:LockFile) =
let getDllsWithinPackage (framework: FrameworkIdentifier) (installModel :InstallModel) =
let dllFiles =
installModel
|> InstallModel.getLegacyReferences (SinglePlatform framework)
|> InstallModel.getLegacyReferences (TargetProfile.SinglePlatform framework)
|> Seq.map (fun l -> l.Path)
|> Seq.map (fun path -> AssemblyDefinition.ReadAssembly path, FileInfo(path))
|> dict
Expand All @@ -113,7 +113,7 @@ type DependencyCache (dependencyFile:DependenciesFile, lockFile:LockFile) =
match tryGet (group,pack.Name) installModelCache with
| None -> ()
| Some model ->
model.GetLibReferenceFiles (SinglePlatform framework) |> Seq.iter (libs.Add >> ignore)
model.GetLibReferenceFiles (TargetProfile.SinglePlatform framework) |> Seq.iter (libs.Add >> ignore)
model.GetAllLegacyFrameworkReferences ()|> Seq.iter (sysLibs.Add >> ignore)
)

Expand Down
2 changes: 1 addition & 1 deletion src/Paket.Core/PaketConfigFiles/InstallModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type FrameworkFolder<'T> = {
} with
member this.GetSinglePlatforms() =
this.Targets
|> Seq.choose (function SinglePlatform t -> Some t | _ -> None)
|> Seq.choose (function TargetProfile.SinglePlatform t -> Some t | _ -> None)

module FrameworkFolder =
let map f (l:FrameworkFolder<_>) = {
Expand Down
12 changes: 6 additions & 6 deletions src/Paket.Core/PaketConfigFiles/ProjectFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,23 +1038,23 @@ module ProjectFile =
match getTargetFrameworkVersion project with
| None -> getTargetFramework project
| Some x -> Some(prefix + x.Replace("v",""))
let defaultResult = SinglePlatform (DotNetFramework FrameworkVersion.V4)
let defaultResult = TargetProfile.SinglePlatform (DotNetFramework FrameworkVersion.V4)
match framework with
| None -> defaultResult
| Some s ->
match FrameworkDetection.Extract(s) with
| None -> defaultResult
| Some x -> SinglePlatform x
| Some x -> TargetProfile.SinglePlatform x

match getTargetFrameworkProfile project with
| Some profile when profile = "Unity Web v3.5" ->
SinglePlatform (DotNetUnity DotNetUnityVersion.V3_5_Web)
TargetProfile.SinglePlatform (DotNetUnity DotNetUnityVersion.V3_5_Web)
| Some profile when profile = "Unity Micro v3.5" ->
SinglePlatform (DotNetUnity DotNetUnityVersion.V3_5_Micro)
TargetProfile.SinglePlatform (DotNetUnity DotNetUnityVersion.V3_5_Micro)
| Some profile when profile = "Unity Subset v3.5" ->
SinglePlatform (DotNetUnity DotNetUnityVersion.V3_5_Subset)
TargetProfile.SinglePlatform (DotNetUnity DotNetUnityVersion.V3_5_Subset)
| Some profile when profile = "Unity Full v3.5" ->
SinglePlatform (DotNetUnity DotNetUnityVersion.V3_5_Full)
TargetProfile.SinglePlatform (DotNetUnity DotNetUnityVersion.V3_5_Full)
| Some profile when String.IsNullOrWhiteSpace profile |> not ->
try
KnownTargetProfiles.FindPortableProfile profile
Expand Down
102 changes: 64 additions & 38 deletions src/Paket.Core/Versioning/FrameworkHandling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -948,17 +948,43 @@ type PortableProfileType =
x.Frameworks
|> List.sort
|> List.map (fun fw -> fw.ToString()))
type TargetProfile =
| SinglePlatform of FrameworkIdentifier
| PortableProfile of PortableProfileType
type TargetProfileRaw =
| SinglePlatformP of FrameworkIdentifier
| PortableProfileP of PortableProfileType
override this.ToString() =
match this with
| SinglePlatform x -> x.ToString()
| PortableProfile p -> p.FolderName
| SinglePlatformP x -> x.ToString()
| PortableProfileP p -> p.FolderName
member x.IsUnsupportedPortable =
match x with
| PortableProfile p -> p.IsUnsupprted
| PortableProfileP p -> p.IsUnsupprted
| _ -> false

[<CustomEquality; CustomComparison>]
type TargetProfile =
{ RawTargetProfile : TargetProfileRaw; CompareString : string }
override x.ToString() = x.CompareString
member x.IsUnsupportedPortable = x.RawTargetProfile.IsUnsupportedPortable

override x.Equals(y) =
match y with
| :? TargetProfile as r -> x.CompareString.Equals(r.CompareString)
| _ -> false
override x.GetHashCode() = x.CompareString.GetHashCode()
interface System.IComparable with
member x.CompareTo(y) =
match y with
| :? TargetProfile as r -> x.CompareString.CompareTo(r.CompareString)
| _ -> failwith "wrong type"

module TargetProfile =
let (|SinglePlatform|PortableProfile|) profile =
match profile.RawTargetProfile with
| SinglePlatformP x -> SinglePlatform x
| PortableProfileP p -> PortableProfile p
let OfPlatform p = { RawTargetProfile = p; CompareString = p.ToString() }
let SinglePlatform s = OfPlatform (SinglePlatformP s)
let PortableProfile s = OfPlatform (PortableProfileP s)

module KnownTargetProfiles =
// These lists are used primarilty when calculating stuff which requires iterating over ALL profiles
Expand Down Expand Up @@ -993,7 +1019,7 @@ module KnownTargetProfiles =

let DotNetFrameworkProfiles =
DotNetFrameworkIdentifiers
|> List.map SinglePlatform
|> List.map TargetProfile.SinglePlatform

let DotNetStandardVersions = [
DotNetStandardVersion.V1_0
Expand All @@ -1008,7 +1034,7 @@ module KnownTargetProfiles =

let DotNetStandardProfiles =
DotNetStandardVersions
|> List.map (DotNetStandard >> SinglePlatform)
|> List.map (DotNetStandard >> TargetProfile.SinglePlatform)

let DotNetCoreAppVersions = [
DotNetCoreAppVersion.V1_0
Expand All @@ -1025,7 +1051,7 @@ module KnownTargetProfiles =

let DotNetCoreProfiles =
DotNetCoreAppVersions
|> List.map (DotNetCoreApp >> SinglePlatform)
|> List.map (DotNetCoreApp >> TargetProfile.SinglePlatform)

let WindowsVersions = [
WindowsVersion.V8
Expand All @@ -1035,11 +1061,11 @@ module KnownTargetProfiles =

let WindowsProfiles =
WindowsVersions
|> List.map (Windows >> SinglePlatform)
|> List.map (Windows >> TargetProfile.SinglePlatform)

let DotNetUnityProfiles =
DotNetUnityVersions
|> List.map (DotNetUnity >> SinglePlatform)
|> List.map (DotNetUnity >> TargetProfile.SinglePlatform)

let SilverlightVersions = [
SilverlightVersion.V3
Expand All @@ -1049,7 +1075,7 @@ module KnownTargetProfiles =

let SilverlightProfiles =
SilverlightVersions
|> List.map (Silverlight >> SinglePlatform)
|> List.map (Silverlight >> TargetProfile.SinglePlatform)

let MonoAndroidVersions = [
MonoAndroidVersion.V1
Expand All @@ -1070,7 +1096,7 @@ module KnownTargetProfiles =

let MonoAndroidProfiles =
MonoAndroidVersions
|> List.map (MonoAndroid >> SinglePlatform)
|> List.map (MonoAndroid >> TargetProfile.SinglePlatform)

let UAPVersons = [
UAPVersion.V10
Expand All @@ -1079,7 +1105,7 @@ module KnownTargetProfiles =

let UAPProfiles =
UAPVersons
|> List.map (UAP >> SinglePlatform)
|> List.map (UAP >> TargetProfile.SinglePlatform)

let WindowsPhoneVersions = [
WindowsPhoneVersion.V7
Expand All @@ -1091,15 +1117,15 @@ module KnownTargetProfiles =

let WindowsPhoneSilverlightProfiles =
WindowsPhoneVersions
|> List.map (WindowsPhone >> SinglePlatform)
|> List.map (WindowsPhone >> TargetProfile.SinglePlatform)

let WindowsPhoneAppVersions = [
WindowsPhoneAppVersion.V8_1
]

let WindowsPhoneAppProfiles =
WindowsPhoneAppVersions
|> List.map (WindowsPhoneApp >> SinglePlatform)
|> List.map (WindowsPhoneApp >> TargetProfile.SinglePlatform)

// http://nugettoolsdev.azurewebsites.net/4.0.0/parse-framework?framework=.NETPortable%2CVersion%3Dv0.0%2CProfile%3DProfile3
let AllPortableProfiles =
Expand Down Expand Up @@ -1157,12 +1183,12 @@ module KnownTargetProfiles =
SilverlightProfiles @
WindowsPhoneSilverlightProfiles @
MonoAndroidProfiles @
[SinglePlatform(MonoTouch)
SinglePlatform(XamariniOS)
SinglePlatform(XamarinMac)
SinglePlatform(XamarinTV)
SinglePlatform(XamarinWatch)] @
(AllPortableProfiles |> List.map PortableProfile)
[TargetProfile.SinglePlatform(MonoTouch)
TargetProfile.SinglePlatform(XamariniOS)
TargetProfile.SinglePlatform(XamarinMac)
TargetProfile.SinglePlatform(XamarinTV)
TargetProfile.SinglePlatform(XamarinWatch)] @
(AllPortableProfiles |> List.map TargetProfile.PortableProfile)

let AllDotNetStandardAndCoreProfiles =
DotNetStandardProfiles @
Expand All @@ -1185,7 +1211,7 @@ module KnownTargetProfiles =
Native(Release,Arm)]

let AllProfiles =
(AllNativeProfiles |> List.map SinglePlatform) @
(AllNativeProfiles |> List.map TargetProfile.SinglePlatform) @
AllDotNetStandardAndCoreProfiles @
AllDotNetProfiles
|> Set.ofList
Expand All @@ -1195,7 +1221,7 @@ module KnownTargetProfiles =
AllProfiles
|> Set.toSeq
|> Seq.tryPick (function
| PortableProfile p when p.ProfileName.ToLowerInvariant() = lowerName -> Some (PortableProfile p)
| TargetProfile.PortableProfile p when p.ProfileName.ToLowerInvariant() = lowerName -> Some (TargetProfile.PortableProfile p)
| _ -> None)
let FindPortableProfile name =
match TryFindPortableProfile name with
Expand Down Expand Up @@ -1223,7 +1249,7 @@ module SupportCalculation =
KnownTargetProfiles.AllPortableProfiles
|> List.filter (fun p -> p.ProfileName <> name)
|> List.filter (fun other -> isSupportedNotEqual portable other)
|> List.map PortableProfile
|> List.map TargetProfile.PortableProfile
type SupportMap = System.Collections.Concurrent.ConcurrentDictionary<PortableProfileType,PortableProfileType list>
let ofSeq s = s|> dict |> System.Collections.Concurrent.ConcurrentDictionary
let toSeq s = s|> Seq.map (fun (kv:System.Collections.Generic.KeyValuePair<_,_>) -> kv.Key, kv.Value)
Expand Down Expand Up @@ -1266,7 +1292,7 @@ module SupportCalculation =
sup
let private getSupportedPortables p =
getSupported p
|> List.choose (function PortableProfile p -> Some p | _ -> failwithf "Expected portable")
|> List.choose (function TargetProfile.PortableProfile p -> Some p | _ -> failwithf "Expected portable")

let createInitialSupportMap () =
KnownTargetProfiles.AllPortableProfiles
Expand Down Expand Up @@ -1295,7 +1321,7 @@ module SupportCalculation =
let private findPortablePriv =
memoize (fun (fws: _ list) ->
if fws.Length = 0 then failwithf "can not find portable for an empty list (Details: Empty lists need to be handled earlier with a warning)!"
let fallback = PortableProfile (UnsupportedProfile (fws |> List.sort))
let fallback = TargetProfile.PortableProfile (UnsupportedProfile (fws |> List.sort))
let minimal =
fws
|> List.filter (function
Expand All @@ -1320,7 +1346,7 @@ module SupportCalculation =
|> List.sortBy (fun p -> p.Frameworks.Length)
|> List.tryHead
match firstMatch with
| Some p -> PortableProfile p
| Some p -> TargetProfile.PortableProfile p
| None ->
fallback
else
Expand All @@ -1334,10 +1360,10 @@ module SupportCalculation =

let getSupportedPlatforms x =
match x with
| SinglePlatform tf ->
| TargetProfile.SinglePlatform tf ->
let rawSupported =
tf.RawSupportedPlatforms
|> List.map SinglePlatform
|> List.map TargetProfile.SinglePlatform
let profilesSupported =
// See https://docs.microsoft.com/en-us/dotnet/articles/standard/library
// NOTE: This is explicit in NuGet world (ie users explicitely need to add "imports")
Expand Down Expand Up @@ -1398,11 +1424,11 @@ module SupportCalculation =
p.Frameworks
|> List.exists (fun fw -> fw = tf))
profiles
|> List.map PortableProfile
|> List.map TargetProfile.PortableProfile
rawSupported @ profilesSupported
| PortableProfile p ->
| TargetProfile.PortableProfile p ->
getSupportedPreCalculated p
|> List.map PortableProfile
|> List.map TargetProfile.PortableProfile
|> Set.ofList

let getSupportedPlatformsTransitive =
Expand All @@ -1429,13 +1455,13 @@ module SupportCalculation =
/// true when x is supported by y, for example netstandard15 is supported by netcore10
let isSupportedBy x y =
match x with
| PortableProfile (PortableProfileType.UnsupportedProfile xs' as x') ->
| TargetProfile.PortableProfile (PortableProfileType.UnsupportedProfile xs' as x') ->
// custom profiles are not in our lists -> custom logic
match y with
| PortableProfile y' ->
| TargetProfile.PortableProfile y' ->
x' = y' ||
isSupportedNotEqual y' x'
| SinglePlatform y' ->
| TargetProfile.SinglePlatform y' ->
y'.RawSupportedPlatformsTransitive |> Seq.exists (fun y'' ->
xs' |> Seq.contains y'')
| _ ->
Expand All @@ -1452,8 +1478,8 @@ module SupportCalculation =
type TargetProfile with
member p.Frameworks =
match p with
| SinglePlatform fw -> [fw]
| PortableProfile p -> p.Frameworks
| TargetProfile.SinglePlatform fw -> [fw]
| TargetProfile.PortableProfile p -> p.Frameworks
static member FindPortable warnWhenUnsupported (fws: _ list) = SupportCalculation.findPortable warnWhenUnsupported fws

member inline x.PlatformsSupporting = SupportCalculation.getPlatformsSupporting x
Expand Down
Loading

0 comments on commit cdf2982

Please sign in to comment.