/ Check-in [a929be55]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Performance enhancement in sqlite3PutVarint().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a929be551924144c9bc7aab608404d59e479abb5
User & Date: drh 2014-08-22 18:48:25
Context
2014-08-22
20:35
Combine the pcacheAddToDirtyList() and pcacheRemoveFromDirtyList() routines into a single pcacheManageDirtyList() routine. The resulting binary code is slightly faster and a few bytes smaller. check-in: 6bcf1af6 user: drh tags: trunk
18:48
Performance enhancement in sqlite3PutVarint(). check-in: a929be55 user: drh tags: trunk
18:00
Split the sqlite3Error() routine into sqlite3Error() and sqlite3ErrorWithMsg(), for a slight size reduction and performance increase. check-in: cf561d1f user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/sqliteInt.h.

  3303   3303   LogEst sqlite3LogEstFromDouble(double);
  3304   3304   #endif
  3305   3305   u64 sqlite3LogEstToInt(LogEst);
  3306   3306   
  3307   3307   /*
  3308   3308   ** Routines to read and write variable-length integers.  These used to
  3309   3309   ** be defined locally, but now we use the varint routines in the util.c
  3310         -** file.  Code should use the MACRO forms below, as the Varint32 versions
  3311         -** are coded to assume the single byte case is already handled (which 
  3312         -** the MACRO form does).
         3310  +** file.
  3313   3311   */
  3314   3312   int sqlite3PutVarint(unsigned char*, u64);
  3315         -int sqlite3PutVarint32(unsigned char*, u32);
  3316   3313   u8 sqlite3GetVarint(const unsigned char *, u64 *);
  3317   3314   u8 sqlite3GetVarint32(const unsigned char *, u32 *);
  3318   3315   int sqlite3VarintLen(u64 v);
  3319   3316   
  3320   3317   /*
  3321         -** The header of a record consists of a sequence variable-length integers.
  3322         -** These integers are almost always small and are encoded as a single byte.
  3323         -** The following macros take advantage this fact to provide a fast encode
  3324         -** and decode of the integers in a record header.  It is faster for the common
  3325         -** case where the integer is a single byte.  It is a little slower when the
  3326         -** integer is two or more bytes.  But overall it is faster.
  3327         -**
  3328         -** The following expressions are equivalent:
  3329         -**
  3330         -**     x = sqlite3GetVarint32( A, &B );
  3331         -**     x = sqlite3PutVarint32( A, B );
  3332         -**
  3333         -**     x = getVarint32( A, B );
  3334         -**     x = putVarint32( A, B );
  3335         -**
         3318  +** The common case is for a varint to be a single byte.  They following
         3319  +** macros handle the common case without a procedure call, but then call
         3320  +** the procedure for larger varints.
  3336   3321   */
  3337   3322   #define getVarint32(A,B)  \
  3338   3323     (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
  3339   3324   #define putVarint32(A,B)  \
  3340   3325     (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
  3341         -  sqlite3PutVarint32((A),(B)))
         3326  +  sqlite3PutVarint((A),(B)))
  3342   3327   #define getVarint    sqlite3GetVarint
  3343   3328   #define putVarint    sqlite3PutVarint
  3344   3329   
  3345   3330   
  3346   3331   const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
  3347   3332   void sqlite3TableAffinity(Vdbe*, Table*, int);
  3348   3333   char sqlite3CompareAffinity(Expr *pExpr, char aff2);

Changes to src/util.c.

   704    704   ** of bytes written is returned.
   705    705   **
   706    706   ** A variable-length integer consists of the lower 7 bits of each byte
   707    707   ** for all bytes that have the 8th bit set and one byte with the 8th
   708    708   ** bit clear.  Except, if we get to the 9th byte, it stores the full
   709    709   ** 8 bits and is the last byte.
   710    710   */
   711         -int sqlite3PutVarint(unsigned char *p, u64 v){
          711  +static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
   712    712     int i, j, n;
   713    713     u8 buf[10];
   714    714     if( v & (((u64)0xff000000)<<32) ){
   715    715       p[8] = (u8)v;
   716    716       v >>= 8;
   717    717       for(i=7; i>=0; i--){
   718    718         p[i] = (u8)((v & 0x7f) | 0x80);
................................................................................
   728    728     buf[0] &= 0x7f;
   729    729     assert( n<=9 );
   730    730     for(i=0, j=n-1; j>=0; j--, i++){
   731    731       p[i] = buf[j];
   732    732     }
   733    733     return n;
   734    734   }
   735         -
   736         -/*
   737         -** This routine is a faster version of sqlite3PutVarint() that only
   738         -** works for 32-bit positive integers and which is optimized for
   739         -** the common case of small integers.  A MACRO version, putVarint32,
   740         -** is provided which inlines the single-byte case.  All code should use
   741         -** the MACRO version as this function assumes the single-byte case has
   742         -** already been handled.
   743         -*/
   744         -int sqlite3PutVarint32(unsigned char *p, u32 v){
   745         -#ifndef putVarint32
   746         -  if( (v & ~0x7f)==0 ){
   747         -    p[0] = v;
          735  +int sqlite3PutVarint(unsigned char *p, u64 v){
          736  +  if( v<=0x7f ){
          737  +    p[0] = v&0x7f;
   748    738       return 1;
   749    739     }
   750         -#endif
   751         -  if( (v & ~0x3fff)==0 ){
   752         -    p[0] = (u8)((v>>7) | 0x80);
   753         -    p[1] = (u8)(v & 0x7f);
          740  +  if( v<=0x3fff ){
          741  +    p[0] = ((v>>7)&0x7f)|0x80;
          742  +    p[1] = v&0x7f;
   754    743       return 2;
   755    744     }
   756         -  return sqlite3PutVarint(p, v);
          745  +  return putVarint64(p,v);
   757    746   }
   758    747   
   759    748   /*
   760    749   ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
   761    750   ** are defined here rather than simply putting the constant expressions
   762    751   ** inline in order to work around bugs in the RVT compiler.
   763    752   **

Changes to src/vdbemem.c.

  1126   1126   
  1127   1127     nRet = 1 + nSerial + nVal;
  1128   1128     aRet = sqlite3DbMallocRaw(db, nRet);
  1129   1129     if( aRet==0 ){
  1130   1130       sqlite3_result_error_nomem(context);
  1131   1131     }else{
  1132   1132       aRet[0] = nSerial+1;
  1133         -    sqlite3PutVarint(&aRet[1], iSerial);
         1133  +    putVarint32(&aRet[1], iSerial);
  1134   1134       sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
  1135   1135       sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
  1136   1136       sqlite3DbFree(db, aRet);
  1137   1137     }
  1138   1138   }
  1139   1139   
  1140   1140   /*