Skip to content

Fix: Binding doesn't respect special IValueConverter return values - #12060

Closed
bakerhillpins wants to merge 16 commits into
dotnet:mainfrom
bakerhillpins:fix-11740
Closed

bakerhillpins wants to merge 16 commits into
dotnet:mainfrom
bakerhillpins:fix-11740

Conversation

@bakerhillpins

@bakerhillpins bakerhillpins commented Dec 13, 2022 •

Copy link
Copy Markdown

Description of Change

Update Binding classes in MAUI to support special return values from the IValueConverter assigned to the Binding.Converter property.

Make changes to stop Bindings from being resolved multiple times during the course of a single update or change to the children of the View.

I've modified the MAUI source to support the both the Binding.DoNothing and the BindableProperty.UnsetValue functionality described in the IValueConverter and IMultiValueConverter docs. These changes support both the Binding Fallbacks and the MultiBinding discussions in the docs and this would then operate as WPF does.

The one omission here is it's unclear how the StringFormat behavior should be handled as it's not specifically discussed anywhere in MAUI or WPF documentation that I can find. Discussions on StackOverflow indicate that WPF bindings ignore StringFormat during application of TargetNullValue or FallbackValue and so I'm assuming that MAUI should keep this and I've made changes to support that.

It should also be noted that IValueConverter.ConvertBack discusses using Fallback and Targetnull Values however the documentation for these clearly states it's applied to Source values only:

The TargetNullValue property allows a replacement value to be defined that will be used when the binding source is resolved, but the value is null.

I originally targeted the NET6.0 branch because the documentation (until yesterday morning) specifically said to do this if you expected this to be applied to NET6. I have that branch available if needed.

Issues Fixed

fixes #11740
fixes #10792
fixes #10806

Unit tests that support Binding IValueConverter converters that return Binding.DoNothing and BindableProperty.UnsetValue.
This is a first pass at updating the Binding classes to support the IValueConverter and IMultiValueConverter as spec'd on the desktop. Issues remain with over processing these return values, for example they can get picked up by StringFormat when they should not.
Fix: Allow FallbackValue to be replaced with default value where necessary.
Process return values from GetSourceValue.
Don't update MultiBindings unless there's been a change in one of the included bindings.
Use same implementation as MultiBinding. IValueConverter.ConvertBack talks about using FallbackValue and NulltargetValue with the special return values but the documentation clearly states that those 2 properties on a Binding are only applied to Source value operations.
@ghost ghost added the community ✨ Community Contribution label Dec 13, 2022
@ghost

ghost commented Dec 13, 2022

Copy link
Copy Markdown

Hey there @bakerhillpins! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@rmarinho

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

DRY - use common method for Apply to remove duplicated code.
Rework flow so that FallbackValue is only calculated for a from source Binding operation when it's needed (rather than as a default value initialization).
@bakerhillpins

bakerhillpins commented Dec 14, 2022 •

Copy link
Copy Markdown
Author

Hope I didn't miss the pipeline run. I made some changes for DRY and optimization of TypedBinding. I realize that performance measurements are VERY subjective but I ran a few cycles on both the original code (main branch) and on my most recent updates to be sure that I wasn't mucking up the works. This is what I got for your reference. I'd be interested to know what you folks see in your testing.

fix-11740 branch:

      SpeedTestApply Failed: Applying 100000 Typedbindings			: 29ms.
Xunit.Sdk.XunitException
Applying 100000 Typedbindings			: 29ms.
Applying 100000 Typedbindings (without INPC)	: 16ms.
Applying 100000 Bindings			: 82ms.
Setting  100000 values				: 7ms.
   at Microsoft.Maui.Controls.Core.UnitTests.TypedBindingUnitTests.SpeedTestApply() in D:\Repos\Forks\maui\src\Controls\tests\Core.UnitTests\TypedBindingUnitTests.cs:line 1999
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)


      SpeedTestSetBC Failed: Setting BC for 100000 Typedbindings			: 116ms.
