Do you have a benchmark demonstrating that time taken in memsys5Roundup is a bottleneck on some compiler and platform?
gcc and clang both turn for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2); into a tight loop that just execute an add or shift-left and a compare every iteration. This is pretty fast, and I'm skeptical that a lookup table could be much faster. - David On Thu, Aug 9, 2018 at 4:00 AM, 2009110205 <[email protected]> wrote: > We can increase the efficiency with the following code replace: > > > > > static int memsys5Roundup(int n){//changed by wjf > if( n > 0x40000000 ) return 0; > int i = 0; > while (_BlockSize[i++]<n); > return _BlockSize[i-1]; > } > //static int memsys5Roundup(int n){ > // int iFullSz; > // if( n > 0x40000000 ) return 0; > // for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2); > // return iFullSz; > //} > > > > > > > > > /* > ** Initialize the memory allocator. > ** > ** This routine is not threadsafe. The caller must be holding a mutex > ** to prevent multiple threads from entering at the same time. > */ > static int _BlockSize[32];//add by wjf > static int memsys5Init(void *NotUsed){ > int ii; /* Loop counter */ > int nByte; /* Number of bytes of memory available to this > allocator */ > u8 *zByte; /* Memory usable by this allocator */ > int nMinLog; /* Log base 2 of minimum allocation size in bytes */ > int iOffset; /* An offset into mem5.aCtrl[] */ > > > UNUSED_PARAMETER(NotUsed); > > > /* For the purposes of this routine, disable the mutex */ > mem5.mutex = 0; > > > /* The size of a Mem5Link object must be a power of two. Verify that > ** this is case. > */ > assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 ); > > > nByte = sqlite3GlobalConfig.nHeap; > zByte = (u8*)sqlite3GlobalConfig.pHeap; > assert( zByte!=0 ); /* sqlite3_config() does not allow otherwise */ > > > /* boundaries on sqlite3GlobalConfig.mnReq are enforced in > sqlite3_config() */ > nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq); > mem5.szAtom = (1<<nMinLog); > while( (int)sizeof(Mem5Link)>mem5.szAtom ){ > mem5.szAtom = mem5.szAtom << 1; > } > //add by wjf > long long size = mem5.szAtom; > int i = 0; > while (size < 0x40000000) { > _BlockSize[i++] = (int)size; > size *= 2; > } > > > mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8))); > mem5.zPool = zByte; > mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom]; > > > for(ii=0; ii<=LOGMAX; ii++){ > mem5.aiFreelist[ii] = -1; > } > > > iOffset = 0; > for(ii=LOGMAX; ii>=0; ii--){ > int nAlloc = (1<<ii); > if( (iOffset+nAlloc)<=mem5.nBlock ){ > mem5.aCtrl[iOffset] = ii | CTRL_FREE; > memsys5Link(iOffset, ii); > iOffset += nAlloc; > } > assert((iOffset+nAlloc)>mem5.nBlock); > } > > > /* If a mutex is required for normal operation, allocate one */ > if( sqlite3GlobalConfig.bMemstat==0 ){ > mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); > } > > > return SQLITE_OK; > } > > _______________________________________________ > sqlite-users mailing list > [email protected] > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users > _______________________________________________ sqlite-users mailing list [email protected] http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

