Skip to content

Commit

Permalink
perf: Remove DateTimeOffset from TimeLine
Browse files Browse the repository at this point in the history
Removes the use of DateTimeOffset in TimeLine and derivatives
as the time is used to build a delta, not the absolute time.
This sames time at startup, avoiding the initialization of DateTimeOffset
and later to get the full date which is not use.
  • Loading branch information
jeromelaban committed Sep 7, 2021
1 parent 1e49c0f commit 96585ff
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
5 changes: 4 additions & 1 deletion build/PackageDiffIgnore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3929,7 +3929,10 @@
<Member
fullName="System.Void Windows.Devices.Bluetooth.BluetoothLEAppearance..ctor()"
reason="Doesn't exist in UWP" />


<Member
fullName="System.Boolean Windows.UI.Xaml.Media.Animation.Timeline.NeedsRepeat(System.DateTimeOffset lastBeginTime, System.Int32 replayCount)"
reason="Doesn't exist in UWP" />

<Member
fullName="System.Void Windows.UI.Notifications.BadgeUpdater..ctor()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
using Uno.Disposables;
using Uno.Extensions;
using Uno.Logging;
using System.Diagnostics;

namespace Windows.UI.Xaml.Media.Animation
{
[ContentProperty(Name = "KeyFrames")]
partial class ColorAnimationUsingKeyFrames : Timeline, ITimeline
{
private DateTimeOffset _lastBeginTime;
private readonly Stopwatch _activeDuration = new Stopwatch();
private int _replayCount = 1;
private ColorOffset? _startingValue = null;
private ColorOffset _finalValue;
Expand Down Expand Up @@ -95,7 +96,7 @@ void ITimeline.Begin()
_wasBeginScheduled = false;
_subscriptions.Clear(); //Dispose all and start a new

_lastBeginTime = DateTimeOffset.Now;
_activeDuration.Restart();
_replayCount = 1;

//Start the animation
Expand Down Expand Up @@ -351,7 +352,7 @@ private void Replay()
private void OnEnd()
{
// If the animation was GPU based, remove the animated value
if (NeedsRepeat(_lastBeginTime, _replayCount))
if (NeedsRepeat(_activeDuration, _replayCount))
{
Replay(); // replay the animation
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
using Uno.Extensions;
using Windows.UI.Core;
using Uno.Logging;
using System.Diagnostics;

namespace Windows.UI.Xaml.Media.Animation
{
[ContentProperty(Name = "KeyFrames")]
public partial class DoubleAnimationUsingKeyFrames : Timeline, ITimeline
{
private DateTimeOffset _lastBeginTime;
private readonly Stopwatch _activeDuration = new Stopwatch();
private int _replayCount = 1;
private double? _startingValue = null;
private double _finalValue;
Expand Down Expand Up @@ -78,7 +79,7 @@ void ITimeline.Begin()
_wasBeginScheduled = false;
_subscriptions.Clear(); //Dispose all and start a new

_lastBeginTime = DateTimeOffset.Now;
_activeDuration.Restart();
_replayCount = 1;

//Start the animation
Expand Down Expand Up @@ -315,7 +316,7 @@ private void Replay()
private void OnEnd()
{
// If the animation was GPU based, remove the animated value
if (NeedsRepeat(_lastBeginTime, _replayCount))
if (NeedsRepeat(_activeDuration, _replayCount))
{
Replay(); // replay the animation
return;
Expand Down
7 changes: 4 additions & 3 deletions src/Uno.UI/UI/Xaml/Media/Animation/Storyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using Windows.UI.Core;
using Uno.Disposables;
using System.Diagnostics;

namespace Windows.UI.Xaml.Media.Animation
{
Expand All @@ -29,7 +30,7 @@ public static class TraceProvider
public const int StoryBoard_Resume = 4;
}

private DateTimeOffset _lastBeginTime;
private readonly Stopwatch _activeDuration = new Stopwatch();
private int _replayCount = 1;
private int _runningChildren = 0;
private bool _hasFillingChildren = false;
Expand Down Expand Up @@ -110,7 +111,7 @@ public void Begin()
State = TimelineState.Active;
_hasFillingChildren = false;
_replayCount = 1;
_lastBeginTime = DateTimeOffset.Now;
_activeDuration.Restart();

Play();
}
Expand Down Expand Up @@ -340,7 +341,7 @@ private void Child_Completed(object sender, object e)

if (_runningChildren == 0)
{
if (NeedsRepeat(_lastBeginTime, _replayCount))
if (NeedsRepeat(_activeDuration, _replayCount))
{
Replay(); // replay the animation
return;
Expand Down
9 changes: 5 additions & 4 deletions src/Uno.UI/UI/Xaml/Media/Animation/Timeline.animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Windows.UI.Core;
using Uno.Logging;
using Uno.UI.DataBinding;
using System.Diagnostics;

namespace Windows.UI.Xaml.Media.Animation
{
Expand All @@ -30,7 +31,7 @@ public static class TraceProvider
public const int Resume = 4;
}

private DateTimeOffset _lastBeginTime;
private readonly Stopwatch _activeDuration = new Stopwatch();
private int _replayCount = 1;
private T? _startingValue = null;
private T? _endValue = null;
Expand Down Expand Up @@ -71,7 +72,7 @@ private TimelineState State
private string[] GetTraceProperties() => _owner?.GetTraceProperties();
private void ClearValue() => _owner?.ClearValue();
private void SetValue(object value) => _owner?.SetValue(value);
private bool NeedsRepeat(DateTimeOffset lastBeginTime, int replayCount) => _owner?.NeedsRepeat(lastBeginTime, replayCount) ?? false;
private bool NeedsRepeat(Stopwatch activeDuration, int replayCount) => _owner?.NeedsRepeat(activeDuration, replayCount) ?? false;
private object GetValue() => _owner?.GetValue();

public void Begin()
Expand All @@ -89,7 +90,7 @@ public void Begin()

_subscriptions.Clear(); //Dispose all and start a new

_lastBeginTime = DateTimeOffset.Now;
_activeDuration.Restart();
_replayCount = 1;

//Start the animation
Expand Down Expand Up @@ -330,7 +331,7 @@ private void SetAnimatorDuration()
private void OnEnd()
{
// If the animation was GPU based, remove the animated value
if (NeedsRepeat(_lastBeginTime, _replayCount))
if (NeedsRepeat(_activeDuration, _replayCount))
{
Replay(); // replay the animation
return;
Expand Down
5 changes: 3 additions & 2 deletions src/Uno.UI/UI/Xaml/Media/Animation/Timeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using Windows.UI.Core;
using Windows.UI.Xaml.Data;
using System.Diagnostics;

namespace Windows.UI.Xaml.Media.Animation
{
Expand Down Expand Up @@ -322,9 +323,9 @@ void ITimeline.Deactivate()
/// Checks if the Timeline will repeat.
/// </summary>
/// <returns><c>true</c>, Repeat needed, <c>false</c> otherwise.</returns>
protected bool NeedsRepeat(DateTimeOffset lastBeginTime, int replayCount)
private protected bool NeedsRepeat(Stopwatch duration, int replayCount)
{
var totalTime = DateTimeOffset.Now - lastBeginTime;
var totalTime = duration.Elapsed;

//3 types of repeat behavors,
return ((RepeatBehavior.Type == RepeatBehaviorType.Forever) // Forever: Will always repeat the Timeline
Expand Down

0 comments on commit 96585ff

Please sign in to comment.