-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathSeqTests.fs
341 lines (278 loc) · 11.6 KB
/
SeqTests.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
module SeqTests
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Order
open BenchmarkDotNet.Mathematics
open BenchmarkDotNet.Configs
open System.Threading
open System
module sequenceResultMTests =
// original used in v4.16.0
module SeqCache =
let sequenceResultM (xs: seq<Result<'t, 'e>>) : Result<'t seq, 'e> =
let rec loop xs ts =
match Seq.tryHead xs with
| Some x ->
x
|> Result.bind (fun t -> loop (Seq.tail xs) (t :: ts))
| None ->
Ok(
List.rev ts
|> List.toSeq
)
// Seq.cache prevents double evaluation in Seq.tail
loop (Seq.cache xs) []
module SeqFold =
let traverseResultM' state (f: 'okInput -> Result<'okOutput, 'error>) xs =
let folder state x =
match state, f x with
| Error e, _ -> Error e
| Ok oks, Ok ok ->
Seq.singleton ok
|> Seq.append oks
|> Ok
| Ok _, Error e -> Error e
Seq.fold folder state xs
|> Result.map Seq.rev
let traverseResultM (f: 'okInput -> Result<'okOutput, 'error>) xs =
traverseResultM' (Ok Seq.empty) f xs
let sequenceResultM xs = traverseResultM id xs
module InlinedSeqFold =
let inline traverseResultM'
state
([<InlineIfLambda>] f: 'okInput -> Result<'okOutput, 'error>)
xs
=
let folder state x =
match state, f x with
| Error e, _ -> Error e
| Ok oks, Ok ok ->
Seq.singleton ok
|> Seq.append oks
|> Ok
| Ok _, Error e -> Error e
Seq.fold folder state xs
|> Result.map Seq.rev
let traverseResultM (f: 'okInput -> Result<'okOutput, 'error>) xs =
traverseResultM' (Ok Seq.empty) f xs
let sequenceResultM xs = traverseResultM id xs
module SeqUnfold =
let traverseResultM' initialState (f: 'okInput -> Result<'okOutput, 'error>) xs =
(initialState, 0)
|> Seq.unfold (fun (state, i) ->
xs
|> Seq.tryItem i
|> Option.bind (fun x ->
match state, f x with
| Error _, _ -> None
| Ok oks, Ok ok ->
let newState =
Seq.singleton ok
|> Seq.append oks
|> Ok
Some(newState, (newState, i + 1))
| Ok _, Error e -> Some(Error e, (Error e, i + 1))
)
)
|> Seq.last
let traverseResultM f xs = traverseResultM' (Ok Seq.empty) f xs
let sequenceResultM xs = traverseResultM id xs
module GetEnumerator =
let traverseResultM' state (f: 'okInput -> Result<'okOutput, 'error>) (xs: seq<'okInput>) =
let mutable state = state
let enumerator = xs.GetEnumerator()
while Result.isOk state
&& enumerator.MoveNext() do
match state, f enumerator.Current with
| Error _, _ -> ()
| Ok oks, Ok ok -> state <- Ok(Seq.append oks (Seq.singleton ok))
| Ok _, Error e -> state <- Error e
state
let traverseResultM f xs = traverseResultM' (Ok Seq.empty) f xs
let sequenceResultM xs = traverseResultM id xs
module InlinedGetEnumerator =
// adds an early exit upon encountering an error
let inline traverseResultM'
state
([<InlineIfLambda>] f: 'okInput -> Result<'okOutput, 'error>)
(xs: seq<'okInput>)
=
let mutable state = state
let enumerator = xs.GetEnumerator()
while Result.isOk state
&& enumerator.MoveNext() do
match state, f enumerator.Current with
| Error e, _ -> state <- Error e
| Ok oks, Ok ok ->
state <-
Seq.singleton ok
|> Seq.append oks
|> Ok
| Ok _, Error e -> state <- Error e
state
let traverseResultM f xs = traverseResultM' (Ok Seq.empty) f xs
let sequenceResultM xs = traverseResultM id xs
module SeqExpr =
let inline traverseResultM'
state
([<InlineIfLambda>] f: 'okInput -> Result<'okOutput, 'error>)
(xs: 'okInput seq)
=
match state with
| Error _ -> state
| Ok oks ->
use enumerator = xs.GetEnumerator()
let rec loop oks =
if enumerator.MoveNext() then
match f enumerator.Current with
| Ok ok ->
loop (
seq {
yield ok
yield! oks
}
)
| Error e -> Error e
else
Ok(Seq.rev oks)
loop oks
let traverseResultM f xs = traverseResultM' (Ok Seq.empty) f xs
let sequenceResultM xs = traverseResultM id xs
[<MemoryDiagnoser>]
[<Orderer(SummaryOrderPolicy.FastestToSlowest)>]
[<RankColumn(NumeralSystem.Stars)>]
[<MinColumn; MaxColumn; MedianColumn; MeanColumn; CategoriesColumn>]
[<GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)>]
type SeqBenchmarks() =
member _.thousandWithSleep = 1000u
member _.fiveThousandWithSleep = 5_000u
// for testing early escape performance
member _.GetPartialOkSeqWithSleep size =
seq {
for i in 1u .. size do
Thread.Sleep(TimeSpan.FromMicroseconds(1.0))
if i = size / 2u then Error "error" else Ok i
}
member _.thousand = 1_000u
member _.million = 1_000_000u
member _.GetPartialOkSeq size =
seq {
for i in 1u .. size do
if i = size / 2u then Error "error" else Ok i
}
[<Benchmark(Baseline = true, Description = "original")>]
[<BenchmarkCategory("1000", "sleep")>]
member this.seqCache() =
sequenceResultMTests.SeqCache.sequenceResultM (
this.GetPartialOkSeqWithSleep this.thousandWithSleep
)
[<Benchmark(Description = "Seq.fold")>]
[<BenchmarkCategory("1000", "sleep")>]
member this.seqFoldSmall() =
sequenceResultMTests.SeqFold.sequenceResultM (
this.GetPartialOkSeqWithSleep this.thousandWithSleep
)
[<Benchmark(Description = "inlined Seq.fold")>]
[<BenchmarkCategory("1000", "sleep")>]
member this.inlineSeqFoldSmall() =
sequenceResultMTests.InlinedSeqFold.sequenceResultM (
this.GetPartialOkSeqWithSleep this.thousandWithSleep
)
[<Benchmark(Description = "Seq.unfold")>]
[<BenchmarkCategory("1000", "sleep")>]
member this.seqUnfoldSmall() =
sequenceResultMTests.SeqUnfold.sequenceResultM (
this.GetPartialOkSeqWithSleep this.thousandWithSleep
)
[<Benchmark(Description = "GetEnumerator w/ mutability")>]
[<BenchmarkCategory("1000", "sleep")>]
member this.getEnumeratorSmall() =
sequenceResultMTests.GetEnumerator.sequenceResultM (
this.GetPartialOkSeqWithSleep this.thousandWithSleep
)
[<Benchmark(Description = "inlined GetEnumerator w/ mutability")>]
[<BenchmarkCategory("1000", "sleep")>]
member this.inlineGetEnumeratorSmall() =
sequenceResultMTests.InlinedGetEnumerator.sequenceResultM (
this.GetPartialOkSeqWithSleep this.thousandWithSleep
)
[<Benchmark(Description = "Seq expression")>]
[<BenchmarkCategory("1000", "sleep")>]
member this.seqExpressionSmall() =
sequenceResultMTests.SeqExpr.sequenceResultM (
this.GetPartialOkSeqWithSleep this.thousandWithSleep
)
// made this baseline for this category since unfold and original were so unperformant for this size of data
[<Benchmark(Baseline = true, Description = "Seq.fold")>]
[<BenchmarkCategory("5000", "sleep")>]
member this.seqFoldLarge() =
sequenceResultMTests.SeqFold.sequenceResultM (
this.GetPartialOkSeqWithSleep this.fiveThousandWithSleep
)
[<Benchmark(Description = "inlined Seq.fold")>]
[<BenchmarkCategory("5000", "sleep")>]
member this.inlineSeqFoldLarge() =
sequenceResultMTests.InlinedSeqFold.sequenceResultM (
this.GetPartialOkSeqWithSleep this.fiveThousandWithSleep
)
[<Benchmark(Description = "GetEnumerator w/ mutability")>]
[<BenchmarkCategory("5000", "sleep")>]
member this.getEnumeratorLarge() =
sequenceResultMTests.GetEnumerator.sequenceResultM (
this.GetPartialOkSeqWithSleep this.fiveThousandWithSleep
)
[<Benchmark(Description = "inlined GetEnumerator w/ mutability")>]
[<BenchmarkCategory("5000", "sleep")>]
member this.inlineGetEnumeratorLarge() =
sequenceResultMTests.InlinedGetEnumerator.sequenceResultM (
this.GetPartialOkSeqWithSleep this.fiveThousandWithSleep
)
[<Benchmark(Description = "Seq expression")>]
[<BenchmarkCategory("5000", "sleep")>]
member this.seqExpressionLarge() =
sequenceResultMTests.SeqExpr.sequenceResultM (
this.GetPartialOkSeqWithSleep this.fiveThousandWithSleep
)
[<Benchmark(Baseline = true)>]
[<BenchmarkCategory("1,000", "func vs expr")>]
member this.SeqFuncsSmall() =
this.GetPartialOkSeq this.thousand
|> sequenceResultMTests.InlinedGetEnumerator.sequenceResultM
[<Benchmark>]
[<BenchmarkCategory("1,000", "func vs expr")>]
member this.SeqExprSmall() =
this.GetPartialOkSeq this.thousand
|> sequenceResultMTests.SeqExpr.sequenceResultM
[<Benchmark(Baseline = true)>]
[<BenchmarkCategory("1,000", "func vs expr", "iter")>]
member this.SeqFuncsSmallIter() =
this.GetPartialOkSeq this.thousand
|> sequenceResultMTests.GetEnumerator.sequenceResultM
|> Result.map (Seq.iter ignore)
[<Benchmark>]
[<BenchmarkCategory("1,000", "func vs expr", "iter")>]
member this.SeqExprSmallIter() =
this.GetPartialOkSeq this.thousand
|> sequenceResultMTests.SeqExpr.sequenceResultM
|> Result.map (Seq.iter ignore)
[<Benchmark(Baseline = true)>]
[<BenchmarkCategory("1,000,000", "func vs expr")>]
member this.SeqFuncsLarge() =
this.GetPartialOkSeq this.million
|> sequenceResultMTests.InlinedGetEnumerator.sequenceResultM
[<Benchmark>]
[<BenchmarkCategory("1,000,000", "func vs expr")>]
member this.SeqExprLarge() =
this.GetPartialOkSeq this.million
|> sequenceResultMTests.SeqExpr.sequenceResultM
[<Benchmark(Baseline = true)>]
[<BenchmarkCategory("1,000,000", "func vs expr", "iter")>]
member this.SeqFuncsLargeIter() =
this.GetPartialOkSeq this.million
|> sequenceResultMTests.GetEnumerator.sequenceResultM
|> Result.map (Seq.iter ignore)
[<Benchmark>]
[<BenchmarkCategory("1,000,000", "func vs expr", "iter")>]
member this.SeqExprLargeIter() =
this.GetPartialOkSeq this.million
|> sequenceResultMTests.SeqExpr.sequenceResultM
|> Result.map (Seq.iter ignore)