Bug 518146 - Generate shorter GUIDs than nsIUUIDGenerator
authorEdward Lee <edilee@mozilla.com>
Thu, 24 Sep 2009 15:54:47 -0700
changeset 45771 09fe5a0ab1fcb4a4854114be02070586b2439cbf
parent 45770 b4d9f0ec3ddeaf697c42566f6dca31477b3d97c1
child 45772 a93c07f52847f55208eb3ead2bdd601ca0c84953
push id14033
push useredward.lee@engineering.uiuc.edu
push dateWed, 23 Jun 2010 22:21:35 +0000
treeherdermozilla-central@227db4ad8cdf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs518146
Bug 518146 - Generate shorter GUIDs than nsIUUIDGenerator Randomly generate 10 characters from a pool of 70 possible characters for an equivalent 61.29-bit GUID which should have around 1% chance of collision (within a collection) after ~100 million records.
services/sync/modules/util.js
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -144,19 +144,38 @@ let Utils = {
       // Expose the exception if something inside the batch failed
       if (batchEx!= null)
         throw batchEx;
     };
   },
 
   // Generates a brand-new globally unique identifier (GUID).
   makeGUID: function makeGUID() {
-    let uuidgen = Cc["@mozilla.org/uuid-generator;1"].
-                  getService(Ci.nsIUUIDGenerator);
-    return uuidgen.generateUUID().toString().replace(/[{}]/g, '');
+    // 70 characters that are not-escaped URL-friendly
+    const code =
+      "!()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
+
+    let guid = "";
+    let num = 0;
+    let val;
+
+    // Generate ten 70-value characters for a 70^10 (~61.29-bit) GUID
+    for (let i = 0; i < 10; i++) {
+      // Refresh the number source after using it a few times
+      if (i == 0 || i == 5)
+        num = Math.random();
+
+      // Figure out which code to use for the next GUID character
+      num *= 70;
+      val = Math.floor(num);
+      guid += code[val];
+      num -= val;
+    }
+
+    return guid;
   },
 
   anno: function anno(id, anno, val, expire) {
     // Figure out if we have a bookmark or page
     let annoFunc = (typeof id == "number" ? "Item" : "Page") + "Annotation";
 
     // Convert to a nsIURI if necessary
     if (typeof id == "string")