Skip to content

Commit 05904c2

Browse files
committed
formatting
1 parent 5e2ad50 commit 05904c2

File tree

7 files changed

+605
-368
lines changed

7 files changed

+605
-368
lines changed

benchmarks/ApplicativeTests.fs

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,105 +6,135 @@ open FsToolkit.ErrorHandling
66
open System.Threading
77
open System
88

9-
let successSlowSync (time : TimeSpan) =
9+
let successSlowSync (time: TimeSpan) =
1010
Thread.Sleep time
1111
Ok time.Ticks
1212

13-
let errorSlowSync (time : TimeSpan) =
13+
let errorSlowSync (time: TimeSpan) =
1414
Thread.Sleep time
1515
Error "failed"
1616

1717
[<MemoryDiagnoser>]
18-
type Result_BindvsAndCEBenchmarks () =
18+
type Result_BindvsAndCEBenchmarks() =
1919

2020
member this.ValuesForDelay = [|
21-
TimeSpan.FromMilliseconds (0.)
22-
TimeSpan.FromMilliseconds (10.)
23-
TimeSpan.FromMilliseconds (100.)
21+
TimeSpan.FromMilliseconds(0.)
22+
TimeSpan.FromMilliseconds(10.)
23+
TimeSpan.FromMilliseconds(100.)
2424
|]
2525
// let this.delay = TimeSpan.FromMilliseconds 100.
2626
[<ParamsSource("ValuesForDelay")>]
27-
member val public delay =
28-
TimeSpan.MinValue
29-
with get,set
27+
member val public delay = TimeSpan.MinValue with get, set
3028

3129

3230
[<Benchmark(Baseline = true)>]
33-
member this.All_Success_Bind() =
31+
member this.All_Success_Bind() =
3432
result {
3533
let! r1 = successSlowSync this.delay
3634
let! r2 = successSlowSync this.delay
3735
let! r3 = successSlowSync this.delay
38-
return r1 + r2 + r3
36+
37+
return
38+
r1
39+
+ r2
40+
+ r3
3941
}
4042
|> ignore
4143

4244
[<Benchmark>]
43-
member this.All_Success_And() =
45+
member this.All_Success_And() =
4446
result {
4547
let! r1 = successSlowSync this.delay
4648
and! r2 = successSlowSync this.delay
4749
and! r3 = successSlowSync this.delay
48-
return r1 + r2 + r3
50+
51+
return
52+
r1
53+
+ r2
54+
+ r3
4955
}
5056
|> ignore
5157

5258
[<Benchmark>]
53-
member this.Fail_First_Bind() =
59+
member this.Fail_First_Bind() =
5460
result {
5561
let! r1 = errorSlowSync this.delay
5662
let! r2 = successSlowSync this.delay
5763
let! r3 = successSlowSync this.delay
58-
return r1 + r2 + r3
64+
65+
return
66+
r1
67+
+ r2
68+
+ r3
5969
}
6070
|> ignore
6171

6272
[<Benchmark>]
63-
member this.Fail_First_And() =
73+
member this.Fail_First_And() =
6474
result {
6575
let! r1 = errorSlowSync this.delay
6676
and! r2 = successSlowSync this.delay
6777
and! r3 = successSlowSync this.delay
68-
return r1 + r2 + r3
78+
79+
return
80+
r1
81+
+ r2
82+
+ r3
6983
}
7084
|> ignore
7185

7286
[<Benchmark>]
73-
member this.Fail_Mid_Bind() =
87+
member this.Fail_Mid_Bind() =
7488
result {
7589
let! r1 = successSlowSync this.delay
7690
let! r2 = errorSlowSync this.delay
7791
let! r3 = successSlowSync this.delay
78-
return r1 + r2 + r3
92+
93+
return
94+
r1
95+
+ r2
96+
+ r3
7997
}
8098
|> ignore
8199

82100
[<Benchmark>]
83-
member this.Fail_Mid_And() =
101+
member this.Fail_Mid_And() =
84102
result {
85103
let! r1 = successSlowSync this.delay
86104
and! r2 = errorSlowSync this.delay
87105
and! r3 = successSlowSync this.delay
88-
return r1 + r2 + r3
106+
107+
return
108+
r1
109+
+ r2
110+
+ r3
89111
}
90112
|> ignore
91113

92114
[<Benchmark>]
93-
member this.Fail_Last_Bind() =
115+
member this.Fail_Last_Bind() =
94116
result {
95117
let! r1 = successSlowSync this.delay
96118
let! r2 = successSlowSync this.delay
97119
let! r3 = errorSlowSync this.delay
98-
return r1 + r2 + r3
120+
121+
return
122+
r1
123+
+ r2
124+
+ r3
99125
}
100126
|> ignore
101127

102128
[<Benchmark>]
103-
member this.Fail_Last_And() =
129+
member this.Fail_Last_And() =
104130
result {
105131
let! r1 = successSlowSync this.delay
106132
and! r2 = successSlowSync this.delay
107133
and! r3 = errorSlowSync this.delay
108-
return r1 + r2 + r3
134+
135+
return
136+
r1
137+
+ r2
138+
+ r3
109139
}
110140
|> ignore

benchmarks/AsyncResultCE.fs

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,43 @@ open System
44
open BenchmarkDotNet
55
open BenchmarkDotNet.Attributes
66
open FsToolkit.ErrorHandling
7+
78
module AsyncResultCE =
89

910

1011
type AsyncResultInlinedLambdaBuilder() =
1112

