summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-06-20 23:20:20 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-06-21 12:19:43 +0200
commit629a8520c7611f61a513458961a08ae7494158ab (patch)
tree342c1c8384b064c35aa14437d9243036cabe1e26
parentd54ac10449083a490e741d6c54e6f3aa07b71af0 (diff)
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.
-rw-r--r--opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt45
1 files 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
@@ -348,25 +348,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.
*/
private fun scheduleUpdate() {
@@ -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
}