Skip to content

Commit c616ef4

Browse files
authored
Merge pull request #1097 from kerams/opt
Optimize diffing
2 parents b2afa17 + 5b9f54f commit c616ef4

File tree

8 files changed

+38
-44
lines changed

8 files changed

+38
-44
lines changed

Directory.Packages.props

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<Project>
2-
32
<PropertyGroup>
43
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
54
</PropertyGroup>
6-
75
<ItemGroup>
86
<PackageVersion Include="FsCheck.NUnit" Version="2.16.4" />
9-
<PackageVersion Include="FSharp.Core" Version="7.0.0" />
7+
<PackageVersion Include="FSharp.Core" Version="9.0.101" />
108
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
119
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
1210
<PackageVersion Include="NUnit" Version="3.13.3" />
@@ -15,5 +13,4 @@
1513
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
1614
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
1715
</ItemGroup>
18-
1916
</Project>

src/Fabulous.Benchmarks/Benchmarks.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ let main _argv =
183183

184184
printfn "Hello"
185185

186-
BenchmarkRunner.Run<DiffingSmallScalars.Benchmarks>() |> ignore
186+
BenchmarkRunner.Run(typeof<DiffingSmallScalars.Benchmarks>.Assembly) |> ignore
187187

188188
0 // return an integer exit code
189189

src/Fabulous.Tests/Fabulous.Tests.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net9.0</TargetFramework>
44
<OutputType>Library</OutputType>
@@ -20,6 +20,7 @@
2020
</ItemGroup>
2121
<ItemGroup>
2222
<PackageReference Include="FsCheck.NUnit" />
23+
<PackageReference Include="FSharp.Core" />
2324
<PackageReference Include="Microsoft.NET.Test.Sdk" />
2425
<PackageReference Include="NUnit" />
2526
<PackageReference Include="NUnit3TestAdapter" />

src/Fabulous/Logger.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace Fabulous
33
open System
44
open System.Runtime.CompilerServices
55

6-
[<Struct>]
76
type LogLevel =
87
| Debug = 0
98
| Info = 1

src/Fabulous/Primitives.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module ScalarAttributeKey =
6464
| Code.Inline -> Inline
6565
| _ -> Boxed
6666

67-
let inline getKeyValue (key: ScalarAttributeKey) : int = int((int key) &&& Code.KeyMask)
67+
let inline getKeyValue (key: ScalarAttributeKey) : int = int key &&& Code.KeyMask
6868

6969
let inline compare (a: ScalarAttributeKey) (b: ScalarAttributeKey) =
7070
let a = int a

src/Fabulous/Reconciler.fs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ open Fabulous
44

55
module Reconciler =
66

7-
let private compareScalars (struct (key, a, b): struct (ScalarAttributeKey * obj * obj)) : ScalarAttributeComparison =
8-
let data = AttributeDefinitionStore.getScalar key
9-
data.CompareBoxed a b
10-
117
let update (canReuseView: Widget -> Widget -> bool) (prevOpt: Widget voption) (next: Widget) (node: IViewNode) : unit =
128

13-
let diff = WidgetDiff.create(prevOpt, next, canReuseView, compareScalars)
9+
let diff =
10+
WidgetDiff.create(prevOpt, next, canReuseView, fun key a b -> (AttributeDefinitionStore.getScalar key).CompareBoxed a b)
1411

1512
node.ApplyDiff(&diff)

src/Fabulous/ViewNode.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type ViewNode =
9292

9393
definition.UpdateNode (ValueSome removed.Value) ValueNone (this :> IViewNode)
9494

95-
| WidgetChange.Updated struct (newAttr, diffs) ->
95+
| WidgetChange.Updated(newAttr, diffs) ->
9696
let definition = (AttributeDefinitionStore.getWidget newAttr.Key)
9797

9898
definition.ApplyDiff diffs (this :> IViewNode)
@@ -110,7 +110,7 @@ type ViewNode =
110110

111111
definition.UpdateNode (ValueSome removed.Value) ValueNone (this :> IViewNode)
112112

113-
| WidgetCollectionChange.Updated struct (oldAttr, newAttr, diffs) ->
113+
| WidgetCollectionChange.Updated(oldAttr, newAttr, diffs) ->
114114
let definition = (AttributeDefinitionStore.getWidgetCollection newAttr.Key)
115115

116116
definition.ApplyDiff oldAttr.Value diffs (this :> IViewNode)

src/Fabulous/WidgetDiff.fs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ type ScalarAttributeComparison =
1111

1212
[<Struct; IsByRefLike; RequireQualifiedAccess; NoComparison; NoEquality>]
1313
type EnumerationMode<'a> =
14-
| AllAddedOrRemoved of prevAdd: 'a[] * bool // the first element is either prev or next depending on the bool, but using prev as the label allows the compiler to reuse struct fields between cases
14+
| AllAddedOrRemoved of prev: 'a[] * bool // the first element is either prev or next depending on the bool, but using prev as the label allows the compiler to reuse struct fields between cases
1515
| Empty
16-
| ActualDiff of prevDiff: 'a[] * next: 'a[]
16+
| ActualDiff of prev: 'a[] * next: 'a[]
1717