Xunit.Sdk.XunitException
Setting BC for 100000 Typedbindings			: 116ms.
Setting BC for 100000 Typedbindings (without INPC)	: 92ms.
Setting BC for 100000 Bindings				: 154ms.
Setting  100000 values					: 7ms.
   at Microsoft.Maui.Controls.Core.UnitTests.TypedBindingUnitTests.SpeedTestSetBC() in D:\Repos\Forks\maui\src\Controls\tests\Core.UnitTests\TypedBindingUnitTests.cs:line 2065
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

main branch:

      SpeedTestApply Failed: Applying 100000 Typedbindings			: 34ms.
Xunit.Sdk.XunitException
Applying 100000 Typedbindings			: 34ms.
Applying 100000 Typedbindings (without INPC)	: 18ms.
Applying 100000 Bindings			: 93ms.
Setting  100000 values				: 9ms.
   at Microsoft.Maui.Controls.Core.UnitTests.TypedBindingUnitTests.SpeedTestApply() in D:\Repos\Forks\maui\src\Controls\tests\Core.UnitTests\TypedBindingUnitTests.cs:line 1655
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)


      SpeedTestSetBC Failed: Setting BC for 100000 Typedbindings			: 129ms.
Xunit.Sdk.XunitException
Setting BC for 100000 Typedbindings			: 129ms.
Setting BC for 100000 Typedbindings (without INPC)	: 113ms.
Setting BC for 100000 Bindings				: 171ms.
Setting  100000 values					: 9ms.
   at Microsoft.Maui.Controls.Core.UnitTests.TypedBindingUnitTests.SpeedTestSetBC() in D:\Repos\Forks\maui\src\Controls\tests\Core.UnitTests\TypedBindingUnitTests.cs:line 1721
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

@hartez hartez added t/enhancement ☀️ New feature or request and removed t/enhancement ☀️ New feature or request labels Dec 14, 2022
@mattleibow mattleibow added area-xaml XAML, CSS, Triggers, Behaviors legacy-area-perf Startup / Runtime performance labels Dec 16, 2022
@mattleibow

Copy link
Copy Markdown
Member

Adding the perf label as well as this does have some exciting changes. @jonathanpeppers not sure if you are interested in checking this as well?

@mattleibow

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@bakerhillpins

bakerhillpins commented Dec 16, 2022 •

Copy link
Copy Markdown
Author

FYI, I understand the problem associated with #10792 and have a fix. I believe I understand #10806 as well but still investigating. May be the same fix for both.

Be interested to know if you want separate PRs or include in this.

@StephaneDelcroix

Copy link
Copy Markdown
Contributor

does this cover compiled bindings too ?

@bakerhillpins

bakerhillpins commented Dec 16, 2022 •

Copy link
Copy Markdown
Author

does this cover compiled bindings too ?

yes, Note the "performance" post above.

@jonathanpeppers jonathanpeppers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all performance changes, we should add a benchmark here:

https://github.com/dotnet/maui/tree/main/src/Core/tests/Benchmarks

A new benchmark comparing the different binding modes, would be great!

This way we have numbers for how much this improves things. And we will know it for sure won't be worse on accident!

Comment thread src/Controls/src/Core/MultiBinding.cs Resolved
Comment thread src/Controls/src/Core/MultiBinding.cs Resolved
@bakerhillpins

bakerhillpins commented Dec 16, 2022 •

Copy link
Copy Markdown
Author

@jonathanpeppers

I didn't add perf code, I simply enabled the 2 perf tests in the TypedBindingTests.

There's room for more DRY changes. Specifically putting a subclass in between BindingBase and several of it's inheritors. This class would carry the common code for the Converter and ConverterParameter properties and the associated GetSourceValue/GetTargetValue methods which are identical. I wasn't sure if this was appropriate because someone commented out the call to the base class code in TypedBinding so I figured it was a change left out for performance reasons.

Should the subclass be added? This change would apparently also remove duplicate documentation code.

Remove the converters that were added as they duplicated functionality that already exists in existing implementation.  The TestConverterReturn class isn't actually needed.
Coalesce a context value from multiple sources as is done in Binding.Apply.  Include appropriate tests.
@bakerhillpins

