Skip to content

Commit 6b8d5c9

Browse files
committed
Additional collection optimizations
1 parent c3cef1b commit 6b8d5c9

File tree

3 files changed

+87
-80
lines changed

3 files changed

+87
-80
lines changed

src/FSharp.Formatting.ApiDocs/GenerateModel.fs

Lines changed: 77 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,9 @@ module internal TypeFormatter =
12821282
n
12831283

12841284
curriedArgs
1285-
|> List.map (List.map (fun x -> formatArgNameAndType (counter ()) x |> fst))
1286-
|> List.map (fun argTuple ->
1285+
|> List.map (fun args ->
1286+
let argTuple = args |> List.map (formatArgNameAndType (counter ()) >> fst)
1287+
12871288
match argTuple with
12881289
| [] -> !! "()"
12891290
| [ argName ] when argName = "()" -> !! "()"
@@ -2217,10 +2218,9 @@ module internal SymbolReader =
22172218
}
22182219

22192220
/// Returns whether the link is not included in the document defined links
2220-
let linkNotDefined (doc: LiterateDocument) (link: string) =
2221+
let linkDefined (doc: LiterateDocument) (link: string) =
22212222
[ link; link.Replace("\r\n", ""); link.Replace("\r\n", " "); link.Replace("\n", ""); link.Replace("\n", " ") ]
2222-
|> Seq.map (fun key -> not (doc.DefinedLinks.ContainsKey(key)))
2223-
|> Seq.reduce (fun a c -> a && c)
2223+
|> List.exists (fun key -> doc.DefinedLinks.ContainsKey(key))
22242224

22252225
/// Returns a tuple of the undefined link and its Cref if it exists
22262226
let getTypeLink (ctx: ReadingContext) undefinedLink =
@@ -2265,8 +2265,10 @@ module internal SymbolReader =
22652265
replacedParagraphs
22662266
|> Seq.collect collectParagraphIndirectLinks
22672267
|> Seq.choose (fun line ->
2268-
let linkIsDefined = linkNotDefined doc line |> not
2269-
if linkIsDefined then None else getTypeLink ctx line |> Some)
2268+
if linkDefined doc line then
2269+
None
2270+
else
2271+
getTypeLink ctx line |> Some)
22702272
|> Seq.iter (addLinkToType doc)
22712273

22722274
doc.With(paragraphs = replacedParagraphs)
@@ -2276,15 +2278,12 @@ module internal SymbolReader =
22762278

22772279
let text =
22782280
lines
2279-
|> List.filter (
2280-
findCommand
2281-
>> (function
2281+
|> List.choose (fun line ->
2282+
match findCommand line with
22822283
| Some(k, v) ->
22832284
cmds.[k] <- v
2284-
false
2285-
| _ -> true)
2286-
)
2287-
|> List.map fst
2285+
None
2286+
| _ -> fst line |> Some)
22882287
|> String.concat "\n"
22892288

22902289
let doc =
@@ -2422,8 +2421,7 @@ module internal SymbolReader =
24222421

24232422
let readChildren ctx (entities: FSharpEntity seq) reader cond =
24242423
entities
2425-
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility)
2426-
|> Seq.filter cond
2424+
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility && cond v)
24272425
|> Seq.sortBy (fun (c: FSharpEntity) -> c.DisplayName)
24282426
|> Seq.choose (reader ctx)
24292427
|> List.ofSeq
@@ -2449,23 +2447,28 @@ module internal SymbolReader =
24492447

24502448
let readAllMembers ctx entityUrl kind (members: FSharpMemberOrFunctionOrValue seq) =
24512449
members
2452-
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility)
2453-
|> Seq.filter (fun v ->
2454-
not v.IsCompilerGenerated
2455-
&& not v.IsPropertyGetterMethod
2456-
&& not v.IsPropertySetterMethod
2457-
&& not v.IsEventAddMethod
2458-
&& not v.IsEventRemoveMethod)
2459-
|> Seq.choose (tryReadMember ctx entityUrl kind)
2450+
|> Seq.choose (fun v ->
2451+
if
2452+
checkAccess ctx v.Accessibility
2453+
&& not v.IsCompilerGenerated
2454+
&& not v.IsPropertyGetterMethod
2455+
&& not v.IsPropertySetterMethod
2456+
&& not v.IsEventAddMethod
2457+
&& not v.IsEventRemoveMethod
2458+
then
2459+
tryReadMember ctx entityUrl kind v
2460+
else
2461+
None)
24602462
|> List.ofSeq
24612463
|> collectNamespaceDocs
24622464

