/ Check-in [dd705955]
Login

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

Overview
Comment:Minor modification to restoreOrClearCursorPosition() to improve efficiency. Do not allocate the extra 8-bytes if memory-management is not enabled. (CVS 2936)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: dd705955429d847af85ffaf248976bcd1d861852
User & Date: danielk1977 2006-01-13 06:33:24
Context
2006-01-13
11:22
Remove a few duplicate variable initializations in sqlite3BtreeCursor(). (CVS 2937) check-in: 5e46ec01 user: danielk1977 tags: trunk
06:33
Minor modification to restoreOrClearCursorPosition() to improve efficiency. Do not allocate the extra 8-bytes if memory-management is not enabled. (CVS 2936) check-in: dd705955 user: danielk1977 tags: trunk
04:31
Additional speed enhancements in btree.c. (CVS 2935) check-in: 48b550ce user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** $Id: btree.c,v 1.295 2006/01/13 04:31:58 drh Exp $
           12  +** $Id: btree.c,v 1.296 2006/01/13 06:33:24 danielk1977 Exp $
    13     13   **
    14     14   ** This file implements a external (disk-based) database using BTrees.
    15     15   ** For a detailed discussion of BTrees, refer to
    16     16   **
    17     17   **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
    18     18   **     "Sorting And Searching", pages 473-480. Addison-Wesley
    19     19   **     Publishing Company, Reading, Massachusetts.