Copy link
Copy Markdown
Author

Add fix and tests for #10792

@jfversluis

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@bakerhillpins

Copy link
Copy Markdown
Author

@jfversluis @StephaneDelcroix I can't express how disappointed I am that this is still waiting for a single reviewer. I'd happily update to resolve the conflicts that are now blocking.

@jonathanpeppers

Copy link
Copy Markdown
Member

@bakerhillpins you didn't address my comment, if you could add a benchmark if this supposedly improves performance: #12060 (review)

By how much does it improve things?

There is also a merge conflict, if you'd like to address that. Thanks.

@bakerhillpins

Copy link
Copy Markdown
Author

@jonathanpeppers This PR is not and never was about changes for the sake of performance. The performance code mentioned is commented out in the MS source. I simply ran it to see that I hadn't regressed and mentioned that in my response to you. The issues resolved are discussed in the PR's initial post.

I did however change the order of binding processing to fix an issue where bindings on an object were being evaluated 2x because property bindings were being evaluated before the BindingContext binding was on an object. Thus the bindings on that object would be evaluated a 2nd time when the BindingContext was applied. This included children bindings. #10806

There is also a merge conflict, if you'd like to address that. Thanks.

This has been sitting here for months without conflict waiting for review/comment at the beginning of which time I asked for feedback and received nothing. As I mentioned, I'm willing to take the time to fix the conflict but right now I don't feel like anyone is taking my time seriously.

@jonathanpeppers

Copy link
Copy Markdown
Member

@bakerhillpins but we don't have a BenchmarkDotNet benchmark of multi-binding. I would want to see the results of that before merging this.

Your description here seems like you did something manual instead of using BenchmarkDotNet:

#12060 (comment)

If you need an example, I wrote one recently in: #14830

Thanks.

@bakerhillpins

bakerhillpins commented May 1, 2023 •

Copy link
Copy Markdown
Author

@bakerhillpins but we don't have a BenchmarkDotNet benchmark of multi-binding. I would want to see the results of that before merging this.

Why? I made NO PERFORMANCE changes.

Your description here seems like you did something manual instead of using BenchmarkDotNet:

You don't appear to be reading what I'm writing and I don't know how to tell you any more clearly.

The "perfomance code" is in your binding unit tests and commented out. I didn't write it, I simply used it as a sanity check.

Conflicting files
src/Controls/src/Core/BindableObject.cs
@samhouts samhouts added this to the Under Consideration milestone Jul 31, 2023
@samhouts samhouts added the stale Indicates a stale issue/pr and will be closed soon label Sep 11, 2023
@berhir

berhir commented Sep 29, 2023

Copy link
Copy Markdown

What is the status of this PR?
I ran today into the issue with Binding.DoNothing and found this PR that was created a long time ago.

I can feel the pain and frustration of the creator who obviously put a lot of time and thinking into this PR.
The PR fixes 3 issues and now it was marked as stale... why is no one reviewing it? @jfversluis @mattleibow @samhouts

@bakerhillpins

bakerhillpins commented Sep 29, 2023 •

Copy link
Copy Markdown
Author

What is the status of this PR? I ran today into the issue with Binding.DoNothing and found this PR that was created a long time ago.

I can feel the pain and frustration of the creator who obviously put a lot of time and thinking into this PR. The PR fixes 3 issues and now it was marked as stale... why is no one reviewing it? @jfversluis @mattleibow @samhouts

@berhir
First off, thank you for recognizing my frustration. As for the PR, It's basically dead. I put many hours (including personal time) into this and they have zero interest. And yes I'm bent over it. The goalposts kept moving during the process and I'm done chasing. It appears they've moved yet again based upon the "Some Checks" section.

MS puts on a good show with open source and asking for PRs/community development but they don't appear to back it up. I doubt it's a personal thing, based upon the release cycle and quality I'm seeing they are pushing too hard/fast. It's the early days of .NET all over again, unfortunately. Though YMMV.

