Skip to content

Commit

Permalink
Get rid of %A to have better perf
Browse files Browse the repository at this point in the history
  • Loading branch information
matthid committed Apr 29, 2018
1 parent b291c7a commit ae3752c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 46 deletions.
7 changes: 7 additions & 0 deletions src/app/Fake.Core.CommandLineParsing/docopt.fs/Docopt/Args.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ type DocoptResult =
| Flags of int
| Argument of string
| Arguments of string list
override x.ToString() =
match x with
| NoResult -> "NoResult"
| Flag -> "Flag"
| Flags f -> sprintf "Flags(%d)" f
| Argument arg -> sprintf "Argument(%s)" arg
| Arguments args -> sprintf "Arguments([%s])" (System.String.Join(";", args :> _ seq))

type DocoptMap = Map<string, DocoptResult>

Expand Down
13 changes: 11 additions & 2 deletions src/app/Fake.Core.CommandLineParsing/docopt.fs/Docopt/Options.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ type SafeOption =
member xx.IsShort = Option.isSome xx.Short
member xx.IsLong = Option.isSome xx.Long
override xx.ToString() =
sprintf "Option { Short=%A; Long=%A; ArgName=%A; Default=%A }"
xx.Short xx.Long xx.ArgumentName xx.DefaultValue
let inline printCOpt c =
match c with
| Some c -> sprintf "'%c'" c
| None -> "<none>"
let inline printSOpt s =
match s with
| Some s -> sprintf "\"%s\"" s
| None -> "<none>"

sprintf "Option { Short=%s; Long=%s; ArgName=%s; Default=%s }"
(printCOpt xx.Short) (printSOpt xx.Long) (printSOpt xx.ArgumentName) (printSOpt xx.DefaultValue)

type SafeOptions(list:SafeOption list) =
let findIn (l':string) (list:SafeOption list) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ exception UsageException of string
with override x.ToString () = sprintf "UsageException: %s" x.Data0

module private Helpers =
begin
let raiseArgvException errlist' =
let pos = Position(null, 0L, 0L, 0L) in
let perror = ParserError(pos, null, errlist') in
Expand All @@ -33,7 +32,18 @@ module private Helpers =
let raiseUnexpectedShort s' = raiseInternal (unexpectedShort s')
let raiseUnexpectedLong l' = raiseInternal (unexpectedLong l')
let raiseUnexpectedArg a' = raiseInternal (unexpectedArg a')
end
let inline printSOption s =
match s with
| None -> ""
| Some s -> s

let inline printReplyStatus (r:ReplyStatus) =
match r with
| ReplyStatus.Ok -> "Ok"
| ReplyStatus.Error -> "Error"
| ReplyStatus.FatalError -> "FatalError"
| _ -> sprintf "%d" (int r)


open Helpers