1818
module EnumerationMode =
1919
let fromOptions prev next =
@@ -75,31 +75,31 @@ module private SkipRepeatingScalars =
7575

7676
[<Struct; IsByRefLike; RequireQualifiedAccess>]
7777
type ScalarChange =
78-
| Added of added: ScalarAttribute
79-
| Removed of removed: ScalarAttribute
80-
| Updated of old: ScalarAttribute * updated: ScalarAttribute
78+
| Added of attr: ScalarAttribute
79+
| Removed of attr: ScalarAttribute
80+
| Updated of attr: ScalarAttribute * updated: ScalarAttribute // old * updated
8181

8282
and [<Struct; RequireQualifiedAccess>] WidgetChange =
83-
| Added of added: WidgetAttribute
84-
| Removed of removed: WidgetAttribute
85-
| Updated of updated: struct (WidgetAttribute * WidgetDiff)
86-
| ReplacedBy of replacedBy: WidgetAttribute
83+
| Added of widget: WidgetAttribute
84+
| Removed of widget: WidgetAttribute
85+
| Updated of widget: WidgetAttribute * diff: WidgetDiff // updated * diff
86+
| ReplacedBy of widget: WidgetAttribute
8787

8888
and [<Struct; RequireQualifiedAccess>] WidgetCollectionChange =
89-
| Added of added: WidgetCollectionAttribute
90-
| Removed of removed: WidgetCollectionAttribute
91-
| Updated of updated: struct (WidgetCollectionAttribute * WidgetCollectionAttribute * WidgetCollectionItemChanges)
89+
| Added of attr: WidgetCollectionAttribute
90+
| Removed of attr: WidgetCollectionAttribute
91+
| Updated of attr: WidgetCollectionAttribute * updated: WidgetCollectionAttribute * diff: WidgetCollectionItemChanges // old * updated * diff
9292

9393
and [<Struct; IsByRefLike; RequireQualifiedAccess>] WidgetCollectionItemChange =
94-
| Insert of widgetInserted: struct (int * Widget)
95-
| Replace of widgetReplaced: struct (int * Widget * Widget)
96-
| Update of widgetUpdated: struct (int * WidgetDiff)
97-
| Remove of removed: struct (int * Widget)
94+
| Insert of index: int * widget: Widget
95+
| Replace of index: int * widget: Widget * replacedBy: Widget // index * old * replacedBy
96+
| Update of index: int * diff: WidgetDiff
97+
| Remove of index: int * widget: Widget
9898

9999
and [<Struct; IsByRefLike; RequireQualifiedAccess>] EnvironmentChange =
100-
| Added of added: EnvironmentAttribute
101-
| Removed of removed: EnvironmentAttribute
102-
| Updated of old: EnvironmentAttribute * updated: EnvironmentAttribute
100+
| Added of attr: EnvironmentAttribute
101+
| Removed of attr: EnvironmentAttribute
102+
| Updated of attr: EnvironmentAttribute * updated: EnvironmentAttribute // old * updated
103103

104104
and [<Struct; NoComparison; NoEquality>] WidgetDiff =
105105
{ ScalarChanges: ScalarChanges
@@ -112,7 +112,7 @@ and [<Struct; NoComparison; NoEquality>] WidgetDiff =
112112
prevOpt: Widget voption,
113113
next: Widget,
114114
canReuseView: Widget -> Widget -> bool,
115-
compareScalars: struct (ScalarAttributeKey * obj * obj) -> ScalarAttributeComparison
115+
compareScalars: ScalarAttributeKey -> obj -> obj -> ScalarAttributeComparison
116116
) : WidgetDiff =
117117

118118
let prevScalarAttributes =
@@ -141,7 +141,7 @@ and [<Struct; NoComparison; NoEquality>] WidgetDiff =
141141
EnvironmentChanges = EnvironmentChanges(prevEnvironmentAttributes, next.EnvironmentAttributes) }
142142

143143
and [<Struct; NoComparison; NoEquality>] ScalarChanges
144-
(prev: ScalarAttribute[], next: ScalarAttribute[], compareScalars: struct (ScalarAttributeKey * obj * obj) -> ScalarAttributeComparison) =
144+
(prev: ScalarAttribute[], next: ScalarAttribute[], compareScalars: ScalarAttributeKey -> obj -> obj -> ScalarAttributeComparison) =
145145
member _.GetEnumerator() =
146146
ScalarChangesEnumerator(EnumerationMode.fromOptions prev next, compareScalars)
147147