24632465
let readMembers ctx entityUrl kind (entity: FSharpEntity) cond =
24642466
entity.MembersFunctionsAndValues
2465-
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility)
2466-
|> Seq.filter (fun v -> not v.IsCompilerGenerated)
2467-
|> Seq.filter cond
2468-
|> Seq.choose (tryReadMember ctx entityUrl kind)
2467+
|> Seq.choose (fun v ->
2468+
if checkAccess ctx v.Accessibility && not v.IsCompilerGenerated && cond v then
2469+
tryReadMember ctx entityUrl kind v
2470+
else
2471+
None)
24692472
|> List.ofSeq
24702473
|> collectNamespaceDocs
24712474

@@ -2486,47 +2489,51 @@ module internal SymbolReader =
24862489
let readUnionCases ctx entityUrl (typ: FSharpEntity) =
24872490
typ.UnionCases
24882491
|> List.ofSeq
2489-
|> List.filter (fun v -> checkAccess ctx v.Accessibility)
24902492
|> List.choose (fun case ->
2491-
readCommentsInto case ctx case.XmlDocSig (fun cat catidx exclude _ comment ->
2492-
let details = readUnionCase ctx typ case
2493-
2494-
ApiDocMember(
2495-
case.Name,
2496-
readAttributes case.Attributes,
2497-
entityUrl,
2498-
ApiDocMemberKind.UnionCase,
2499-
cat,
2500-
catidx,
2501-
exclude,
2502-
details,
2503-
comment,
2504-
case,
2505-
ctx.WarnOnMissingDocs
2506-
)))
2493+
if checkAccess ctx case.Accessibility |> not then
2494+
None
2495+
else
2496+
readCommentsInto case ctx case.XmlDocSig (fun cat catidx exclude _ comment ->
2497+
let details = readUnionCase ctx typ case
2498+
2499+
ApiDocMember(
2500+
case.Name,
2501+
readAttributes case.Attributes,
2502+
entityUrl,
2503+
ApiDocMemberKind.UnionCase,
2504+
cat,
2505+
catidx,
2506+
exclude,
2507+
details,
2508+
comment,
2509+
case,
2510+
ctx.WarnOnMissingDocs
2511+
)))
25072512
|> collectNamespaceDocs
25082513

25092514
let readRecordFields ctx entityUrl (typ: FSharpEntity) =
25102515
typ.FSharpFields
25112516
|> List.ofSeq
2512-
|> List.filter (fun field -> not field.IsCompilerGenerated)
25132517
|> List.choose (fun field ->
2514-
readCommentsInto field ctx field.XmlDocSig (fun cat catidx exclude _ comment ->
2515-
let details = readFSharpField ctx field
2516-
2517-
ApiDocMember(
2518-
field.Name,
2519-
readAttributes (Seq.append field.FieldAttributes field.PropertyAttributes),
2520-
entityUrl,
2521-
ApiDocMemberKind.RecordField,
2522-
cat,
2523-
catidx,
2524-
exclude,
2525-
details,
2526-
comment,
2527-
field,
2528-
ctx.WarnOnMissingDocs
2529-
)))
2518+
if field.IsCompilerGenerated then
2519+
None
2520+
else
2521+
readCommentsInto field ctx field.XmlDocSig (fun cat catidx exclude _ comment ->
2522+
let details = readFSharpField ctx field
2523+
2524+
ApiDocMember(
2525+
field.Name,
2526+
readAttributes (Seq.append field.FieldAttributes field.PropertyAttributes),
2527+
entityUrl,
2528+
ApiDocMemberKind.RecordField,
2529+
cat,
2530+
catidx,
2531+
exclude,
2532+
details,
2533+
comment,
2534+
field,
2535+
ctx.WarnOnMissingDocs
2536+
)))
25302537
|> collectNamespaceDocs
25312538

