From 629a8520c7611f61a513458961a08ae7494158ab Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Sun, 20 Jun 2021 23:20:20 +0200 Subject: simulator: Optimize flag management of resource context This change optimizes the internal flag management used in the SimResourceContextImpl to use bitwise flags instead of enums. This approach simplifies the implementation immensely and reduces the number of branches. --- .../resources/impl/SimResourceContextImpl.kt | 45 +++++++--------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt index 237a2a77..5c3f95e8 100644 --- a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt +++ b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt @@ -93,7 +93,7 @@ internal class SimResourceContextImpl( /** * The update flag indicating why the update was triggered. */ - private var _flag: Flag = Flag.None + private var _flag: Int = 0 /** * The current pending update. @@ -123,7 +123,7 @@ internal class SimResourceContextImpl( return } - enableFlag(Flag.Interrupt) + _flag = _flag or FLAG_INTERRUPT scheduleUpdate() } @@ -132,7 +132,7 @@ internal class SimResourceContextImpl( return } - enableFlag(Flag.Invalidate) + _flag = _flag or FLAG_INVALIDATE scheduleUpdate() } @@ -149,7 +149,7 @@ internal class SimResourceContextImpl( */ fun requiresUpdate(timestamp: Long): Boolean { // Either the resource context is flagged or there is a pending update at this timestamp - return _flag != Flag.None || _pendingUpdate?.timestamp == timestamp + return _flag != 0 || _pendingUpdate?.timestamp == timestamp } /** @@ -161,7 +161,7 @@ internal class SimResourceContextImpl( val newState = doUpdate(timestamp, oldState) _state = newState - _flag = Flag.None + _flag = 0 when (newState) { SimResourceState.Pending -> @@ -198,7 +198,7 @@ internal class SimResourceContextImpl( // Resource context is not active, so its state will not update SimResourceState.Pending, SimResourceState.Stopped -> state SimResourceState.Active -> { - val isInterrupted = _flag == Flag.Interrupt + val isInterrupted = _flag and FLAG_INTERRUPT != 0 val remainingWork = getRemainingWork(timestamp) val isConsume = _limit > 0.0 @@ -347,25 +347,6 @@ internal class SimResourceContextImpl( } } - /** - * Enable the specified [flag] taking into account precedence. - */ - private fun enableFlag(flag: Flag) { - _flag = when (_flag) { - Flag.None -> flag - Flag.Invalidate -> - when (flag) { - Flag.None -> flag - else -> flag - } - Flag.Interrupt -> - when (flag) { - Flag.None, Flag.Invalidate -> flag - else -> flag - } - } - } - /** * Schedule an update for this resource context. */ @@ -402,12 +383,12 @@ internal class SimResourceContextImpl( } /** - * An enumeration of flags that can be assigned to a resource context to indicate whether they are invalidated or - * interrupted. + * A flag to indicate that the context should be invalidated. */ - enum class Flag { - None, - Interrupt, - Invalidate - } + private val FLAG_INVALIDATE = 0b01 + + /** + * A flag to indicate that the context should be interrupted. + */ + private val FLAG_INTERRUPT = 0b10 } -- cgit v1.2.3