Skip to content

Commit 71c95c7

Browse files
authored
Merge pull request #1511 from Thorium/less-seq-wraps
Small memory and performance optimization: Less nested seq-wrapping on recursive functions
2 parents 0daae3a + 820aa86 commit 71c95c7

File tree

4 files changed

+52
-41
lines changed

4 files changed

+52
-41
lines changed

src/FSharp.Data.Csv.Core/CsvRuntime.fs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,20 @@ module internal CsvReader =
6464

6565
/// Reads multiple lines from the input, skipping newline characters
6666
let rec readLines lineNumber =
67-
seq {
68-
match reader.Read() with
69-
| -1 -> ()
70-
| Char '\r'
71-
| Char '\n' -> yield! readLines lineNumber
72-
| current ->
67+
match reader.Read() with
68+
| -1 -> Seq.empty
69+
| Char '\r'
70+
| Char '\n' -> readLines lineNumber
71+
| current ->
72+
seq {
7373
yield
7474
readLine [] (StringBuilder()) current
7575
|> List.rev
7676
|> Array.ofList,
7777
lineNumber
7878

7979
yield! readLines (lineNumber + 1)
80-
}
80+
}
8181

8282
readLines 0
8383

src/FSharp.Data.Html.Core/HtmlParser.fs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@ open System.Collections.Generic
1414

1515
module private TextParser =
1616

17-
let toPattern f c = if f c then Some c else None
17+
let toPattern f c = if f c then ValueSome c else ValueNone
1818

19+
[<return: Struct>]
1920
let (|EndOfFile|_|) (c: char) =
2021
let value = c |> int
21-
if (value = -1 || value = 65535) then Some c else None
2222

23+
if (value = -1 || value = 65535) then
24+
ValueSome c
25+
else
26+
ValueNone
27+
28+
[<return: Struct>]
2329
let (|Whitespace|_|) = toPattern Char.IsWhiteSpace
30+
31+
[<return: Struct>]
2432
let (|LetterDigit|_|) = toPattern Char.IsLetterOrDigit
33+
34+
[<return: Struct>]
2535
let (|Letter|_|) = toPattern Char.IsLetter
2636

2737
// --------------------------------------------------------------------------------------

src/FSharp.Data.Runtime.Utilities/NameUtils.fs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,36 @@ let nicePascalName (s: string) =
4141
else
4242
// Starting to parse a new segment
4343
let rec restart i =
44-
seq {
45-
match tryAt s i with
46-
| EOF -> ()
47-
| LetterDigit _ & Upper _ -> yield! upperStart i (i + 1)
48-
| LetterDigit _ -> yield! consume i false (i + 1)
49-
| _ -> yield! restart (i + 1)
50-
}
44+
match tryAt s i with
45+
| EOF -> Seq.empty
46+
| LetterDigit _ & Upper _ -> upperStart i (i + 1)
47+
| LetterDigit _ -> consume i false (i + 1)
48+
| _ -> restart (i + 1)
5149
// Parsed first upper case letter, continue either all lower or all upper
5250
and upperStart from i =
53-
seq {
54-
match tryAt s i with
55-
| Upper _ -> yield! consume from true (i + 1)
56-
| Lower _ -> yield! consume from false (i + 1)
57-
| _ ->
58-
yield from, i
51+
match tryAt s i with
52+
| Upper _ -> consume from true (i + 1)
53+
| Lower _ -> consume from false (i + 1)
54+
| _ ->
55+
seq {
56+
yield struct (from, i)
5957
yield! restart (i + 1)
60-
}
58+
}
6159
// Consume are letters of the same kind (either all lower or all upper)
6260
and consume from takeUpper i =
63-
seq {
64-
match tryAt s i with
65-
| Lower _ when not takeUpper -> yield! consume from takeUpper (i + 1)
66-
| Upper _ when takeUpper -> yield! consume from takeUpper (i + 1)
67-
| Lower _ when takeUpper ->
68-
yield from, (i - 1)
61+
match takeUpper, tryAt s i with
62+
| false, Lower _ -> consume from takeUpper (i + 1)
63+
| true, Upper _ -> consume from takeUpper (i + 1)
64+
| true, Lower _ ->
65+
seq {
66+
yield struct (from, (i - 1))
6967
yield! restart (i - 1)
70-
| _ ->
71-
yield from, i
68+
}
69+
| _ ->
70+
seq {
71+
yield struct (from, i)
7272
yield! restart i
73-
}
73+
}
7474

7575
// Split string into segments and turn them to PascalCase
7676
seq {

src/FSharp.Data.Runtime.Utilities/TextConversions.fs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ module private Helpers =
4444

4545
let ParseISO8601FormattedDateTime text cultureInfo =
4646
match DateTime.TryParse(text, cultureInfo, dateTimeStyles) with
47-
| true, d -> d |> Some
48-
| false, _ -> None
47+
| true, d -> d |> ValueSome
48+
| false, _ -> ValueNone
4949

5050
// --------------------------------------------------------------------------------------
5151

@@ -155,8 +155,9 @@ type TextConversions private () =
155155
else
156156
// Parse ISO 8601 format, fixing time zone if needed
157157
match ParseISO8601FormattedDateTime text cultureInfo with
158-
| Some d when d.Kind = DateTimeKind.Unspecified -> new DateTime(d.Ticks, DateTimeKind.Local) |> Some
159-
| x -> x
158+
| ValueSome d when d.Kind = DateTimeKind.Unspecified -> new DateTime(d.Ticks, DateTimeKind.Local) |> Some
159+
| ValueSome x -> Some x
160+
| ValueNone -> None
160161

161162
static member AsDateTimeOffset cultureInfo (text: string) =
162163
// get TimeSpan presentation from 4-digit integers like 0000 or -0600
@@ -167,8 +168,8 @@ type TextConversions private () =
167168

168169
let offset str =
169170
match Int32.TryParse str with
170-
| true, v -> getTimeSpanFromHourMin v |> Some
171-
| false, _ -> None
171+
| true, v -> getTimeSpanFromHourMin v |> ValueSome
172+
| false, _ -> ValueNone
172173

173174
let matchesMS = msDateRegex.Value.Match(text.Trim())
174175

@@ -178,15 +179,15 @@ type TextConversions private () =
178179
// only if the timezone offset is specified with '-' or '+' prefix, after the millis
179180
// e.g. 1231456+1000, 123123+0000, 123123-0500, etc.
180181
match offset matchesMS.Groups.[2].Value with
181-
| Some ofst ->
182+
| ValueSome ofst ->
182183
matchesMS.Groups.[1].Value
183184
|> Double.Parse
184185
|> DateTimeOffset(1970, 1, 1, 0, 0, 0, ofst).AddMilliseconds
185186
|> Some
186-
| None -> None
187+
| ValueNone -> None
187188
else
188189
match ParseISO8601FormattedDateTime text cultureInfo with
189-
| Some d when d.Kind <> DateTimeKind.Unspecified ->
190+
| ValueSome d when d.Kind <> DateTimeKind.Unspecified ->
190191
match DateTimeOffset.TryParse(text, cultureInfo, dateTimeStyles) with
191192
| true, dto -> dto |> Some
192193
| false, _ -> None

0 commit comments

Comments
 (0)