Skip to content

Commit 6471d1d

Browse files
committed
feat: use ListCollector to build lists
1 parent b0483ad commit 6471d1d

File tree

4 files changed

+193
-165
lines changed

4 files changed

+193
-165
lines changed

benchmarks/FSharp.SystemTextJson.Benchmarks/Program.fs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@ open System
66
open BenchmarkDotNet.Attributes
77
open BenchmarkDotNet.Diagnosers
88
open BenchmarkDotNet.Configs
9-
open BenchmarkDotNet.Jobs
109
open BenchmarkDotNet.Running
1110
open BenchmarkDotNet.Validators
1211
open BenchmarkDotNet.Exporters
13-
open BenchmarkDotNet.Environments
14-
open System.Reflection
15-
open BenchmarkDotNet.Configs
1612

1713
open System.Text.Json
1814
open System.Text.Json.Serialization
1915
open Newtonsoft.Json
20-
open Newtonsoft.Json.Linq
2116

2217
type TestRecord =
2318
{ name: string
2419
thing: bool option
25-
time: System.DateTimeOffset }
20+
time: DateTimeOffset }
2621

2722
type SimpleClass() =
2823
member val Name: string = null with get, set
@@ -94,6 +89,28 @@ type ReflectionComparison() =
9489
for i in 0 .. this.Iterations do
9590
TypeCache.isRecord typeof<TestRecord> |> ignore
9691

92+
type ListDeserialization<'t>(instance: 't) =
93+
let options = JsonFSharpOptions().ToJsonSerializerOptions()
94+
95+
[<Params(10, 100)>]
96+
member val ListLength = 0 with get, set
97+
98+
member val String = "" with get, set
99+
100+
[<GlobalSetup>]
101+
member this.InitList() =
102+
this.String <- System.Text.Json.JsonSerializer.Serialize(Array.replicate this.ListLength instance, options)
103+
104+
[<Benchmark>]
105+
member this.ListCollector() =
106+
System.Text.Json.JsonSerializer.Deserialize<'t list>(this.String, options)
107+
108+
[<Benchmark>]
109+
member this.AsArray() =
110+
System.Text.Json.JsonSerializer.Deserialize<'t array>(this.String, options)
111+
|> List.ofArray
112+
113+
type ListDeserialization() = inherit ListDeserialization<int>(42)
97114

98115
let config =
99116
ManualConfig
@@ -106,7 +123,8 @@ let defaultSwitch () =
106123
BenchmarkSwitcher(
107124
[| typeof<Records>
108125
typeof<Classes>
109-
typeof<ReflectionComparison> |]
126+
typeof<ReflectionComparison>
127+
typeof<ListDeserialization> |]
110128
)
111129

112130

paket.dependencies

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ frameworks netstandard2.0
22
storage none
33
source https://api.nuget.org/v3/index.json
44

5-
nuget FSharp.Core >= 4.7.0 lowest_matching:true
5+
nuget FSharp.Core >= 6.0 lowest_matching:true
66
nuget Microsoft.SourceLink.GitHub prerelease copy_local:true
77
nuget Nerdbank.GitVersioning copy_local:true
88
nuget System.Text.Json >= 6.0.10 lowest_matching: true
@@ -12,7 +12,7 @@ framework net9.0
1212
storage none
1313
source https://api.nuget.org/v3/index.json
1414

15-
nuget FSharp.Core >= 5.0
15+
nuget FSharp.Core >= 6.0
1616
nuget FsCheck.XUnit
1717
nuget Microsoft.NET.Test.Sdk
1818
nuget System.Text.Json >= 8.0.5 prerelease

0 commit comments

Comments
 (0)