Changeset: 265dfc40540c for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/265dfc40540c
Modified Files:
        gdk/gdk_sample.c
Branch: default
Log Message:

Generate a list of random numbers before checking for duplicates.
This means we quickly generate the numbers with a single locking
operation instead of locking and unlocking all the time.


diffs (64 lines):

diff --git a/gdk/gdk_sample.c b/gdk/gdk_sample.c
--- a/gdk/gdk_sample.c
+++ b/gdk/gdk_sample.c
@@ -129,28 +129,40 @@ do_batsample(BAT *b, BUN n, random_state
                        return NULL;
                }
 
-               /* while we do not have enough sample OIDs yet */
+               /* generate a list of random numbers; note we use the
+                * "tree" array, but we use the value from each location
+                * before it is overwritten by the use as part of the
+                * binary tree */
                if (lock) {
-                       /* serialize access to random state engine */
-                       for (rescnt = 0; rescnt < n; rescnt++) {
-                               oid candoid;
-                               do {
-                                       MT_lock_set(lock);
-                                       candoid = minoid + next(rse) % cnt;
-                                       MT_lock_unset(lock);
-                                       /* if that candidate OID was already
-                                        * generated, try again */
-                               } while (!OIDTreeMaybeInsert(tree, candoid, 
rescnt));
-                       }
+                       MT_lock_set(lock);
+                       for (rescnt = 0; rescnt < n; rescnt++)
+                               tree[rescnt].o = next(rse);
+                       MT_lock_unset(lock);
                } else {
-                       for (rescnt = 0; rescnt < n; rescnt++) {
-                               oid candoid;
-                               do {
-                                       candoid = minoid + next(rse) % cnt;
-                                       /* if that candidate OID was already
-                                        * generated, try again */
-                               } while (!OIDTreeMaybeInsert(tree, candoid, 
rescnt));
-                       }
+                       for (rescnt = 0; rescnt < n; rescnt++)
+                               tree[rescnt].o = next(rse);
+               }
+
+               /* while we do not have enough sample OIDs yet */
+               BUN rnd = 0;
+               for (rescnt = 0; rescnt < n; rescnt++) {
+                       oid candoid;
+                       do {
+                               if (rnd == n) {
+                                       /* we ran out of random numbers,
+                                        * so generate more */
+                                       if (lock)
+                                               MT_lock_set(lock);
+                                       for (rnd = rescnt; rnd < n; rnd++)
+                                               tree[rnd].o = next(rse);
+                                       if (lock)
+                                               MT_lock_unset(lock);
+                                       rnd = rescnt;
+                               }
+                               candoid = minoid + tree[rnd++].o % cnt;
+                               /* if that candidate OID was already
+                                * generated, try again */
+                       } while (!OIDTreeMaybeInsert(tree, candoid, rescnt));
                }
                if (!antiset) {
                        OIDTreeToBAT(tree, bn);
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to