Expand All @@ -47,6 +57,10 @@ type ArgumentStreamPosition =
| ArgumentPos of int
// For short options like -adf we iterate over every letter
| ShortArgumentPartialPos of int * int
override x.ToString () =
match x with
| ArgumentPos p -> sprintf "ArgumentPos(%d)" p
| ShortArgumentPartialPos (p, part) -> sprintf "ArgumentPos(%d, %d)" p part
member x.ArgIndex =
match x with
| ShortArgumentPartialPos (p, _)
Expand Down Expand Up @@ -204,7 +218,7 @@ type ArgumentStream<'TUserState> private (argv:string array, initState:'TUserSta
static member Create(argv:string array, initState:'TUserState) =
new ArgumentStream<'TUserState>(argv, initState)
override x.ToString() =
sprintf "Pos: %A, %A, state: %A" pos argv state
sprintf "Pos: %O, [|%s|], state: %O" pos (System.String.Join(";", argv)) state

module ArgumentStream =
let create (argv:string array) (initState:'TUserState) =
Expand Down Expand Up @@ -364,7 +378,7 @@ module ArgParser =
reply <- elementParser stream
while reply.Status = Ok (*&& stateTag <> stream.StateTag*) do
if stateTag = stream.StateTag then
failwithf "infiniteLoopException %A" stream
failwithf "infiniteLoopException %O" stream
xs <- foldState xs reply.Result
error <- reply.Error
stateTag <- stream.StateTag
Expand Down Expand Up @@ -445,7 +459,7 @@ module ArgParser =
Reply(result)
| None ->
let e1 = expected itemType
let e2 = unexpected (sprintf "%A" (stream.PeekFull()))
let e2 = unexpected (sprintf "%s" (stream.PeekFull() |> printSOption))
let error = mergeErrors e1 e2
Reply(ReplyStatus.Error, error)

Expand All @@ -457,7 +471,7 @@ module ArgParser =
Reply(result)
| None ->
let e1 = expected itemType
let e2 = unexpected (sprintf "%A" (stream.PeekFull()))
let e2 = unexpected (sprintf "%s" (stream.PeekFull() |> printSOption))
let error = mergeErrors e1 e2
Reply(ReplyStatus.Error, error)

Expand Down Expand Up @@ -516,7 +530,7 @@ module ArgParser =
Map.add key (DocoptResult.Flags (n2 + 1)) map
| Some (DocoptResult.Flags n1), DocoptResult.Flags n2 ->
Map.add key (DocoptResult.Flags (n1 + n2)) map
| Some v, _ -> failwithf "Cannot add value %A as %s -> %A already exists in the result map" newItem key v
| Some v, _ -> failwithf "Cannot add value %O as %s -> %O already exists in the result map" newItem key v

let saveInMap key f =
updateUserState (fun item map ->
Expand All @@ -540,7 +554,7 @@ module ArgParser =


let pLongFlag (flag:SafeOption) =
if not flag.IsLong then failwithf "Cannot parse empty short flag %A" flag
if not flag.IsLong then failwithf "Cannot parse empty short flag %O" flag
let keys =
[ if flag.IsShort then yield flag.FullShort
if flag.IsLong then yield flag.FullLong ]
Expand All @@ -561,8 +575,9 @@ module ArgParser =
single
>>= saveInMapM keys (fun _ -> DocoptResult.Flag)


let pShortFlag (flag : SafeOption) =
if not flag.IsShort then failwithf "Cannot parse empty short flag %A" flag
if not flag.IsShort then failwithf "Cannot parse empty short flag %O" flag
let keys =
[ if flag.IsShort then yield flag.FullShort
if flag.IsLong then yield flag.FullLong ]
Expand All @@ -582,7 +597,7 @@ module ArgParser =
Reply(result)
| _ ->
let e1 = expected (sprintf "ShortFlag '%s'" flag.FullShort)
let e2 = unexpected (sprintf "%A" (stream.PeekFull()))
let e2 = unexpected (sprintf "%s" (stream.PeekFull() |> printSOption))
let error = mergeErrors e1 e2
Reply(ReplyStatus.Error, error)
//match arg with
Expand Down Expand Up @@ -843,12 +858,12 @@ type UsageParser(usageStrings':string array, sections:(string * SafeOptions) lis
sw.ToString()
match reply.Status = ReplyStatus.Ok, errors, state.IsEnd with
| true, [||], true -> reply.Result
| _, _ , true -> raise <| DocoptException (sprintf "errors %A: %s" reply.Status errorText)
| _, _ , true -> raise <| DocoptException (sprintf "errors %s: %s" (printReplyStatus reply.Status) errorText)
| _, [||], false ->
let unparsed = argv.[state.Position.ArgIndex..argv.Length - 1]
raise <| DocoptException (sprintf "'%A' could not be parsed" unparsed)
raise <| DocoptException (sprintf "'[|%s|]' could not be parsed" (System.String.Join(";", unparsed :> _ seq)))
| _ ->
let unparsed = argv.[state.Position.ArgIndex..argv.Length - 1]
raise <| DocoptException (sprintf "errors: %s, ('%A' could not be parsed)" errorText unparsed)
raise <| DocoptException (sprintf "errors: %s, ('[|%s|]' could not be parsed)" errorText (System.String.Join(";", unparsed :> _ seq)))

member __.Asts = asts
47 changes: 16 additions & 31 deletions src/app/Fake.Runtime/FakeRuntime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ let paketCachingProvider (script:string) (logLevel:Trace.VerboseLevel) cacheDir
let intellisenseFile = Path.Combine (cacheDir, "intellisense.fsx")
if logLevel.PrintVerbose then Trace.log <| sprintf "Writing '%s'" intellisenseFile
let groupScripts = Paket.LoadingScripts.ScriptGeneration.generateScriptContent context
let scripts, groupScript =
let _, groupScript =
match groupScripts with
| [] -> failwith "generateScriptContent returned []"
| [h] -> failwithf "generateScriptContent returned a single item: %A" h
Expand Down Expand Up @@ -266,30 +266,10 @@ let paketCachingProvider (script:string) (logLevel:Trace.VerboseLevel) cacheDir
match lockFile.Groups |> Map.tryFind groupName with
| Some g -> ()
| None -> failwithf "The group '%s' was not found in the lockfile. You might need to run 'paket install' first!" groupName.Name
//let (cache:DependencyCache) = DependencyCache(paketDependencies.GetDependenciesFile(), lockFile)

let (cache:DependencyCache) = DependencyCache(lockFile)
use dependencyCacheProfile = Fake.Profile.startCategory Fake.Profile.Category.PaketDependencyCache
if logLevel.PrintVerbose then Trace.log "Setup DependencyCache..."
try
cache.SetupGroup groupName |> ignore
with e when e.Message.Contains "doesn't exist. Did you restore" ->
let idx = e.Message.IndexOf(" doesn't exist. Did you restore")
let folder = e.Message.Substring("Folder ".Length, idx - "Folder ".Length)
let rec printFolder f =
if not (System.IO.Directory.Exists f) then
printfn "Dir '%s' doesn't exist" f
else
printfn "Dir '%s':" f
System.IO.Directory.EnumerateDirectories(f)
|> Seq.iter (fun dir -> printfn " -> %s" dir)
let parent = System.IO.Path.GetDirectoryName(f)
if not (isNull parent) then
printFolder parent

printFolder folder
reraise()
let orderedGroup = cache.OrderedGroups groupName // lockFile.GetGroup groupName
dependencyCacheProfile.Dispose()
//dependencyCacheProfile.Dispose()

let rid =
#if DOTNETCORE
Expand All @@ -300,14 +280,19 @@ let paketCachingProvider (script:string) (logLevel:Trace.VerboseLevel) cacheDir
Paket.Rid.Of(ridString)

// get runtime graph
use runtimeGraphProfile = Fake.Profile.startCategory Fake.Profile.Category.PaketRuntimeGraph
if logLevel.PrintVerbose then Trace.log <| sprintf "Calculating the runtime graph..."
let graph =
orderedGroup
|> Seq.choose (fun p ->
RuntimeGraph.getRuntimeGraphFromNugetCache cacheDir (Some PackagesFolderGroupConfig.NoPackagesFolder) groupName p.Resolved)
|> RuntimeGraph.mergeSeq
runtimeGraphProfile.Dispose()
async {
if logLevel.PrintVerbose then Trace.log <| sprintf "Calculating the runtime graph..."
use runtimeGraphProfile = Fake.Profile.startCategory Fake.Profile.Category.PaketRuntimeGraph
let result =
orderedGroup
|> Seq.choose (fun p ->
RuntimeGraph.getRuntimeGraphFromNugetCache cacheDir (Some PackagesFolderGroupConfig.NoPackagesFolder) groupName p.Resolved)
|> RuntimeGraph.mergeSeq
runtimeGraphProfile.Dispose()
return result
}
|> Async.StartAsTask

// Restore load-script
writeIntellisenseFile cacheDir {
Expand Down Expand Up @@ -340,7 +325,7 @@ let paketCachingProvider (script:string) (logLevel:Trace.VerboseLevel) cacheDir
|> Seq.map (fun fi -> true, FileInfo fi.Path)
|> Seq.toList
let runtimeAssemblies =
installModel.GetRuntimeAssemblies graph rid targetProfile
installModel.GetRuntimeAssemblies graph.Result rid targetProfile
|> Seq.map (fun fi -> false, FileInfo fi.Library.Path)
|> Seq.toList
runtimeAssemblies @ refAssemblies)
Expand Down

0 comments on commit ae3752c

Please sign in to comment.