@MartyIX

MartyIX commented Oct 18, 2023

Copy link
Copy Markdown
Contributor

I'm just a passerby interested in #10806 being fixed.

@bakerhillpins: Your PR is exciting! I read the posts here and it looks to me that @jonathanpeppers just wants to know that this PR does not introduce a performance regressions as src/Controls/src/Core/BindableObject.cs is an important piece of MAUI.

Maybe a benchmark similar to this one #17756 (comment) would be sufficient to persuade people that no such regression occurred. Maybe @albyrock87 can share some benchmarking code to make it easier to measure it.

One additional thought is that if this PR can be split into multiple smaller PRs, it might be easier to review it. 1200 LOC is big.

Not trying to offend you even more, just sharing my honest opinion.

@bakerhillpins

bakerhillpins commented Oct 18, 2023 •

Copy link
Copy Markdown
Author

I'm just a passerby interested in #10806 being fixed.

@bakerhillpins: Your PR is exciting! I read the posts here and it looks to me that @jonathanpeppers just wants to know that this PR does not introduce a performance regressions as src/Controls/src/Core/BindableObject.cs is an important piece of MAUI.

Thank you for noticing.

From my perspective it's all bug fixes and always was.. MS made an assumption it was performance related because I ran some of MS's own "performance" code and then randomly labeled it as perf related. It would seem that no one read the 3 referenced bug reports that were fixed to determine what the PR was about.

The only "performance" change was a bug fix (#10806) where bindings are being executed 2x down the entire xaml tree. The 1st time is with the incorrect BindingContext value.

Maybe a benchmark similar to this one #17756 (comment) would be sufficient to persuade people that no such regression occurred. Maybe @albyrock87 can share some benchmarking code to make it easier to measure it.

Not going to be me.

One additional thought is that if this PR can be split into multiple smaller PRs, it might be easier to review it. 1200 LOC is big.

Most of the changes are TESTS that I added to pick up on all the missing functionality and bugs I found in the binding code.

Not trying to offend you even more, just sharing my honest opinion.

You did not, but Thank you for that. I as well.

@Alex-Dobrynin

This comment was marked as off-topic.

@Eilon Eilon added perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) and removed legacy-area-perf Startup / Runtime performance labels May 10, 2024
@PureWeen PureWeen removed this from the Under Consideration milestone Aug 2, 2024
@jbtdevgit

This comment was marked as off-topic.

@albyrock87

albyrock87 commented Sep 6, 2024 •

Copy link
Copy Markdown
Contributor

Hey @bakerhillpins ,
my PR #24553 has been merged and it includes a slightly different fix for #10806 compared to what you have here.

As you can see I tried at all costs to avoid OrderBy to sort properties for two reasons:

  1. Performance
  2. It changes the order used by the developer to set bindings on the target causing potential unwanted side effects on interdependent properties

I wanted to help by preparing a PR targeting your repo to merge main into your branch and then solve the conflicts I caused.
Unfortunately I failed, because in the mean time SetterSpecificity came into the game within binding, so your code appears to not be compatible with current state of main.

Maybe I'm missing something, but I wanted to tell you that I tried :) and what was the outcome.

Thanks for all the work you've done here.

@PureWeen PureWeen added the s/pr-needs-author-input PR needs an update from the author label Sep 6, 2024
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hi @bakerhillpins. We have added the "s/pr-needs-author-input" label to this issue, which indicates that we have an open question/action for you before we can take further action. This PRwill be closed automatically in 14 days if we do not hear back from you by then - please feel free to re-open it if you come back to this PR after that time.

@bakerhillpins

This comment was marked as off-topic.

@bakerhillpins

This comment was marked as off-topic.

@ncls-alien

This comment was marked as off-topic.

@github-actions github-actions Bot locked and limited conversation to collaborators Oct 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-xaml XAML, CSS, Triggers, Behaviors community ✨ Community Contribution perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) s/pr-needs-author-input PR needs an update from the author stale Indicates a stale issue/pr and will be closed soon

Projects

None yet