@@ -150,7 +150,7 @@ and [<Struct; NoComparison; NoEquality>] WidgetChanges
150150
prev: WidgetAttribute[],
151151
next: WidgetAttribute[],
152152
canReuseView: Widget -> Widget -> bool,
153-
compareScalars: struct (ScalarAttributeKey * obj * obj) -> ScalarAttributeComparison
153+
compareScalars: ScalarAttributeKey -> obj -> obj -> ScalarAttributeComparison
154154
) =
155155
member _.GetEnumerator() =
156156
WidgetChangesEnumerator(EnumerationMode.fromOptions prev next, canReuseView, compareScalars)
@@ -160,7 +160,7 @@ and [<Struct; NoComparison; NoEquality>] WidgetCollectionChanges
160160
prev: WidgetCollectionAttribute[],
161161
next: WidgetCollectionAttribute[],
162162
canReuseView: Widget -> Widget -> bool,
163-
compareScalars: struct (ScalarAttributeKey * obj * obj) -> ScalarAttributeComparison
163+
compareScalars: ScalarAttributeKey -> obj -> obj -> ScalarAttributeComparison
164164
) =
165165
member _.GetEnumerator() =
166166
WidgetCollectionChangesEnumerator(EnumerationMode.fromOptions prev next, canReuseView, compareScalars)
@@ -171,7 +171,7 @@ and [<Struct; NoComparison; NoEquality>] WidgetCollectionItemChanges
171171
prev: ArraySlice<Widget>,
172172
next: ArraySlice<Widget>,
173173
canReuseView: Widget -> Widget -> bool,
174-
compareScalars: struct (ScalarAttributeKey * obj * obj) -> ScalarAttributeComparison
174+
compareScalars: ScalarAttributeKey -> obj -> obj -> ScalarAttributeComparison
175175
) =
176176
member _.GetEnumerator() =
177177
WidgetCollectionItemChangesEnumerator(ArraySlice.toSpan prev, ArraySlice.toSpan next, canReuseView, compareScalars)
@@ -182,7 +182,7 @@ and [<Struct; NoComparison; NoEquality>] EnvironmentChanges(prev: EnvironmentAtt
182182

183183
// enumerators
184184
and [<Struct; IsByRefLike>] ScalarChangesEnumerator
185-
(mode: EnumerationMode<ScalarAttribute>, compareScalars: struct (ScalarAttributeKey * obj * obj) -> ScalarAttributeComparison) =
185+
(mode: EnumerationMode<ScalarAttribute>, compareScalars: ScalarAttributeKey -> obj -> obj -> ScalarAttributeComparison) =
186186

187187
[<DefaultValue(false)>]
188188
val mutable private current: ScalarChange
@@ -270,7 +270,7 @@ and [<Struct; IsByRefLike>] ScalarChangesEnumerator
270270
res <- ValueSome true
271271

272272
| ScalarAttributeKey.Kind.Boxed ->
273-
match compareScalars struct (prevKey, prevAttr.Value, nextAttr.Value) with
273+
match compareScalars prevKey prevAttr.Value nextAttr.Value with
274274
// Previous and next values are identical, we don't need to do anything
275275
| ScalarAttributeComparison.Identical -> ()
276276

@@ -297,7 +297,7 @@ and [<Struct; IsByRefLike>] WidgetChangesEnumerator
297297
(
298298
mode: EnumerationMode<WidgetAttribute>,
299299
canReuseView: Widget -> Widget -> bool,
300-
compareScalars: struct (ScalarAttributeKey * obj * obj) -> ScalarAttributeComparison
300+
compareScalars: ScalarAttributeKey -> obj -> obj -> ScalarAttributeComparison
301301
) =
302302

303303
[<DefaultValue(false)>]
@@ -414,7 +414,7 @@ and [<Struct; IsByRefLike>] WidgetCollectionChangesEnumerator
414414
(
415415
mode: EnumerationMode<WidgetCollectionAttribute>,
416416
canReuseView: Widget -> Widget -> bool,
417-
compareScalars: struct (ScalarAttributeKey * obj * obj) -> ScalarAttributeComparison
417+
compareScalars: ScalarAttributeKey -> obj -> obj -> ScalarAttributeComparison
418418
) =
419419

420420
[<DefaultValue(false)>]
@@ -515,7 +515,7 @@ and [<Struct; IsByRefLike>] WidgetCollectionItemChangesEnumerator
515515
prev: Span<Widget>,
516516
next: Span<Widget>,
517517
canReuseView: Widget -> Widget -> bool,
518-
compareScalars: struct (ScalarAttributeKey * obj * obj) -> ScalarAttributeComparison
518+
compareScalars: ScalarAttributeKey -> obj -> obj -> ScalarAttributeComparison
519519
) =
520520
[<DefaultValue(false)>]
521521
val mutable private current: WidgetCollectionItemChange

0 commit comments

Comments
 (0)