12-
member _.Return(value: 'T) : Async<Result<'T, 'TError>> = async.Return <| result.Return value
13+
member _.Return(value: 'T) : Async<Result<'T, 'TError>> =
14+
async.Return
15+
<| result.Return value
1316

14-
member inline _.ReturnFrom(asyncResult: Async<Result<'T, 'TError>>) : Async<Result<'T, 'TError>> = asyncResult
17+
member inline _.ReturnFrom
18+
(asyncResult: Async<Result<'T, 'TError>>)
19+
: Async<Result<'T, 'TError>> =
20+
asyncResult
1521

16-
member _.Zero() : Async<Result<unit, 'TError>> = async.Return <| result.Zero()
22+
member _.Zero() : Async<Result<unit, 'TError>> =
23+
async.Return
24+
<| result.Zero()
1725

1826
member inline _.Bind
1927
(
2028
asyncResult: Async<Result<'T, 'TError>>,
2129
[<InlineIfLambda>] binder: 'T -> Async<Result<'U, 'TError>>
2230
) : Async<Result<'U, 'TError>> =
23-
async.Bind(asyncResult, fun r ->
24-
match r with
25-
| Ok x -> binder x
26-
| Error e -> Error e |> async.Return
31+
async.Bind(
32+
asyncResult,
33+
fun r ->
34+
match r with
35+
| Ok x -> binder x
36+
| Error e ->
37+
Error e
38+
|> async.Return
2739
)
28-
member inline _.Delay([<InlineIfLambda>] generator: unit -> Async<Result<'T, 'TError>>) : Async<Result<'T, 'TError>> =
40+
41+
member inline _.Delay
42+
([<InlineIfLambda>] generator: unit -> Async<Result<'T, 'TError>>)
43+
: Async<Result<'T, 'TError>> =
2944
async.Delay generator
3045

3146
/// <summary>
@@ -34,9 +49,8 @@ module AsyncResultCE =
3449
/// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder
3550
/// </summary>
3651
member inline _.Source(result: Async<Result<_, _>>) : Async<Result<_, _>> = result
37-
open AsyncResultCE
38-
3952

53+
open AsyncResultCE
4054

4155

4256
[<AutoOpen>]
@@ -45,6 +59,7 @@ open AsyncResultCE
4559
module AsyncResultCEExtensions =
4660

4761
type AsyncResultInlinedLambdaBuilder with
62+
4863
/// <summary>
4964
/// Needed to allow `for..in` and `for..do` functionality
5065
/// </summary>
@@ -54,55 +69,69 @@ module AsyncResultCEExtensions =
5469
/// Method lets us transform data types into our internal representation.
5570
/// </summary>
5671
member inline _.Source(result: Result<_, _>) : Async<Result<_, _>> = Async.singleton result
72+
5773
let rec fib n =
58-
if n < 2L then n
59-
else fib (n - 1L) + fib (n - 2L)
60-
61-
let rec afib (n, level) = async {
62-
if n < 0L then return Error "No"
63-
elif n < 2L then
64-
return Ok n
65-
elif n < level then
66-
return Ok (fib n)
74+
if n < 2L then
75+
n
6776
else
68-
let! n2a = afib (n-2L, level) |> Async.StartChild
69-
let! n1 = afib (n-1L, level)
70-
let! n2 = n2a
71-
match n1, n2 with
72-
| Ok n1, Ok n2 ->
73-
return Ok (n2 + n1)
74-
| Error e, _
75-
| _, Error e -> return Error e
77+
fib (n - 1L)
78+
+ fib (n - 2L)
79+
80+
let rec afib (n, level) =
81+
async {
82+
if n < 0L then
83+
return Error "No"
84+
elif n < 2L then
85+
return Ok n
86+
elif n < level then
87+
return Ok(fib n)
88+
else
89+
let! n2a =
90+
afib (n - 2L, level)
91+
|> Async.StartChild
92+
93+
let! n1 = afib (n - 1L, level)
94+
let! n2 = n2a
95+
96+
match n1, n2 with
97+
| Ok n1, Ok n2 -> return Ok(n2 + n1)
98+
| Error e, _
99+
| _, Error e -> return Error e
76100
}
101+
77102
let asyncResultInlinedIfLambda = AsyncResultInlinedLambdaBuilder()
78103

79104
[<MemoryDiagnoser>]
80-
type AsyncResult_BindCEBenchmarks () =
105+
type AsyncResult_BindCEBenchmarks() =
81106

82107
[<Benchmark(Baseline = true)>]
83-
member this.afib() =
84-
afib(10,5)
108+
member this.afib() =
109+
afib (10, 5)
85110
|> Async.StartImmediateAsTask
86111

87112
[<Benchmark>]
88-
member this.Result_Normal_Bind_CE() =
89-
let action () = asyncResult {
90-
let! a = Ok 10
91-
let! b = Ok 5
92-
let! c = afib(a, b)
93-
return c
94-
}
113+
member this.Result_Normal_Bind_CE() =
114+
let action () =
115+
asyncResult {
116+
let! a = Ok 10
117+
let! b = Ok 5
118+
let! c = afib (a, b)
119+
return c
120+
}
121+
95122
action ()
96123
|> Async.StartImmediateAsTask
97124

98125

99126
[<Benchmark>]
100-
member this.Result_Alt_Inlined_Bind_CE () =
101-
let action () = asyncResultInlinedIfLambda {
102-
let! a = Ok 10
103-
let! b = Ok 5
104-
let! c = afib(a, b)
105-
return c
106-
}
127+
member this.Result_Alt_Inlined_Bind_CE() =
128+
let action () =
129+
asyncResultInlinedIfLambda {
130+
let! a = Ok 10
131+
let! b = Ok 5
132+
let! c = afib (a, b)
133+
return c
134+
}
135+
107136
action ()
108137
|> Async.StartImmediateAsTask

0 commit comments

Comments
 (0)