25322539
let readStaticParams ctx entityUrl (typ: FSharpEntity) =
@@ -2586,11 +2593,12 @@ module internal SymbolReader =
25862593
if isNull nameAttr then
25872594
None
25882595
else
2589-
Some(nameAttr.Value, p.Value))
2590-
|> Seq.iter (fun (name, xmlDoc) ->
2591-
let xmlDocSig = getFSharpStaticParamXmlSig typ name
2596+
let xmlDocSig = getFSharpStaticParamXmlSig typ nameAttr.Value
25922597

2593-
registerXmlDoc ctx xmlDocSig (Security.SecurityElement.Escape xmlDoc) |> ignore)
2598+
registerXmlDoc ctx xmlDocSig (Security.SecurityElement.Escape p.Value)
2599+
|> ignore
2600+
|> Some)
2601+
|> ignore
25942602

25952603
let rec readType (ctx: ReadingContext) (typ: FSharpEntity) =
25962604
if typ.IsProvided && typ.XmlDoc <> FSharpXmlDoc.None then
@@ -2618,17 +2626,15 @@ module internal SymbolReader =
26182626

26192627
let ivals, svals =
26202628
getMembers typ
2621-
|> List.ofSeq
2622-
|> List.filter (fun v ->
2629+
|> Seq.filter (fun v ->
26232630
checkAccess ctx v.Accessibility
26242631
&& not v.IsCompilerGenerated
2625-
&& not v.IsOverrideOrExplicitInterfaceImplementation)
2626-
|> List.filter (fun v ->
2627-
not v.IsCompilerGenerated
2632+
&& not v.IsOverrideOrExplicitInterfaceImplementation
26282633
&& not v.IsEventAddMethod
26292634
&& not v.IsEventRemoveMethod
26302635
&& not v.IsPropertyGetterMethod
26312636
&& not v.IsPropertySetterMethod)
2637+
|> List.ofSeq
26322638
|> List.partition (fun v -> v.IsInstanceMember)
26332639

26342640
let cvals, svals = svals |> List.partition (fun v -> v.CompiledName = ".ctor")

src/FSharp.Formatting.Common/YaafFSharpScripting.fs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ module internal CompilerServiceExtensions =
8383

8484
options.OtherOptions
8585
|> Array.filter (fun path -> path.StartsWith("-r:", StringComparison.Ordinal))
86-
|> Array.filter (fun path -> path.StartsWith("-r:", StringComparison.Ordinal))
8786
//|> Seq.choose (fun path -> if path.StartsWith "-r:" then path.Substring 3 |> Some else None)
8887
//|> Seq.map (fun path -> path.Replace("\\\\", "\\"))
8988
|> Array.toList)
@@ -180,11 +179,11 @@ module internal CompilerServiceExtensions =
180179
|| libDirs
181180
|> List.exists (fun lib ->
182181
Directory.EnumerateFiles(lib)
183-
|> Seq.filter (fun file -> Path.GetExtension file =? ".dll")
184182
|> Seq.filter (fun file ->
185183
// If we find a FSharp.Core in a lib path, we check if is suited for us...
186-
Path.GetFileNameWithoutExtension file <>? "FSharp.Core"
187-
|| (tryCheckFsCore file |> Option.isSome))
184+
Path.GetExtension file =? ".dll"
185+
&& (Path.GetFileNameWithoutExtension file <>? "FSharp.Core"
186+
|| (tryCheckFsCore file |> Option.isSome)))
188187
|> Seq.toList
189188
|> isAssembly asm)
190189

@@ -279,10 +278,9 @@ module internal CompilerServiceExtensions =
279278

280279
let findReferences libDir =
281280
Directory.EnumerateFiles(libDir, "*.dll")
282-
|> Seq.map Path.GetFullPath
283281
// Filter files already referenced directly
284282
|> Seq.filter (fun file ->
285-
let fileName = Path.GetFileName file
283+
let fileName = Path.GetFullPath file |> Path.GetFileName
286284

287285
dllFiles
288286
|> List.exists (fun (dllFile: string) -> Path.GetFileName dllFile =? fileName)

src/fsdocs-tool/BuildCommand.fs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ type internal DocContent
9595
(subFolderFullPath = rootOutputFolderFullPath)
9696

9797
let allCultures =
98-
System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
99-
|> Array.map (fun x -> x.TwoLetterISOLanguageName)
100-
|> Array.filter (fun x -> x.Length = 2)
98+
CultureInfo.GetCultures(CultureTypes.AllCultures)
99+
|> Array.choose (fun x ->
100+
if x.TwoLetterISOLanguageName.Length <> 2 then
101+
None
102+
else
103+
Some x.TwoLetterISOLanguageName)
101104
|> Array.distinct
102105

103106
let makeMarkdownLinkResolver

0 commit comments

Comments
 (0)