................................................................................
   577    577   ** at most one effective restoreOrClearCursorPosition() call after each 
   578    578   ** saveCursorPosition().
   579    579   **
   580    580   ** If the second argument argument - doSeek - is false, then instead of 
   581    581   ** returning the cursor to it's saved position, any saved position is deleted
   582    582   ** and the cursor state set to CURSOR_INVALID.
   583    583   */
   584         -static int restoreOrClearCursorPosition(BtCursor *pCur, int doSeek){
          584  +static int restoreOrClearCursorPositionX(BtCursor *pCur, int doSeek){
   585    585     int rc = SQLITE_OK;
   586         -  if( pCur->eState==CURSOR_REQUIRESEEK ){
   587         -    assert( sqlite3ThreadDataReadOnly()->useSharedData );
   588         -    if( doSeek ){
   589         -      rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, &pCur->skip);
   590         -    }else{
   591         -      pCur->eState = CURSOR_INVALID;
   592         -    }
   593         -    if( rc==SQLITE_OK ){
   594         -      sqliteFree(pCur->pKey);
   595         -      pCur->pKey = 0;
   596         -      assert( CURSOR_VALID==pCur->eState || CURSOR_INVALID==pCur->eState );
   597         -    }
          586  +  assert( sqlite3ThreadDataReadOnly()->useSharedData );
          587  +  assert( pCur->eState==CURSOR_REQUIRESEEK );
          588  +  if( doSeek ){
          589  +    rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, &pCur->skip);
          590  +  }else{
          591  +    pCur->eState = CURSOR_INVALID;
          592  +  }
          593  +  if( rc==SQLITE_OK ){
          594  +    sqliteFree(pCur->pKey);
          595  +    pCur->pKey = 0;
          596  +    assert( CURSOR_VALID==pCur->eState || CURSOR_INVALID==pCur->eState );
   598    597     }
   599    598     return rc;
   600    599   }
   601    600   
          601  +#define restoreOrClearCursorPosition(p,x) \
          602  +  (p->eState==CURSOR_REQUIRESEEK?restoreOrClearCursorPositionX(p,x):SQLITE_OK)
          603  +
   602    604   /*
   603    605   ** Query to see if btree handle p may obtain a lock of type eLock 
   604    606   ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
   605    607   ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
   606    608   ** SQLITE_LOCKED if not.
   607    609   */
   608    610   static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){

Changes to src/build.c.

    18     18   **     CREATE INDEX
    19     19   **     DROP INDEX
    20     20   **     creating ID lists
    21     21   **     BEGIN TRANSACTION
    22     22   **     COMMIT
    23     23   **     ROLLBACK
    24     24   **
    25         -** $Id: build.c,v 1.378 2006/01/12 01:56:44 drh Exp $
           25  +** $Id: build.c,v 1.379 2006/01/13 06:33:24 danielk1977 Exp $
    26     26   */
    27     27   #include "sqliteInt.h"
    28     28   #include <ctype.h>
    29     29   
    30     30   /*
    31     31   ** This routine is called when a new SQL statement is beginning to
    32     32   ** be parsed.  Initialize the pParse structure as needed.
................................................................................
  1169   1169     int i;
  1170   1170   
  1171   1171     if( (p = pParse->pNewTable)==0 ) return;
  1172   1172     i = p->nCol-1;
  1173   1173   
  1174   1174     if( sqlite3LocateCollSeq(pParse, zType, nType) ){
  1175   1175       Index *pIdx;
  1176         -    p->aCol[i].zColl = sqlite3StrNDup(zType, nType);
         1176  +    p->aCol[i].zColl = sqliteStrNDup(zType, nType);
  1177   1177     
  1178   1178       /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
  1179   1179       ** then an index may have been created on this column before the
  1180   1180       ** collation type was added. Correct this if it is the case.
  1181   1181       */
  1182   1182       for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
  1183   1183         assert( pIdx->nColumn==1 );
................................................................................
  3149   3149     if( pName1==0 || pName1->z==0 ){
  3150   3150       reindexDatabases(pParse, 0);
  3151   3151       return;
  3152   3152     }else if( pName2==0 || pName2->z==0 ){
  3153   3153       assert( pName1->z );
  3154   3154       pColl = sqlite3FindCollSeq(db, ENC(db), (char*)pName1->z, pName1->n, 0);
  3155   3155       if( pColl ){
  3156         -      char *z = sqlite3StrNDup(pName1->z, pName1->n);
         3156  +      char *z = sqliteStrNDup(pName1->z, pName1->n);
  3157   3157         if( z ){
  3158   3158           reindexDatabases(pParse, z);
  3159   3159           sqliteFree(z);
  3160   3160         }
  3161   3161         return;
  3162   3162       }
  3163   3163     }

Changes to src/expr.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used for analyzing expressions and
    13     13   ** for generating VDBE code that evaluates expressions in SQLite.
    14     14   **
    15         -** $Id: expr.c,v 1.248 2006/01/11 21:41:22 drh Exp $
           15  +** $Id: expr.c,v 1.249 2006/01/13 06:33:24 danielk1977 Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /*
    21     21   ** Return the 'affinity' of the expression pExpr if any.
    22     22   **
................................................................................
   351    351           break;
   352    352         }
   353    353       }
   354    354       if( i>=pParse->nVarExpr ){
   355    355         pExpr->iTable = ++pParse->nVar;
   356    356         if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
   357    357           pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
   358         -        sqlite3ReallocOrFree((void**)&pParse->apVarExpr,
          358  +        sqliteReallocOrFree((void**)&pParse->apVarExpr,
   359    359                          pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
   360    360         }
   361    361         if( !sqlite3ThreadDataReadOnly()->mallocFailed ){
   362    362           assert( pParse->apVarExpr!=0 );
   363    363           pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
   364    364         }
   365    365       }

Changes to src/os_common.h.

   129    129   ** sqlite3GenericOsFree
   130    130   ** sqlite3GenericAllocationSize
   131    131   **
   132    132   ** Implementation of the os level dynamic memory allocation interface in terms
   133    133   ** of the standard malloc(), realloc() and free() found in many operating
   134    134   ** systems. No rocket science here.
   135    135   */
          136  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   136    137   void *sqlite3GenericMalloc(int n){
   137    138     char *p = (char *)malloc(n+8);
   138    139     assert(n>0);
   139    140     assert(sizeof(int)<=8);
   140    141     if( p ){
   141    142       *(int *)p = n;
   142    143     }
................................................................................
   154    155   void sqlite3GenericFree(void *p){
   155    156     assert(p);
   156    157     free((void *)((char *)p - 8));
   157    158   }
   158    159   int sqlite3GenericAllocationSize(void *p){
   159    160     return p ? *(int *)((char *)p - 8) : 0;
   160    161   }
          162  +#else
          163  +void *sqlite3GenericMalloc(int n){
          164  +  char *p = (char *)malloc(n);
          165  +  return (void *)p;
          166  +}
          167  +void *sqlite3GenericRealloc(void *p, int n){
          168  +  assert(n>0);
          169  +  p = realloc(p, n);
          170  +  return p;
          171  +}
          172  +void sqlite3GenericFree(void *p){
          173  +  assert(p);
          174  +  free(p);
          175  +}
          176  +int sqlite3GenericAllocationSize(void *p){
          177  +  assert(0);
          178  +}
          179  +#endif

Changes to src/os_win.c.

   930    930     int nByte;
   931    931     nByte = strlen(zRelative) + MAX_PATH + 1001;
   932    932     zFull = sqliteMalloc( nByte );
   933    933     if( zFull==0 ) return 0;
   934    934     if( cygwin_conv_to_full_win32_path(zRelative, zFull) ) return 0;
   935    935   #elif OS_WINCE
   936    936     /* WinCE has no concept of a relative pathname, or so I am told. */
   937         -  zFull = sqlite3StrDup(zRelative);
          937  +  zFull = sqliteStrDup(zRelative);
   938    938   #else
   939    939     char *zNotUsed;
   940    940     WCHAR *zWide;
   941    941     int nByte;
   942    942     zWide = utf8ToUnicode(zRelative);
   943    943     if( zWide ){
   944    944       WCHAR *zTemp, *zNotUsedW;

Changes to src/prepare.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains the implementation of the sqlite3_prepare()
    13     13   ** interface, and routines that contribute to loading the database schema
    14     14   ** from disk.
    15     15   **
    16         -** $Id: prepare.c,v 1.21 2006/01/12 12:43:36 drh Exp $
           16  +** $Id: prepare.c,v 1.22 2006/01/13 06:33:24 danielk1977 Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "os.h"
    20     20   #include <ctype.h>
    21     21   
    22     22   /*
    23     23   ** Fill the InitData structure with an error message that indicates
................................................................................
   318    318   
   319    319   /*
   320    320   ** Initialize all database files - the main database file, the file
   321    321   ** used to store temporary tables, and any additional database files
   322    322   ** created using ATTACH statements.  Return a success code.  If an
   323    323   ** error occurs, write an error message into *pzErrMsg.
   324    324   **
   325         -** After the database is initialized, the SQLITE_Initialized
   326         -** bit is set in the flags field of the sqlite structure. 
          325  +** After a database is initialized, the DB_SchemaLoaded bit is set
          326  +** bit is set in the flags field of the Db structure. If the database
          327  +** file was of zero-length, then the DB_Empty flag is also set.
   327    328   */
   328    329   int sqlite3Init(sqlite3 *db, char **pzErrMsg){
   329    330     int i, rc;
   330    331     int called_initone = 0;
   331    332     
   332    333     if( db->init.busy ) return SQLITE_OK;
   333    334     rc = SQLITE_OK;

Changes to src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.293 2006/01/11 21:41:22 drh Exp $
           15  +** $Id: select.c,v 1.294 2006/01/13 06:33:24 danielk1977 Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Allocate a new Select structure and return a pointer to that
    22     22   ** structure.
................................................................................
  1030   1030       memset(&sNC, 0, sizeof(sNC));
  1031   1031       sNC.pSrcList = pSelect->pSrc;
  1032   1032       zType = sqliteStrDup(columnType(&sNC, p));
  1033   1033       pCol->zType = zType;
  1034   1034       pCol->affinity = sqlite3ExprAffinity(p);
  1035   1035       pColl = sqlite3ExprCollSeq(pParse, p);
  1036   1036       if( pColl ){
  1037         -      pCol->zColl = sqlite3StrDup(pColl->zName);
         1037  +      pCol->zColl = sqliteStrDup(pColl->zName);
  1038   1038       }
  1039   1039     }
  1040   1040     pTab->iPKey = -1;
  1041   1041     return pTab;
  1042   1042   }
  1043   1043   
  1044   1044   /*

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.466 2006/01/12 17:20:51 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.467 2006/01/13 06:33:24 danielk1977 Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Extra interface definitions for those who need them
    21     21   */
................................................................................
   280    280   #define sqliteStrNDup(x,y)       sqlite3StrNDup(x,y)
   281    281   #define sqliteReallocOrFree(x,y) sqlite3ReallocOrFree(x,y)
   282    282   
   283    283   #endif
   284    284   
   285    285   #define sqliteFree(x)          sqlite3FreeX(x)
   286    286   #define sqliteAllocSize(x)     sqlite3AllocSize(x)
          287  +
   287    288   
   288    289   /*
   289    290   ** An instance of this structure might be allocated to store information
   290    291   ** specific to a single thread.
   291    292   **
   292    293   ** To avoid a memory leak on windows, the content of this structure is
   293    294   ** checked at the conclusion of each API call.  If it is all zero, it

Changes to src/utf.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used to translate between UTF-8, 
    13     13   ** UTF-16, UTF-16BE, and UTF-16LE.
    14     14   **
    15         -** $Id: utf.c,v 1.35 2005/12/15 22:34:01 drh Exp $
           15  +** $Id: utf.c,v 1.36 2006/01/13 06:33:24 danielk1977 Exp $
    16     16   **
    17     17   ** Notes on UTF-8:
    18     18   **
    19     19   **   Byte-0    Byte-1    Byte-2    Byte-3    Value
    20     20   **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
    21     21   **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
    22     22   **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
................................................................................
   460    460   char *sqlite3utf16to8(const void *z, int nByte){
   461    461     Mem m;
   462    462     memset(&m, 0, sizeof(m));
   463    463     sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
   464    464     sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
   465    465     assert( m.flags & MEM_Term );
   466    466     assert( m.flags & MEM_Str );
   467         -  return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3StrDup(m.z);
          467  +  return (m.flags & MEM_Dyn)!=0 ? m.z : sqliteStrDup(m.z);
   468    468   }
   469    469   
   470    470   /*
   471    471   ** pZ is a UTF-16 encoded unicode string. If nChar is less than zero,
   472    472   ** return the number of bytes up to (but not including), the first pair
   473    473   ** of consecutive 0x00 bytes in pZ. If nChar is not less than zero,
   474    474   ** then return the number of bytes in the first nChar unicode characters

Changes to src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.169 2006/01/12 01:25:18 drh Exp $
           17  +** $Id: util.c,v 1.170 2006/01/13 06:33:24 danielk1977 Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <stdarg.h>
    22     22   #include <ctype.h>
    23     23   
    24     24   /*
................................................................................
   643    643   ** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree().
   644    644   **
   645    645   ** The number of bytes allocated does not include any overhead inserted by 
   646    646   ** any malloc() wrapper functions that may be called. So the value returned
   647    647   ** is the number of bytes that were available to SQLite using pointer p, 
   648    648   ** regardless of how much memory was actually allocated.
   649    649   */
          650  +#if 0          /* This is never actually used */
   650    651   int sqlite3AllocSize(void *p){
   651    652     return OSSIZEOF(p);
   652    653   }
          654  +#endif
   653    655   
   654    656   /*
   655    657   ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
   656    658   ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
   657    659   ** is because when memory debugging is turned on, these two functions are 
   658    660   ** called via macros that record the current file and line number in the
   659    661   ** ThreadData structure.

Changes to src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.526 2006/01/13 01:48:59 drh Exp $
           46  +** $Id: vdbe.c,v 1.527 2006/01/13 06:33:24 danielk1977 Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include "os.h"
    50     50   #include <ctype.h>
    51     51   #include "vdbeInt.h"
    52     52   
    53     53   /*
................................................................................
  4206   4206     int i = p->contextStackTop++;
  4207   4207     Context *pContext;
  4208   4208   
  4209   4209     assert( i>=0 );
  4210   4210     /* FIX ME: This should be allocated as part of the vdbe at compile-time */
  4211   4211     if( i>=p->contextStackDepth ){
  4212   4212       p->contextStackDepth = i+1;
  4213         -    sqlite3ReallocOrFree((void**)&p->contextStack, sizeof(Context)*(i+1));
         4213  +    sqliteReallocOrFree((void**)&p->contextStack, sizeof(Context)*(i+1));
  4214   4214       if( p->contextStack==0 ) goto no_mem;
  4215   4215     }
  4216   4216     pContext = &p->contextStack[i];
  4217   4217     pContext->lastRowid = db->lastRowid;
  4218   4218     pContext->nChange = p->nChange;
  4219   4219     pContext->sFifo = p->sFifo;
  4220   4220     sqlite3VdbeFifoInit(&p->sFifo);

Changes to src/vdbeaux.c.

   143    143   */
   144    144   int sqlite3VdbeMakeLabel(Vdbe *p){
   145    145     int i;
   146    146     i = p->nLabel++;
   147    147     assert( p->magic==VDBE_MAGIC_INIT );
   148    148     if( i>=p->nLabelAlloc ){
   149    149       p->nLabelAlloc = p->nLabelAlloc*2 + 10;
   150         -    sqlite3ReallocOrFree((void**)&p->aLabel,
          150  +    sqliteReallocOrFree((void**)&p->aLabel,
   151    151                             p->nLabelAlloc*sizeof(p->aLabel[0]));
   152    152     }
   153    153     if( p->aLabel ){
   154    154       p->aLabel[i] = -1;
   155    155     }
   156    156     return -1-i;
   157    157   }

Changes to src/where.c.

    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.  This module is reponsible for
    14     14   ** generating the code that loops through a table looking for applicable
    15     15   ** rows.  Indices are selected and used to speed the search when doing
    16     16   ** so is applicable.  Because this module is responsible for selecting
    17     17   ** indices, you might also think of this module as the "query optimizer".
    18     18   **
    19         -** $Id: where.c,v 1.194 2006/01/11 21:41:22 drh Exp $
           19  +** $Id: where.c,v 1.195 2006/01/13 06:33:25 danielk1977 Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   
    23     23   /*
    24     24   ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
    25     25   */
    26     26   #define BMS  (sizeof(Bitmask)*8)
................................................................................
  1206   1206       Vdbe *v = pParse->pVdbe;
  1207   1207   
  1208   1208       sqlite3CodeSubselect(pParse, pX);
  1209   1209       iTab = pX->iTable;
  1210   1210       sqlite3VdbeAddOp(v, OP_Rewind, iTab, brk);
  1211   1211       VdbeComment((v, "# %.*s", pX->span.n, pX->span.z));
  1212   1212       pLevel->nIn++;
  1213         -    sqlite3ReallocOrFree((void**)&pLevel->aInLoop,
         1213  +    sqliteReallocOrFree((void**)&pLevel->aInLoop,
  1214   1214                                    sizeof(pLevel->aInLoop[0])*3*pLevel->nIn);
  1215   1215       aIn = pLevel->aInLoop;
  1216   1216       if( aIn ){
  1217   1217         aIn += pLevel->nIn*3 - 3;
  1218   1218         aIn[0] = OP_Next;
  1219   1219         aIn[1] = iTab;
  1220   1220         aIn[2] = sqlite3VdbeAddOp(v, OP_Column, iTab, 0);