mdedetrich commented on code in PR #371:
URL: https://github.com/apache/incubator-pekko/pull/371#discussion_r1231896345


##########
actor/src/main/scala/org/apache/pekko/util/RandomShortProvider.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * license agreements; and to You under the Apache License, version 2.0:
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * This file is part of the Apache Pekko project, derived from Akka.
+ */
+
+package org.apache.pekko.util
+
+import org.apache.pekko.annotation.InternalApi
+
+import java.util.Random
+
+/**
+ * An efficient thread safe generator of pseudo random shorts based on
+ * https://en.wikipedia.org/wiki/Double_hashing#Enhanced_double_hashing.
+ *
+ * Note that due to the usage of synchronized this method is optimized
+ * for the happy case (i.e. least contention) on multiple threads.
+ */
+
+@InternalApi
+private[pekko] class RandomShortProvider(seed: Random) {
+  // These methods are volatile so that they stay within cache
+  private var index = seed.nextLong
+  private var increment = seed.nextLong
+  private var count = 1L
+
+  def nextId(): Short = synchronized {
+    val result = (0xFFFFFFFF & index).asInstanceOf[Short]

Review Comment:
   So I have just update the version to use a semaphore as well as making the 
lock timeout configurable plus I did some other improvements to make the config 
reading/parsing consistent with the rest of Pekko.
   
   I used the exact same version that you suggested (with a minor performance 
improvement to avoid creating a new class in the failure case of acquiring a 
lock) and the performance nosedived.
   
   ```
   [info] IdGeneratorBanchmark.measureEnhancedDoubleHash                 thrpt  
  3  ≈ 10⁻¹⁰            ops/ns
   [info] IdGeneratorBanchmark.measureSecureRandom                       thrpt  
  3    0,001 ±   0,001  ops/ns
   [info] IdGeneratorBanchmark.measureThreadLocalRandom                  thrpt  
  3    0,311 ±   0,031  ops/ns
   [info] IdGeneratorBanchmark.multipleThreadsMeasureEnhancedDoubleHash  thrpt  
  3  ≈ 10⁻¹⁰            ops/ns
   [info] IdGeneratorBanchmark.multipleThreadsMeasureSecureRandom        thrpt  
  3    0,001 ±   0,001  ops/ns
   [info] IdGeneratorBanchmark.multipleThreadsMeasureThreadLocalRandom   thrpt  
  3    0,608 ±   0,036  ops/ns
   ```



-- 
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]

Reply via email to