/ Check-in [802148f3]
Login

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

Overview
Comment:Make the implementation of the sqlite3_aggregate_context() interface faster for second an subsequent invocations. This helps all aggregate functions to perform better.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:802148f3110462eac939d53ce08eb9a2f6aac739
User & Date: drh 2014-08-23 18:17:19
Context
2014-08-23
19:04
Changes to sqlite3ScratchMalloc() that make the entire memory allocation interface a little faster and about 100 bytes smaller. check-in: f83daa16 user: drh tags: trunk
18:17
Make the implementation of the sqlite3_aggregate_context() interface faster for second an subsequent invocations. This helps all aggregate functions to perform better. check-in: 802148f3 user: drh tags: trunk
17:41
Improved performance in the type handling of arithmetic operators in the VDBE. check-in: 0c0a6039 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/vdbeapi.c.

   601    601     char *zErr;
   602    602     UNUSED_PARAMETER2(NotUsed, NotUsed2);
   603    603     zErr = sqlite3_mprintf(
   604    604         "unable to use function %s in the requested context", zName);
   605    605     sqlite3_result_error(context, zErr, -1);
   606    606     sqlite3_free(zErr);
   607    607   }
          608  +
          609  +/*
          610  +** Create a new aggregate context for p and return a pointer to
          611  +** its pMem->z element.
          612  +*/
          613  +static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
          614  +  Mem *pMem = p->pMem;
          615  +  assert( (pMem->flags & MEM_Agg)==0 );
          616  +  if( nByte<=0 ){
          617  +    sqlite3VdbeMemReleaseExternal(pMem);
          618  +    pMem->flags = MEM_Null;
          619  +    pMem->z = 0;
          620  +  }else{
          621  +    sqlite3VdbeMemGrow(pMem, nByte, 0);
          622  +    pMem->flags = MEM_Agg;
          623  +    pMem->u.pDef = p->pFunc;
          624  +    if( pMem->z ){
          625  +      memset(pMem->z, 0, nByte);
          626  +    }
          627  +  }
          628  +  return (void*)pMem->z;
          629  +}
   608    630   
   609    631   /*
   610    632   ** Allocate or return the aggregate context for a user function.  A new
   611    633   ** context is allocated on the first call.  Subsequent calls return the
   612    634   ** same context that was returned on prior calls.
   613    635   */
   614    636   void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
   615         -  Mem *pMem;
   616    637     assert( p && p->pFunc && p->pFunc->xStep );
   617    638     assert( sqlite3_mutex_held(p->s.db->mutex) );
   618         -  pMem = p->pMem;
   619    639     testcase( nByte<0 );
   620         -  if( (pMem->flags & MEM_Agg)==0 ){
   621         -    if( nByte<=0 ){
   622         -      sqlite3VdbeMemReleaseExternal(pMem);
   623         -      pMem->flags = MEM_Null;
   624         -      pMem->z = 0;
   625         -    }else{
   626         -      sqlite3VdbeMemGrow(pMem, nByte, 0);
   627         -      pMem->flags = MEM_Agg;
   628         -      pMem->u.pDef = p->pFunc;
   629         -      if( pMem->z ){
   630         -        memset(pMem->z, 0, nByte);
   631         -      }
   632         -    }
          640  +  if( (p->pMem->flags & MEM_Agg)==0 ){
          641  +    return createAggContext(p, nByte);
          642  +  }else{
          643  +    return (void*)p->pMem->z;
   633    644     }
   634         -  return (void*)pMem->z;
   635    645   }
   636    646   
   637    647   /*
   638    648   ** Return the auxilary data pointer, if any, for the iArg'th argument to
   639    649   ** the user-function defined by pCtx.
   640    650   */
   641    651   void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){