Skip to content

Commit

Permalink
Improve performances by reducing contention (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
guizmaii committed Aug 29, 2023
1 parent de923dd commit b6ee487
Showing 1 changed file with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ private[zio] object UUIDGeneratorBuilder {
builder: UUIDBuilder[UUIDvX],
): UIO[UUIDvX] =
for {
random <- random.nextLong
uuid <- mutex.withPermit {
for {
timestamp <- clock.currentTime(TimeUnit.MILLISECONDS)
modifiedState <- state.modify { currentState =>
// currentTime clock may run backward
val actualTimestamp = Math.max(currentState.lastUsedEpochMillis, timestamp)
val sequence =
if (currentState.lastUsedEpochMillis == actualTimestamp) {
currentState.sequence + 1
} else 0L

val newState = GeneratorState(lastUsedEpochMillis = actualTimestamp, sequence = sequence)
(newState, newState)
}
} yield builder(
modifiedState.lastUsedEpochMillis,
modifiedState.sequence,
random,
)
}
} yield uuid
modifiedState <- mutex.withPermit {
for {
timestamp <- clock.currentTime(TimeUnit.MILLISECONDS)
modifiedState <- state.modify { currentState =>
// currentTime clock may run backward
val actualTimestamp = Math.max(currentState.lastUsedEpochMillis, timestamp)
val sequence =
if (currentState.lastUsedEpochMillis == actualTimestamp) {
currentState.sequence + 1
} else 0L

val newState = GeneratorState(lastUsedEpochMillis = actualTimestamp, sequence = sequence)
(newState, newState)
}
} yield modifiedState
}
random <- random.nextLong
} yield builder(
modifiedState.lastUsedEpochMillis,
modifiedState.sequence,
random,
)

// noinspection YieldingZIOEffectInspection
def buildGenerator[UUIDvX](builder: UUIDBuilder[UUIDvX]): UIO[UIO[UUIDvX]] =
Expand Down

0 comments on commit b6ee487

Please sign in to comment.