From 7cad516415c5cb8e63464b2e6f7b642a7d500997 Mon Sep 17 00:00:00 2001 From: Ne_Eo Date: Fri, 6 Sep 2024 03:20:20 +0200 Subject: [PATCH 1/2] Improve Timers --- src/haxe/Timer.hx | 14 +------------- .../_internal/backend/native/NativeApplication.hx | 9 +++++---- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/haxe/Timer.hx b/src/haxe/Timer.hx index 1b80cf5ed0..425eab35fd 100644 --- a/src/haxe/Timer.hx +++ b/src/haxe/Timer.hx @@ -270,19 +270,7 @@ class Timer public function stop():Void { - if (mRunning) - { - mRunning = false; - - for (i in 0...sRunningTimers.length) - { - if (sRunningTimers[i] == this) - { - sRunningTimers[i] = null; - break; - } - } - } + mRunning = false; } @:noCompletion private function __check(inTime:Float) diff --git a/src/lime/_internal/backend/native/NativeApplication.hx b/src/lime/_internal/backend/native/NativeApplication.hx index 2208624bfb..c5db23e460 100644 --- a/src/lime/_internal/backend/native/NativeApplication.hx +++ b/src/lime/_internal/backend/native/NativeApplication.hx @@ -96,7 +96,8 @@ class NativeApplication var offset = System.getTimer() - pauseTimer; for (i in 0...Timer.sRunningTimers.length) { - if (Timer.sRunningTimers[i] != null) Timer.sRunningTimers[i].mFireAt += offset; + var timer = Timer.sRunningTimers[i]; + if (timer != null && timer.mRunning) timer.mFireAt += offset; } pauseTimer = -1; } @@ -585,9 +586,9 @@ class NativeApplication { timer = Timer.sRunningTimers[i]; - if (timer != null) + if (timer != null && timer.mRunning) { - if (timer.mRunning && currentTime >= timer.mFireAt) + if (currentTime >= timer.mFireAt) { timer.mFireAt += timer.mTime; timer.run(); @@ -603,7 +604,7 @@ class NativeApplication { Timer.sRunningTimers = Timer.sRunningTimers.filter(function(val) { - return val != null; + return val != null && val.mRunning; }); } } From 3554bfe817244e7a76942ee90b2c2dbf2b7a8e8a Mon Sep 17 00:00:00 2001 From: player-03 Date: Thu, 5 Sep 2024 22:52:24 -0400 Subject: [PATCH 2/2] Simplify timer logic. We aren't using `i`, so we can iterate over the array normally. And there are no longer null entries, so we don't have to check for those. --- .../backend/native/NativeApplication.hx | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/lime/_internal/backend/native/NativeApplication.hx b/src/lime/_internal/backend/native/NativeApplication.hx index c5db23e460..d4e2813adb 100644 --- a/src/lime/_internal/backend/native/NativeApplication.hx +++ b/src/lime/_internal/backend/native/NativeApplication.hx @@ -94,10 +94,9 @@ class NativeApplication if (pauseTimer > -1) { var offset = System.getTimer() - pauseTimer; - for (i in 0...Timer.sRunningTimers.length) + for (timer in Timer.sRunningTimers) { - var timer = Timer.sRunningTimers[i]; - if (timer != null && timer.mRunning) timer.mFireAt += offset; + if (timer.mRunning) timer.mFireAt += offset; } pauseTimer = -1; } @@ -579,14 +578,11 @@ class NativeApplication if (Timer.sRunningTimers.length > 0) { var currentTime = System.getTimer(); - var foundNull = false; - var timer; + var foundStopped = false; - for (i in 0...Timer.sRunningTimers.length) + for (timer in Timer.sRunningTimers) { - timer = Timer.sRunningTimers[i]; - - if (timer != null && timer.mRunning) + if (timer.mRunning) { if (currentTime >= timer.mFireAt) { @@ -596,15 +592,15 @@ class NativeApplication } else { - foundNull = true; + foundStopped = true; } } - if (foundNull) + if (foundStopped) { Timer.sRunningTimers = Timer.sRunningTimers.filter(function(val) { - return val != null && val.mRunning; + return val.mRunning; }); } }