/ Check-in [1bdee199]
Login

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

Overview
Comment:Additional small performance increase and size reduction to the sqlite3VdbeMakeLabel() mechanism.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1bdee199a71e0a6c247b85e72de9e3a3099b7179c33288735721facef3b96459
User & Date: drh 2018-12-29 14:23:22
Context
2018-12-31
16:36
Fix the OP_OpenEphemeral opcode in the bytecode engine so that if it is called a second or subsequent time, it merely clears the existing table rather than creating a new one. Proposed fix for ticket [d0866b26f83e9c55e30de0821f5d]. check-in: 4678cb10 user: drh tags: trunk
2018-12-29
14:23
Additional small performance increase and size reduction to the sqlite3VdbeMakeLabel() mechanism. check-in: 1bdee199 user: drh tags: trunk
02:26
A new implementation of sqlite3VdbeMakeLabel() is faster and makes fewer memory allocations by deferring memory allocation until sqlite3VdbeResolveLabel() is called, at which point the code generator has a better idea of how big the relocation table needs to be. The sqlite3VdbeMakeLabel() routine now takes a Parse* parameter instead of Vdbe*. check-in: 4a0929ac user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/sqliteInt.h.

  3064   3064     int iRangeReg;       /* First register in temporary register block */
  3065   3065     int nErr;            /* Number of errors seen */
  3066   3066     int nTab;            /* Number of previously allocated VDBE cursors */
  3067   3067     int nMem;            /* Number of memory cells used so far */
  3068   3068     int szOpAlloc;       /* Bytes of memory space allocated for Vdbe.aOp[] */
  3069   3069     int iSelfTab;        /* Table associated with an index on expr, or negative
  3070   3070                          ** of the base register during check-constraint eval */
  3071         -  int nLabel;          /* Number of labels used */
         3071  +  int nLabel;          /* The *negative* of the number of labels used */
  3072   3072     int nLabelAlloc;     /* Number of slots in aLabel */
  3073   3073     int *aLabel;         /* Space to hold the labels */
  3074   3074     ExprList *pConstExpr;/* Constant expressions */
  3075   3075     Token constraintName;/* Name of the constraint currently being parsed */
  3076   3076     yDbMask writeMask;   /* Start a write transaction on these databases */
  3077   3077     yDbMask cookieMask;  /* Bitmask of schema verified databases */
  3078   3078     int regRowid;        /* Register holding rowid of CREATE TABLE entry */

Changes to src/vdbe.h.

   152    152   #   define COLNAME_N      1      /* Store only the name */
   153    153   # else
   154    154   #   define COLNAME_N      2      /* Store the name and decltype */
   155    155   # endif
   156    156   #endif
   157    157   
   158    158   /*
   159         -** The following macro converts a relative address in the p2 field
   160         -** of a VdbeOp structure into a negative number so that 
   161         -** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
   162         -** the macro again restores the address.
          159  +** The following macro converts a label returned by sqlite3VdbeMakeLabel()
          160  +** into an index into the Parse.aLabel[] array that contains the resolved
          161  +** address of that label.
   163    162   */
   164         -#define ADDR(X)  (-1-(X))
          163  +#define ADDR(X)  (~(X))
   165    164   
   166    165   /*
   167    166   ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
   168    167   ** header file that defines a number for each opcode used by the VDBE.
   169    168   */
   170    169   #include "opcodes.h"
   171    170   

Changes to src/vdbeaux.c.

   441    441   ** the label is resolved to a specific address, the VDBE will scan
   442    442   ** through its operation list and change all values of P2 which match
   443    443   ** the label into the resolved address.
   444    444   **
   445    445   ** The VDBE knows that a P2 value is a label because labels are
   446    446   ** always negative and P2 values are suppose to be non-negative.
   447    447   ** Hence, a negative P2 value is a label that has yet to be resolved.
          448  +** (Later:) This is only true for opcodes that have the OPFLG_JUMP
          449  +** property.
   448    450   **
   449         -** Zero is returned if a malloc() fails.
          451  +** Variable usage notes:
          452  +**
          453  +**     Parse.aLabel[x]     Stores the address that the x-th label resolves
          454  +**                         into.  For testing (SQLITE_DEBUG), unresolved
          455  +**                         labels stores -1, but that is not required.
          456  +**     Parse.nLabelAlloc   Number of slots allocated to Parse.aLabel[]
          457  +**     Parse.nLabel        The *negative* of the number of labels that have
          458  +**                         been issued.  The negative is stored because
          459  +**                         that gives a performance improvement over storing
          460  +**                         the equivalent positive value.
   450    461   */
   451    462   int sqlite3VdbeMakeLabel(Parse *pParse){
   452         -  return ADDR(pParse->nLabel++);
          463  +  return --pParse->nLabel;
   453    464   }
   454    465   
   455    466   /*
   456    467   ** Resolve label "x" to be the address of the next instruction to
   457    468   ** be inserted.  The parameter "x" must have been obtained from
   458    469   ** a prior call to sqlite3VdbeMakeLabel().
   459    470   */
   460    471   static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){
   461         -  int nNewSize = p->nLabel+10;
          472  +  int nNewSize = 10 - p->nLabel;
   462    473     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
   463    474                        nNewSize*sizeof(p->aLabel[0]));
   464    475     if( p->aLabel==0 ){
   465    476       p->nLabelAlloc = 0;
   466    477     }else{
   467    478   #ifdef SQLITE_DEBUG
   468    479       int i;
................................................................................
   472    483       p->aLabel[j] = v->nOp;
   473    484     }
   474    485   }
   475    486   void sqlite3VdbeResolveLabel(Vdbe *v, int x){
   476    487     Parse *p = v->pParse;
   477    488     int j = ADDR(x);
   478    489     assert( v->magic==VDBE_MAGIC_INIT );
   479         -  assert( j<p->nLabel );
          490  +  assert( j<-p->nLabel );
   480    491     assert( j>=0 );
   481    492   #ifdef SQLITE_DEBUG
   482    493     if( p->db->flags & SQLITE_VdbeAddopTrace ){
   483    494       printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
   484    495     }
   485    496   #endif
   486         -  if( p->nLabelAlloc < p->nLabel ){
          497  +  if( p->nLabelAlloc + p->nLabel < 0 ){
   487    498       resizeResolveLabel(p,v,j);
   488    499     }else{
   489    500       assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
   490    501       p->aLabel[j] = v->nOp;
   491    502     }
   492    503   }
   493    504   
................................................................................
   757    768   #endif
   758    769           default: {
   759    770             if( pOp->p2<0 ){
   760    771               /* The mkopcodeh.tcl script has so arranged things that the only
   761    772               ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
   762    773               ** have non-negative values for P2. */
   763    774               assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
   764         -            assert( ADDR(pOp->p2)<pParse->nLabel );
          775  +            assert( ADDR(pOp->p2)<-pParse->nLabel );
   765    776               pOp->p2 = aLabel[ADDR(pOp->p2)];
   766    777             }
   767    778             break;
   768    779           }
   769    780         }
   770    781         /* The mkopcodeh.tcl script has so arranged things that the only
   771    782         ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to