Skip to content

Commit

Permalink
Merge pull request #805 from TimLariviere/reduce-allocations-2
Browse files Browse the repository at this point in the history
Reduce allocations
  • Loading branch information
TimLariviere authored Oct 1, 2020
2 parents 2d69f34 + 7eccc49 commit 44b6cca
Show file tree
Hide file tree
Showing 29 changed files with 860 additions and 704 deletions.
9 changes: 6 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<Project>
<!-- NuGet Specs -->
<PropertyGroup>
<Version>0.58.0</Version>
<Version>0.60.0</Version>
<Authors>Fabulous Contributors</Authors>
<PackageVersion>0.58.0</PackageVersion>
<PackageReleaseNotes>[All] Proper version constraints for the NuGet packages</PackageReleaseNotes>
<PackageVersion>0.60.0-preview1</PackageVersion>
<PackageReleaseNotes>[All] Proper version constraints for the NuGet packages (https://github.com/fsprojects/Fabulous/pull/797)
[All] Add FSharp.Core as a public dependency (https://github.com/fsprojects/Fabulous/pull/796)
[All] Reduced allocations (https://github.com/fsprojects/Fabulous/pull/805)
[Fabulous.XamarinForms] [Templates] Add native main menu to MacOS template (https://github.com/fsprojects/Fabulous/pull/806)</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/fsprojects/Fabulous</PackageProjectUrl>
Expand Down
142 changes: 79 additions & 63 deletions Fabulous.CodeGen/src/Fabulous.CodeGen/Generator/CodeGenerator.fs

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions Fabulous.CodeGen/src/Fabulous.CodeGen/Generator/Models.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module Models =
{ Name: string
UniqueName: string
RelatedProperties: string array }

type UpdateProperty =
{ Name: string
UniqueName: string
Expand All @@ -62,22 +62,27 @@ module Models =
ModelType: string
ConvertModelToValue: string
UpdateCode: string }

type UpdatePropertyWithAttachedProperties =
{ UniqueName: string
CustomAttributeKey: string option
CollectionDataElementType: string option
AttachedProperties: UpdateAttachedProperty array }

type UpdateAttachedPropertiesData =
{ Name: string
FullName: string
BaseName: string option
PropertiesWithAttachedProperties: UpdatePropertyWithAttachedProperties array }

type UpdateData =
{ Name: string
FullName: string
BaseName: string option
ImmediateMembers : UpdateMember array
Events: UpdateEvent array
Properties: UpdateProperty array
PriorityProperties: UpdateProperty array
PropertiesWithAttachedProperties: UpdatePropertyWithAttachedProperties array }
PriorityProperties: UpdateProperty array }

type ConstructData =
{ Name: string
Expand All @@ -87,6 +92,7 @@ module Models =
type BuilderData =
{ Build: BuildData
Create: CreateData option
UpdateAttachedProperties: UpdateAttachedPropertiesData
Update: UpdateData
Construct: ConstructData option }

Expand Down
57 changes: 32 additions & 25 deletions Fabulous.CodeGen/src/Fabulous.CodeGen/Generator/Preparer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,38 @@ module Preparer =
FullName = boundType.FullName
TypeToInstantiate = boundType.TypeToInstantiate }

let toUpdateAttachedPropertiesData (boundType: BoundType) =
let immediatePropertiesWithAttachedProperties = boundType.Properties |> Array.filter (fun p -> not p.IsInherited && p.CollectionData.IsSome && p.CollectionData.Value.AttachedProperties.Length > 0)

let updatePropertiesWithAttachedProperties =
immediatePropertiesWithAttachedProperties
|> Array.map (fun p ->
{ UniqueName = p.UniqueName
CustomAttributeKey = p.CustomAttributeKey
CollectionDataElementType = p.CollectionData |> Option.map (fun c -> c.ElementType)
AttachedProperties =
p.CollectionData
|> Option.map (fun cd ->
cd.AttachedProperties
|> Array.map (fun ap ->
{ Name = ap.Name
UniqueName = ap.UniqueName
CustomAttributeKey = ap.CustomAttributeKey
DefaultValue = ap.DefaultValue
OriginalType = ap.OriginalType
ModelType = ap.ModelType
ConvertModelToValue = ap.ConvertModelToValue
UpdateCode = ap.UpdateCode }))
|> Option.defaultValue [||] })

{ Name = boundType.Name
FullName = boundType.FullName
BaseName = boundType.BaseTypeName
PropertiesWithAttachedProperties = updatePropertiesWithAttachedProperties }

let toUpdateData (boundType: BoundType) =
let immediateEvents = boundType.Events |> Array.filter (fun e -> not e.IsInherited && e.CanBeUpdated)
let immediateProperties = boundType.Properties |> Array.filter (fun p -> not p.IsInherited && p.CanBeUpdated)
let immediatePropertiesWithAttachedProperties = boundType.Properties |> Array.filter (fun p -> not p.IsInherited && p.CollectionData.IsSome && p.CollectionData.Value.AttachedProperties.Length > 0)

let eventMembers = immediateEvents |> Array.map (fun e -> { UniqueName = e.UniqueName; CustomAttributeKey = e.CustomAttributeKey; ModelType = e.ModelType })
let propertyMembers = immediateProperties |> Array.map (fun p -> { UniqueName = p.UniqueName; CustomAttributeKey = p.CustomAttributeKey; ModelType = p.ModelType })
Expand All @@ -67,7 +95,7 @@ module Preparer =
UniqueName = e.UniqueName
RelatedProperties = relatedProperties }
)

let updateProperties =
immediateProperties
|> Array.filter (fun p -> not p.HasPriority)
Expand Down Expand Up @@ -95,36 +123,14 @@ module Preparer =
ConvertModelToValue = p.ConvertModelToValue
UpdateCode = p.UpdateCode
CollectionDataElementType = p.CollectionData |> Option.map (fun c -> c.ElementType) })

