]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Refactor the sqlite3_randomness() implementation for improved performance.
authordrh <drh@noemail.net>
Wed, 21 Aug 2013 22:09:25 +0000 (22:09 +0000)
committerdrh <drh@noemail.net>
Wed, 21 Aug 2013 22:09:25 +0000 (22:09 +0000)
FossilOrigin-Name: 4144dffb57b5ed791d7a6d2f26fab5e7dc77fbd1

manifest
manifest.uuid
src/random.c

index c036908051eb0b334cc4daa03f96dac1f6d70c76..6265e284f8e0bf5bab2757fae382252601ddf648 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Simplification\sto\sthe\sStrAccum\sobject\sand\sthe\ssqlite3StrAccumAppend()\nmethod\sthat\salso\sresults\sin\sslightly\sbetter\sperformance.
-D 2013-08-21T21:12:10.959
+C Refactor\sthe\ssqlite3_randomness()\simplementation\sfor\simproved\sperformance.
+D 2013-08-21T22:09:25.294
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -213,7 +213,7 @@ F src/pcache1.c a467393909a4ed7ca9de066d85ba5c5b04a5be63
 F src/pragma.c 1d96ba749253b49bc229c6294f9c2e5ef3dca85c
 F src/prepare.c fa6988589f39af8504a61731614cd4f6ae71554f
 F src/printf.c da9119eb31a187a4b99f60aa4a225141c0ebb74b
-F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
+F src/random.c 0b2dbc37fdfbfa6bd455b091dfcef5bdb32dba68
 F src/resolve.c 9d53899cc6e1f4ec0b4632d07e97d57827bf63b9
 F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0
 F src/select.c 8b148eb851f384412aea57091659d14b369918ca
@@ -1105,7 +1105,7 @@ F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae
 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
-P ef2a6a37366f91889dc2f1ba9cb6d1257d9b05db
-R b5f522403c7336cdf922a181f76ebfed
+P 700dbbea8647e0fdaee6d0aba3d3dd8ebfbac04a
+R bace2fbe40d117379ef5fcee60c09ed4
 U drh
-Z 3ab1c9a1ca4e97a25908645404ada3f7
+Z a22b19ec66f802a5577e622202a99c6b
index 261a986cf496b8087eeab5fee975223e2ca2e21c..e433e95dded5a54e0a69765aa357d228b86b874d 100644 (file)
@@ -1 +1 @@
-700dbbea8647e0fdaee6d0aba3d3dd8ebfbac04a
\ No newline at end of file
+4144dffb57b5ed791d7a6d2f26fab5e7dc77fbd1
\ No newline at end of file
index 234ebdf658f436cc7c68f027c48b0e1b9ba34e98..7afff50885a97edfd1f4f457fdf4f47d9457c115 100644 (file)
@@ -28,24 +28,11 @@ static SQLITE_WSD struct sqlite3PrngType {
 } sqlite3Prng;
 
 /*
-** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
-** must be held while executing this routine.
-**
-** Why not just use a library random generator like lrand48() for this?
-** Because the OP_NewRowid opcode in the VDBE depends on having a very
-** good source of random numbers.  The lrand48() library function may
-** well be good enough.  But maybe not.  Or maybe lrand48() has some
-** subtle problems on some systems that could cause problems.  It is hard
-** to know.  To minimize the risk of problems due to bad lrand48()
-** implementations, SQLite uses this random number generator based
-** on RC4, which we know works very well.
-**
-** (Later):  Actually, OP_NewRowid does not depend on a good source of
-** randomness any more.  But we will leave this code in all the same.
+** Return N random bytes.
 */
-static u8 randomByte(void){
+void sqlite3_randomness(int N, void *pBuf){
   unsigned char t;
-
+  unsigned char *zBuf = pBuf;
 
   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
   ** state vector.  If writable static data is unsupported on the target,
@@ -60,6 +47,10 @@ static u8 randomByte(void){
 # define wsdPrng sqlite3Prng
 #endif
 
+#if SQLITE_THREADSAFE
+  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
+  sqlite3_mutex_enter(mutex);
+#endif
 
   /* Initialize the state of the random number generator once,
   ** the first time this routine is called.  The seed value does
@@ -88,28 +79,14 @@ static u8 randomByte(void){
     wsdPrng.isInit = 1;
   }
 
-  /* Generate and return single random byte
-  */
-  wsdPrng.i++;
-  t = wsdPrng.s[wsdPrng.i];
-  wsdPrng.j += t;
-  wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
-  wsdPrng.s[wsdPrng.j] = t;
-  t += wsdPrng.s[wsdPrng.i];
-  return wsdPrng.s[t];
-}
-
-/*
-** Return N random bytes.
-*/
-void sqlite3_randomness(int N, void *pBuf){
-  unsigned char *zBuf = pBuf;
-#if SQLITE_THREADSAFE
-  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
-#endif
-  sqlite3_mutex_enter(mutex);
   while( N-- ){
-    *(zBuf++) = randomByte();
+    wsdPrng.i++;
+    t = wsdPrng.s[wsdPrng.i];
+    wsdPrng.j += t;
+    wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
+    wsdPrng.s[wsdPrng.j] = t;
+    t += wsdPrng.s[wsdPrng.i];
+    *(zBuf++) = wsdPrng.s[t];
   }
   sqlite3_mutex_leave(mutex);
 }