pjfanning commented on PR #371:
URL: https://github.com/apache/incubator-pekko/pull/371#issuecomment-1593015247
I tried a solution with an AtomicReference around the state but its
performance was slower than the synchronization and Semaphore locking variants.
Approximately in SecureRandom territory in the 8 threads microbenchmark.
```
private val state = {
val (index, increment) = {
val r = new SecureRandom()
(r.nextLong, r.nextLong())
}
new AtomicReference(Array(index, increment, 1))
}
@tailrec
final def nextId(): Short = {
val array = state.get()
var index = array(0)
var increment = array(1)
var count = array(2)
val result = (0xFFFFFFFF & index).asInstanceOf[Short]
index -= increment
// Incorporate the counter into the increment to create a
// tetrahedral number additional term.
increment -= {
count += 1
count - 1
}
if (state.compareAndSet(array, Array(index, increment, count))) {
result
} else {
nextId()
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]