Skip to content

Add Bindable<T>.CopyTo and use in GetUnboundCopy implementation#5531

Merged
smoogipoo merged 5 commits intoppy:masterfrom
peppy:get-unbound-copy-optimisation
Nov 22, 2022
Merged

Add Bindable<T>.CopyTo and use in GetUnboundCopy implementation#5531
smoogipoo merged 5 commits intoppy:masterfrom
peppy:get-unbound-copy-optimisation

Conversation

@peppy
Copy link
Member

@peppy peppy commented Nov 22, 2022

This method is used heavily in AudioAdjustments, which is how I got here.

var aggregate = getAggregate(type) = new AggregateBindable<double>(getAggregateFunction(type), getProperty(type).GetUnboundCopy());

Method Mean Error StdDev Gen 0 Allocated
GetUnboundCopyOld 940.6 ns 6.14 ns 5.13 ns 0.0629 736 B
GetUnboundCopyNew 38.95 ns 0.848 ns 1.160 ns 0.0153 176 B

Breaking Changes

Bindable<T>.CopyTo should be implemented for any custom bindables

To improve the performance of creating clones of bindables (ie. via GetUnboundCopy()), the copy portion of BindTo() has been split out into its own method. If you have any custom bindable types which were copying values across within a BindTo() override, please move the copy operation to CopyTo() instead. Pay special attention to the direction of assignment, which will reverse from what it was in BindTo.

A fix should look something like this:

diff --git a/osu.Framework/Bindables/BindableNumber.cs b/osu.Framework/Bindables/BindableNumber.cs
index 6d605667f..430da79d0 100644
--- a/osu.Framework/Bindables/BindableNumber.cs
+++ b/osu.Framework/Bindables/BindableNumber.cs
@@ -199,12 +199,12 @@ protected void TriggerPrecisionChange(BindableNumber<T> source = null, bool prop
                 PrecisionChanged?.Invoke(precision);
         }
 
-        public override void BindTo(Bindable<T> them)
+        public override void CopyTo(Bindable<T> them)
         {
             if (them is BindableNumber<T> other)
-                Precision = other.Precision;
+                other.Precision = Precision;
 
-            base.BindTo(them);
+            base.CopyTo(them);
         }
 
         public override void UnbindEvents()

@peppy peppy force-pushed the get-unbound-copy-optimisation branch 3 times, most recently from a35029f to 3f1722d Compare November 22, 2022 04:48
@peppy peppy force-pushed the get-unbound-copy-optimisation branch from 3f1722d to 0750864 Compare November 22, 2022 04:51
@peppy
Copy link
Member Author

peppy commented Nov 22, 2022

osu! side changes required: peppy/osu@1632df1

Seems to work okay.

peppy and others added 2 commits November 22, 2022 14:52
@smoogipoo smoogipoo enabled auto-merge November 22, 2022 08:07
@smoogipoo smoogipoo merged commit 30c9f4a into ppy:master Nov 22, 2022
@peppy peppy deleted the get-unbound-copy-optimisation branch November 25, 2022 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants