Skip to content

Commit

Permalink
Remove the use of htonl() in the previous check-in due to linkage iss…
Browse files Browse the repository at this point in the history
…ues.

Add the get2byteAligned() macro and use it for access to the cell offsets
on btree pages for about a 1% performance gain.

FossilOrigin-Name: 79ff36b7170c9e7e7a9935c8b9d1665867771087
  • Loading branch information
drh committed Jun 30, 2015
1 parent 5372e4d commit 329428e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
16 changes: 8 additions & 8 deletions manifest
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
C Make\suse\sof\shtonl()\sand\s<nowiki>__builtin_bswap32()</nowiki>\sfor\sfaster\nimplementations\sof\ssqlite3Get4byte()\sand\ssqlite3Put4byte().
D 2015-06-30T12:47:09.735
C Remove\sthe\suse\sof\shtonl()\sin\sthe\sprevious\scheck-in\sdue\sto\slinkage\sissues.\nAdd\sthe\sget2byteAligned()\smacro\sand\suse\sit\sfor\saccess\sto\sthe\scell\soffsets\non\sbtree\spages\sfor\sabout\sa\s1%\sperformance\sgain.
D 2015-06-30T13:28:18.237
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 285a0a234ed7610d431d91671c136098c2bd86a9
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
Expand Down Expand Up @@ -269,9 +269,9 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240
F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3
F src/bitvec.c d1f21d7d91690747881f03940584f4cc548c9d3d
F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79
F src/btree.c 08ae64b0387e72b8201981a2d7b0f58b92fdff29
F src/btree.c 3a535c0118872c3ee4e198b80a62d09315381dab
F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1
F src/btreeInt.h e4eabc722b1ae017ac9c266a75769d4c6a9afde6
F src/btreeInt.h 583240896ca96ba3b371b42e8cb6bd73c9e4717f
F src/build.c b3f15255d5b16e42dafeaa638fd4f8a47c94ed70
F src/callback.c 7b44ce59674338ad48b0e84e7b72f935ea4f68b0
F src/complete.c addcd8160b081131005d5bc2d34adf20c1c5c92f
Expand Down Expand Up @@ -388,7 +388,7 @@ F src/treeview.c c84b1a8ebc7f1d00cd76ce4958eeb3ae1021beed
F src/trigger.c 322f23aad694e8f31d384dcfa386d52a48d3c52f
F src/update.c 487747b328b7216bb7f6af0695d6937d5c9e605f
F src/utf.c fc6b889ba0779b7722634cdeaa25f1930d93820c
F src/util.c 99396e37038a68073b11bd41b041eace5c1728c2
F src/util.c 89bfe78b4610d456ba431a0865309a20acc115f3
F src/vacuum.c 2ddd5cad2a7b9cef7f9e431b8c7771634c6b1701
F src/vdbe.c 5ee4a2bf871418f61d06dc256b9b3a0084b5ec46
F src/vdbe.h 7a75045d879118b9d3af7e8b3c108f2f27c51473
Expand Down Expand Up @@ -1364,7 +1364,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P b406b20ecdf0bff63c5c222fab11cb9acee86765
R 52c32250d08365fc3595da7414b917a8
P bc27ebd7f73e9fc8e00da6ec82632e439fcce812
R be192770615d2f155ef5ba2c23f2b23e
U drh
Z a9454dee991519551f365a2fb6736c41
Z ba4fa6154f0804a80e26fea56b904f4a
2 changes: 1 addition & 1 deletion manifest.uuid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bc27ebd7f73e9fc8e00da6ec82632e439fcce812
79ff36b7170c9e7e7a9935c8b9d1665867771087
14 changes: 7 additions & 7 deletions src/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -971,9 +971,9 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
** This routine works only for pages that do not contain overflow cells.
*/
#define findCell(P,I) \
((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
#define findCellPastPtr(P,I) \
((P)->aDataOfst + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))


/*
Expand Down Expand Up @@ -1754,7 +1754,7 @@ static int btreeInitPage(MemPage *pPage){

if( !pPage->leaf ) iCellLast--;
for(i=0; i<pPage->nCell; i++){
pc = get2byte(&data[cellOffset+i*2]);
pc = get2byteAligned(&data[cellOffset+i*2]);
testcase( pc==iCellFirst );
testcase( pc==iCellLast );
if( pc<iCellFirst || pc>iCellLast ){
Expand Down Expand Up @@ -6625,7 +6625,7 @@ static int editPage(
#ifdef SQLITE_DEBUG
for(i=0; i<nNew && !CORRUPT_DB; i++){
u8 *pCell = pCArray->apCell[i+iNew];
int iOff = get2byte(&pPg->aCellIdx[i*2]);
int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
pCell = &pTmp[pCell - aData];
}
Expand Down Expand Up @@ -7127,7 +7127,7 @@ static int balance_nonroot(
memset(&b.szCell[b.nCell+limit], 0, sizeof(b.szCell[0])*pOld->nOverflow);
limit = pOld->aiOvfl[0];
for(j=0; j<limit; j++){
b.apCell[b.nCell] = aData + (maskPage & get2byte(piCell));
b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
piCell += 2;
b.nCell++;
}
Expand All @@ -7140,7 +7140,7 @@ static int balance_nonroot(
piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
while( piCell<piEnd ){
assert( b.nCell<nMaxCells );
b.apCell[b.nCell] = aData + (maskPage & get2byte(piCell));
b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
piCell += 2;
b.nCell++;
}
Expand Down Expand Up @@ -9105,7 +9105,7 @@ static int checkTreePage(
/* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
** integer offsets to the cell contents. */
for(i=0; i<nCell; i++){
int pc = get2byte(&data[cellStart+i*2]);
int pc = get2byteAligned(&data[cellStart+i*2]);
u32 size = 65536;
if( pc<=usableSize-4 ){
size = pPage->xCellSize(pPage, &data[pc]);
Expand Down
13 changes: 13 additions & 0 deletions src/btreeInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,16 @@ struct IntegrityCk {
#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
#define get4byte sqlite3Get4byte
#define put4byte sqlite3Put4byte

/*
** get2byteAligned(), unlike get2byte(), requires that its argument point to a
** two-byte aligned address. get2bytea() is only used for accessing the
** cell addresses in a btree header.
*/
#if SQLITE_BYTEORDER==4321
# define get2byteAligned(x) (*(u16*)(x))
#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
#else
# define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
#endif
7 changes: 0 additions & 7 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,6 @@ u32 sqlite3Get4byte(const u8 *p){
u32 x;
memcpy(&x,p,4);
return x;
#elif defined(_MSC_VER)
u32 x;
memcpy(&x,p,4);
return htonl(x);
#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
u32 x;
memcpy(&x,p,4);
Expand All @@ -1098,9 +1094,6 @@ u32 sqlite3Get4byte(const u8 *p){
void sqlite3Put4byte(unsigned char *p, u32 v){
#if SQLITE_BYTEORDER==4321
memcpy(p,&v,4);
#elif defined(_MSC_VER)
u32 x = htonl(v);
memcpy(&x,p,4);
#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
u32 x = __builtin_bswap32(v);
memcpy(p,&x,4);
Expand Down

0 comments on commit 329428e

Please sign in to comment.