Here the allocated size is fixed (always 64 pointers), so alloca does not seem needed.
I wonder how many other functions could avoid dynamic allocation like this one (either with a stack array or alloca). Regards Dominique On Mon, Oct 7, 2019 at 10:26 PM Mateusz Wajchęprzełóż <mati271...@gmail.com> wrote: > > What about sqlite3StackAllocZero and SQLITE_USE_ALLOCA? > > pon., 7 paź 2019 o 20:51 Dominique Pellé <dominique.pe...@gmail.com> > napisał(a): > > > Hi > > > > Below is a patch which avoids a dynamic > > allocation in vdbeSorterSort(...), using a local > > stack array instead (faster and smaller code). > > I assume that a local array of 64 pointers is small > > enough to be in the stack. > > > > Is this worth merging? > > > > $ fossil diff src/vdbesort.c > > Index: src/vdbesort.c > > ================================================================== > > --- src/vdbesort.c > > +++ src/vdbesort.c > > @@ -1394,25 +1394,20 @@ > > ** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if > > ** an error occurs. > > */ > > static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){ > > int i; > > - SorterRecord **aSlot; > > + SorterRecord *aSlot[64] = { 0 }; > > SorterRecord *p; > > int rc; > > > > rc = vdbeSortAllocUnpacked(pTask); > > if( rc!=SQLITE_OK ) return rc; > > > > p = pList->pList; > > pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter); > > > > - aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *)); > > - if( !aSlot ){ > > - return SQLITE_NOMEM_BKPT; > > - } > > - > > while( p ){ > > SorterRecord *pNext; > > if( pList->aMemory ){ > > if( (u8*)p==pList->aMemory ){ > > pNext = 0; > > @@ -1438,11 +1433,10 @@ > > if( aSlot[i]==0 ) continue; > > p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i]; > > } > > pList->pList = p; > > > > - sqlite3_free(aSlot); > > assert( pTask->pUnpacked->errCode==SQLITE_OK > > || pTask->pUnpacked->errCode==SQLITE_NOMEM > > ); > > return pTask->pUnpacked->errCode; > > } > > > > Regards > > Dominique > > _______________________________________________ > > sqlite-users mailing list > > sqlite-users@mailinglists.sqlite.org > > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users > > > _______________________________________________ > sqlite-users mailing list > sqlite-users@mailinglists.sqlite.org > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users _______________________________________________ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users