Skip to content

Commit e8eb074

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

4 files changed

Lines changed: 194 additions & 168 deletions

File tree

benchmarks/FSharp.SystemTextJson.Benchmarks/Program.fs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@ 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

22-
type TestRecord =
23-
{ name: string
24-
thing: bool option
25-
time: System.DateTimeOffset }
17+
type TestRecord = { name: string; thing: bool option; time: DateTimeOffset }
2618

2719
type SimpleClass() =
2820
member val Name: string = null with get, set
@@ -94,6 +86,29 @@ type ReflectionComparison() =
9486
for i in 0 .. this.Iterations do
9587
TypeCache.isRecord typeof<TestRecord> |> ignore
9688

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

98113
let config =
99114
ManualConfig
@@ -106,7 +121,8 @@ let defaultSwitch () =
106121
BenchmarkSwitcher(
107122
[| typeof<Records>
108123
typeof<Classes>
109-
typeof<ReflectionComparison> |]
124+
typeof<ReflectionComparison>
125+
typeof<ListDeserialization> |]
110126
)
111127

112128

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)