Skip to content

Commit b6ee487

Browse files
committed
Improve performances by reducing contention (#13)
1 parent de923dd commit b6ee487

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

zio-uuid/src/main/scala/zio/uuid/internals/UUIDGeneratorBuilder.scala

+22-22
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ private[zio] object UUIDGeneratorBuilder {
2121
builder: UUIDBuilder[UUIDvX],
2222
): UIO[UUIDvX] =
2323
for {
24-
random <- random.nextLong
25-
uuid <- mutex.withPermit {
26-
for {
27-
timestamp <- clock.currentTime(TimeUnit.MILLISECONDS)
28-
modifiedState <- state.modify { currentState =>
29-
// currentTime clock may run backward
30-
val actualTimestamp = Math.max(currentState.lastUsedEpochMillis, timestamp)
31-
val sequence =
32-
if (currentState.lastUsedEpochMillis == actualTimestamp) {
33-
currentState.sequence + 1
34-
} else 0L
35-
36-
val newState = GeneratorState(lastUsedEpochMillis = actualTimestamp, sequence = sequence)
37-
(newState, newState)
38-
}
39-
} yield builder(
40-
modifiedState.lastUsedEpochMillis,
41-
modifiedState.sequence,
42-
random,
43-
)
44-
}
45-
} yield uuid
24+
modifiedState <- mutex.withPermit {
25+
for {
26+
timestamp <- clock.currentTime(TimeUnit.MILLISECONDS)
27+
modifiedState <- state.modify { currentState =>
28+
// currentTime clock may run backward
29+
val actualTimestamp = Math.max(currentState.lastUsedEpochMillis, timestamp)
30+
val sequence =
31+
if (currentState.lastUsedEpochMillis == actualTimestamp) {
32+
currentState.sequence + 1
33+
} else 0L
34+
35+
val newState = GeneratorState(lastUsedEpochMillis = actualTimestamp, sequence = sequence)
36+
(newState, newState)
37+
}
38+
} yield modifiedState
39+
}
40+
random <- random.nextLong
41+
} yield builder(
42+
modifiedState.lastUsedEpochMillis,
43+
modifiedState.sequence,
44+
random,
45+
)
4646

4747
// noinspection YieldingZIOEffectInspection
4848
def buildGenerator[UUIDvX](builder: UUIDBuilder[UUIDvX]): UIO[UIO[UUIDvX]] =

0 commit comments

Comments
 (0)