let updatePropertiesWithAttachedProperties =
immediatePropertiesWithAttachedProperties
|> Array.map (fun p ->
{ UniqueName = p.UniqueName
CustomAttributeKey = p.CustomAttributeKey
CollectionDataElementType = p.CollectionData |> Option.map (fun c -> c.ElementType)
AttachedProperties =
p.CollectionData
|> Option.map (fun cd ->
cd.AttachedProperties
|> Array.map (fun ap ->
{ Name = ap.Name
UniqueName = ap.UniqueName
CustomAttributeKey = ap.CustomAttributeKey
DefaultValue = ap.DefaultValue
OriginalType = ap.OriginalType
ModelType = ap.ModelType
ConvertModelToValue = ap.ConvertModelToValue
UpdateCode = ap.UpdateCode }))
|> Option.defaultValue [||] })

{ Name = boundType.Name
FullName = boundType.FullName
BaseName = boundType.BaseTypeName
ImmediateMembers = immediateMembers
Events = updateEvents
Properties = updateProperties
PriorityProperties = updatePriorityProperties
PropertiesWithAttachedProperties = updatePropertiesWithAttachedProperties }
PriorityProperties = updatePriorityProperties }

let toConstructData (boundType: BoundType) : ConstructData =
let properties = boundType.Properties |> Array.map (fun p -> { Name = p.ShortName; InputType = p.InputType } : ConstructType)
Expand All @@ -138,6 +144,7 @@ module Preparer =
let toBuilderData (boundType: BoundType) =
{ Build = toBuildData boundType
Create = if boundType.CanBeInstantiated then Some (toCreateData boundType) else None
UpdateAttachedProperties = toUpdateAttachedPropertiesData boundType
Update = toUpdateData boundType
Construct = if boundType.CanBeInstantiated then Some (toConstructData boundType) else None }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"assemblies": [
"packages/generator/Xamarin.Forms/lib/netstandard2.0/Xamarin.Forms.Core.dll",
"build_output/Fabulous.XamarinForms/Fabulous.XamarinForms.Core/Fabulous.XamarinForms.Core.dll",
"Fabulous.XamarinForms/src/Fabulous.XamarinForms.Core/bin/Release/netstandard2.0/Fabulous.XamarinForms.Core.dll",

"packages/generator/Xamarin.FFImageLoading/lib/netstandard1.0/FFImageLoading.dll",
"packages/generator/Xamarin.FFImageLoading/lib/netstandard1.0/FFImageLoading.Platform.dll",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"assemblies": [
"packages/generator/Xamarin.Forms/lib/netstandard2.0/Xamarin.Forms.Core.dll",
"build_output/Fabulous.XamarinForms/Fabulous.XamarinForms.Core/Fabulous.XamarinForms.Core.dll",
"Fabulous.XamarinForms/src/Fabulous.XamarinForms.Core/bin/Release/netstandard2.0/Fabulous.XamarinForms.Core.dll",

"packages/generator/Xamarin.Forms.Maps/lib/netstandard2.0/Xamarin.Forms.Maps.dll"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"assemblies": [
"packages/generator/Xamarin.Forms/lib/netstandard2.0/Xamarin.Forms.Core.dll",
"build_output/Fabulous.XamarinForms/Fabulous.XamarinForms.Core/Fabulous.XamarinForms.Core.dll",
"Fabulous.XamarinForms/src/Fabulous.XamarinForms.Core/bin/Release/netstandard2.0/Fabulous.XamarinForms.Core.dll",

"packages/generator/OxyPlot.Core/lib/netstandard1.0/OxyPlot.dll",
"packages/generator/OxyPlot.Xamarin.Forms/lib/portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10/OxyPlot.Xamarin.Forms.dll"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"assemblies": [
"packages/generator/Xamarin.Forms/lib/netstandard2.0/Xamarin.Forms.Core.dll",
"build_output/Fabulous.XamarinForms/Fabulous.XamarinForms.Core/Fabulous.XamarinForms.Core.dll",
"Fabulous.XamarinForms/src/Fabulous.XamarinForms.Core/bin/Release/netstandard2.0/Fabulous.XamarinForms.Core.dll",

"packages/generator/SkiaSharp/lib/netstandard1.3/SkiaSharp.dll",
"packages/generator/SkiaSharp.Views.Forms/ref/netstandard1.3/SkiaSharp.Views.Forms.dll"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"assemblies": [
"packages/generator/Xamarin.Forms/lib/netstandard2.0/Xamarin.Forms.Core.dll",
"build_output/Fabulous.XamarinForms/Fabulous.XamarinForms.Core/Fabulous.XamarinForms.Core.dll",
"Fabulous.XamarinForms/src/Fabulous.XamarinForms.Core/bin/Release/netstandard2.0/Fabulous.XamarinForms.Core.dll",

"packages/generator/Plugin.MediaManager/lib/netstandard2.0/MediaManager.dll",
"packages/generator/Plugin.MediaManager.Forms/lib/netstandard2.0/MediaManager.Forms.dll"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ module ShadowEffectViewExtension =

let create () = ShadowEffect()

let update _ (prevOpt: ViewElement voption) (source: ViewElement) (target: ShadowEffect) =
let update (prevOpt: ViewElement voption) (source: ViewElement) (target: ShadowEffect) =
source.UpdatePrimitive(prevOpt, target, RadiusAttribKey, (fun target v -> target.Radius <- v))
source.UpdatePrimitive(prevOpt, target, ColorAttribKey, (fun target v -> target.Color <- v))
source.UpdatePrimitive(prevOpt, target, DistanceXAttribKey, (fun target v -> target.DistanceX <- v))
source.UpdatePrimitive(prevOpt, target, DistanceYAttribKey, (fun target v -> target.DistanceY <- v))

let updateAttachedProperties _ _ _ _ = ()

ViewElement.Create(create, update, attribs)
ViewElement.Create(create, update, updateAttachedProperties, attribs)

Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ module TestLabel =
let create () = Xamarin.Forms.Label()

// The incremental update method
let update registry (prevOpt: ViewElement voption) (source: ViewElement) (target: Xamarin.Forms.Label) =
ViewBuilders.UpdateView(registry, prevOpt, source, target)
let update (prevOpt: ViewElement voption) (source: ViewElement) (target: Xamarin.Forms.Label) =
ViewBuilders.UpdateView(prevOpt, source, target)
source.UpdatePrimitive(prevOpt, target, TestLabelTextAttribKey, (fun target v -> target.Text <- v))
source.UpdatePrimitive(prevOpt, target, TestLabelFontFamilyAttribKey, (fun target v -> target.FontFamily <- v))

ViewElement.Create<Xamarin.Forms.Label>(create, update, attribs)
let updateAttachedProperties _ _ _ _ = ()

ViewElement.Create<Xamarin.Forms.Label>(create, update, updateAttachedProperties, attribs)

Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ module PancakeViewExtensions =
let create () = Xamarin.Forms.PancakeView.PancakeView()

// The incremental update method
let update registry (prev: ViewElement voption) (source: ViewElement) (target: Xamarin.Forms.PancakeView.PancakeView) =
ViewBuilders.UpdateView(registry, prev,source,target)
let update (prev: ViewElement voption) (source: ViewElement) (target: Xamarin.Forms.PancakeView.PancakeView) =
ViewBuilders.UpdateView(prev,source,target)
source.UpdateElement(prev,target, pancakeContentAttribKey,(fun target -> target.Content), (fun target v -> target.Content <- v))
source.UpdatePrimitive(prev, target, backgroundGradientStartColorAttribKey, (fun target v -> target.BackgroundGradientStartColor <- v))
source.UpdatePrimitive(prev, target, backgroundGradientEndColorAttribKey, (fun target v -> target.BackgroundGradientEndColor <- v))
source.UpdatePrimitive(prev, target, paddingAttribKey, (fun target v -> target.Padding <- v))
source.UpdatePrimitive(prev, target, cornerRadiusKey, (fun target v -> target.CornerRadius <- v))
source.UpdatePrimitive(prev, target, backgroundGradientAngleKey, (fun target v -> target.BackgroundGradientAngle <- v))


ViewElement.Create<Xamarin.Forms.PancakeView.PancakeView>(create, update, attribs)
let updateAttachedProperties _ _ _ _ = ()

ViewElement.Create<Xamarin.Forms.PancakeView.PancakeView>(create, update, updateAttachedProperties, attribs)
Loading

0 comments on commit 44b6cca

Please sign in to comment.