diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-06-20 22:23:31 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-06-21 12:19:42 +0200 |
| commit | 882ae4a9830737ece2db9563d0f56387036a8e3d (patch) | |
| tree | 7b4b126dfa28557edeb0ed440c1610e0e71db1c3 /opendc-simulator/opendc-simulator-resources | |
| parent | df3c9dc3fcd2f89910575bfdc24a3db3af9eba0f (diff) | |
simulator: Optimize access to remainingWork property
This change updates the SimResourceContextImpl to optimize the access to
the remainingWork property, which is required by many calls in the hot
path.
Diffstat (limited to 'opendc-simulator/opendc-simulator-resources')
| -rw-r--r-- | opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt | 47 |
1 files changed, 19 insertions, 28 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 46c5c63f..237a2a77 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 @@ -39,7 +39,8 @@ internal class SimResourceContextImpl( * The clock of the context. */ override val clock: Clock - get() = interpreter.clock + get() = _clock + private val _clock = interpreter.clock /** * The capacity of the resource. @@ -59,18 +60,7 @@ internal class SimResourceContextImpl( * The amount of work still remaining at this instant. */ override val remainingWork: Double - get() { - val now = clock.millis() - - return if (_remainingWorkFlush < now) { - _remainingWorkFlush = now - computeRemainingWork(now).also { _remainingWork = it } - } else { - _remainingWork - } - } - private var _remainingWork: Double = 0.0 - private var _remainingWorkFlush: Long = Long.MIN_VALUE + get() = getRemainingWork(_clock.millis()) /** * A flag to indicate the state of the context. @@ -92,20 +82,6 @@ internal class SimResourceContextImpl( override val demand: Double get() = _limit - private val counters = object : SimResourceCounters { - override var demand: Double = 0.0 - override var actual: Double = 0.0 - override var overcommit: Double = 0.0 - - override fun reset() { - demand = 0.0 - actual = 0.0 - overcommit = 0.0 - } - - override fun toString(): String = "SimResourceCounters[demand=$demand,actual=$actual,overcommit=$overcommit]" - } - /** * The current state of the resource context. */ @@ -223,7 +199,7 @@ internal class SimResourceContextImpl( SimResourceState.Pending, SimResourceState.Stopped -> state SimResourceState.Active -> { val isInterrupted = _flag == Flag.Interrupt - val remainingWork = remainingWork + val remainingWork = getRemainingWork(timestamp) val isConsume = _limit > 0.0 // Update the resource counters only if there is some progress @@ -325,6 +301,21 @@ internal class SimResourceContextImpl( */ private fun next(now: Long): SimResourceState = interpret(consumer.onNext(this), now) + private var _remainingWork: Double = 0.0 + private var _remainingWorkFlush: Long = Long.MIN_VALUE + + /** + * Obtain the remaining work at the given timestamp. + */ + private fun getRemainingWork(now: Long): Double { + return if (_remainingWorkFlush < now) { + _remainingWorkFlush = now + computeRemainingWork(now).also { _remainingWork = it } + } else { + _remainingWork + } + } + /** * Compute the remaining work based on the current state. */ |
