1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Precompute the nDim2 value in the Rtree object and use that to make loops

over coordinates faster.

FossilOrigin-Name: f1f3c8cc733a05c12dd980f2dfa0ab4ccd76c04b
This commit is contained in:
drh
2017-02-01 15:49:02 +00:00
parent 03626e3812
commit 0e6f67b754
3 changed files with 32 additions and 25 deletions

View File

@ -116,6 +116,7 @@ struct Rtree {
sqlite3 *db; /* Host database connection */ sqlite3 *db; /* Host database connection */
int iNodeSize; /* Size in bytes of each node in the node table */ int iNodeSize; /* Size in bytes of each node in the node table */
u8 nDim; /* Number of dimensions */ u8 nDim; /* Number of dimensions */
u8 nDim2; /* Twice the number of dimensions */
u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */ u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
u8 nBytesPerCell; /* Bytes consumed per cell */ u8 nBytesPerCell; /* Bytes consumed per cell */
int iDepth; /* Current depth of the r-tree structure */ int iDepth; /* Current depth of the r-tree structure */
@ -711,7 +712,7 @@ static void nodeOverwriteCell(
int ii; int ii;
u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell]; u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
p += writeInt64(p, pCell->iRowid); p += writeInt64(p, pCell->iRowid);
for(ii=0; ii<(pRtree->nDim*2); ii++){ for(ii=0; ii<pRtree->nDim2; ii++){
p += writeCoord(p, &pCell->aCoord[ii]); p += writeCoord(p, &pCell->aCoord[ii]);
} }
pNode->isDirty = 1; pNode->isDirty = 1;
@ -854,7 +855,7 @@ static void nodeGetCell(
readCoord(pData+4, &pCoord[ii+1]); readCoord(pData+4, &pCoord[ii+1]);
pData += 8; pData += 8;
ii += 2; ii += 2;
}while( ii<pRtree->nDim*2 ); }while( ii<pRtree->nDim2 );
} }
@ -1712,7 +1713,7 @@ static int rtreeFilter(
if( rc!=SQLITE_OK ){ if( rc!=SQLITE_OK ){
break; break;
} }
p->pInfo->nCoord = pRtree->nDim*2; p->pInfo->nCoord = pRtree->nDim2;
p->pInfo->anQueue = pCsr->anQueue; p->pInfo->anQueue = pCsr->anQueue;
p->pInfo->mxLevel = pRtree->iDepth + 1; p->pInfo->mxLevel = pRtree->iDepth + 1;
}else{ }else{
@ -1878,10 +1879,11 @@ static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
** Return the N-dimensional volumn of the cell stored in *p. ** Return the N-dimensional volumn of the cell stored in *p.
*/ */
static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){ static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
RtreeDValue area = (RtreeDValue)1; RtreeDValue area;
int ii; int ii;
for(ii=0; ii<(pRtree->nDim*2); ii+=2){ area = DCOORD(p->aCoord[1]) - DCOORD(p->aCoord[0]);
area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]))); for(ii=2; ii<pRtree->nDim2; ii+=2){
area *= DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]);
} }
return area; return area;
} }
@ -1891,9 +1893,10 @@ static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
** of the objects size in each dimension. ** of the objects size in each dimension.
*/ */
static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){ static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
RtreeDValue margin = (RtreeDValue)0; RtreeDValue margin;
int ii; int ii;
for(ii=0; ii<(pRtree->nDim*2); ii+=2){ margin = DCOORD(p->aCoord[1]) - DCOORD(p->aCoord[0]);
for(ii=2; ii<pRtree->nDim2; ii+=2){
margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])); margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
} }
return margin; return margin;
@ -1903,17 +1906,19 @@ static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
** Store the union of cells p1 and p2 in p1. ** Store the union of cells p1 and p2 in p1.
*/ */
static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
int ii; int ii = 0;
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
for(ii=0; ii<(pRtree->nDim*2); ii+=2){ do{
p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f); p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f); p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
} ii += 2;
}while( ii<pRtree->nDim2 );
}else{ }else{
for(ii=0; ii<(pRtree->nDim*2); ii+=2){ do{
p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i); p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i); p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
} ii += 2;
}while( ii<pRtree->nDim2 );
} }
} }
@ -1924,7 +1929,7 @@ static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
int ii; int ii;
int isInt = (pRtree->eCoordType==RTREE_COORD_INT32); int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
for(ii=0; ii<(pRtree->nDim*2); ii+=2){ for(ii=0; ii<pRtree->nDim2; ii+=2){
RtreeCoord *a1 = &p1->aCoord[ii]; RtreeCoord *a1 = &p1->aCoord[ii];
RtreeCoord *a2 = &p2->aCoord[ii]; RtreeCoord *a2 = &p2->aCoord[ii];
if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
@ -1959,7 +1964,7 @@ static RtreeDValue cellOverlap(
for(ii=0; ii<nCell; ii++){ for(ii=0; ii<nCell; ii++){
int jj; int jj;
RtreeDValue o = (RtreeDValue)1; RtreeDValue o = (RtreeDValue)1;
for(jj=0; jj<(pRtree->nDim*2); jj+=2){ for(jj=0; jj<pRtree->nDim2; jj+=2){
RtreeDValue x1, x2; RtreeDValue x1, x2;
x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj])); x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1])); x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
@ -3015,7 +3020,7 @@ static int rtreeUpdate(
** This problem was discovered after years of use, so we silently ignore ** This problem was discovered after years of use, so we silently ignore
** these kinds of misdeclared tables to avoid breaking any legacy. ** these kinds of misdeclared tables to avoid breaking any legacy.
*/ */
assert( nData<=(pRtree->nDim*2 + 3) ); assert( nData<=(pRtree->nDim2 + 3) );
#ifndef SQLITE_RTREE_INT_ONLY #ifndef SQLITE_RTREE_INT_ONLY
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
@ -3393,7 +3398,8 @@ static int rtreeInit(
pRtree->zDb = (char *)&pRtree[1]; pRtree->zDb = (char *)&pRtree[1];
pRtree->zName = &pRtree->zDb[nDb+1]; pRtree->zName = &pRtree->zDb[nDb+1];
pRtree->nDim = (argc-4)/2; pRtree->nDim = (argc-4)/2;
pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2; pRtree->nDim2 = argc - 4;
pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
pRtree->eCoordType = eCoordType; pRtree->eCoordType = eCoordType;
memcpy(pRtree->zDb, argv[1], nDb); memcpy(pRtree->zDb, argv[1], nDb);
memcpy(pRtree->zName, argv[2], nName); memcpy(pRtree->zName, argv[2], nName);
@ -3468,6 +3474,7 @@ static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
memset(&node, 0, sizeof(RtreeNode)); memset(&node, 0, sizeof(RtreeNode));
memset(&tree, 0, sizeof(Rtree)); memset(&tree, 0, sizeof(Rtree));
tree.nDim = sqlite3_value_int(apArg[0]); tree.nDim = sqlite3_value_int(apArg[0]);
tree.nDim2 = tree.nDim*2;
tree.nBytesPerCell = 8 + 8 * tree.nDim; tree.nBytesPerCell = 8 + 8 * tree.nDim;
node.zData = (u8 *)sqlite3_value_blob(apArg[1]); node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
@ -3480,7 +3487,7 @@ static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
nodeGetCell(&tree, &node, ii, &cell); nodeGetCell(&tree, &node, ii, &cell);
sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid); sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
nCell = (int)strlen(zCell); nCell = (int)strlen(zCell);
for(jj=0; jj<tree.nDim*2; jj++){ for(jj=0; jj<tree.nDim2; jj++){
#ifndef SQLITE_RTREE_INT_ONLY #ifndef SQLITE_RTREE_INT_ONLY
sqlite3_snprintf(512-nCell,&zCell[nCell], " %g", sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
(double)cell.aCoord[jj].f); (double)cell.aCoord[jj].f);

View File

@ -1,5 +1,5 @@
C Use\scompiler\sintrinsic\sfunctions\s(when\savailable)\sfor\sbyteswapping\sin\sRTREE. C Precompute\sthe\snDim2\svalue\sin\sthe\sRtree\sobject\sand\suse\sthat\sto\smake\sloops\nover\scoordinates\sfaster.
D 2017-02-01T15:24:32.835 D 2017-02-01T15:49:02.390
F Makefile.in 5f415e7867296d678fed2e6779aea10c1318b4bc F Makefile.in 5f415e7867296d678fed2e6779aea10c1318b4bc
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc b8ca53350ae545e3562403d5da2a69cec79308da F Makefile.msc b8ca53350ae545e3562403d5da2a69cec79308da
@ -264,7 +264,7 @@ F ext/rbu/sqlite3rbu.c bb0de6cdbdb14a7d55a097238a434b7e99caf318
F ext/rbu/sqlite3rbu.h 6fb6294c34a9ca93b5894a33bca530c6f08decba F ext/rbu/sqlite3rbu.h 6fb6294c34a9ca93b5894a33bca530c6f08decba
F ext/rbu/test_rbu.c 5aa22616afac6f71ebd3d9bc9bf1006cfabcca88 F ext/rbu/test_rbu.c 5aa22616afac6f71ebd3d9bc9bf1006cfabcca88
F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761 F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
F ext/rtree/rtree.c d8ef14e964a9390197ba7e511aa47b89dc39416c F ext/rtree/rtree.c bc73bae0af0d44f92b7d6cb513b719c1e89a7a20
F ext/rtree/rtree.h 834dbcb82dc85b2481cde6a07cdadfddc99e9b9e F ext/rtree/rtree.h 834dbcb82dc85b2481cde6a07cdadfddc99e9b9e
F ext/rtree/rtree1.test 42dadfc7b44a436cd74a1bebc0b9b689e4eaf7ec F ext/rtree/rtree1.test 42dadfc7b44a436cd74a1bebc0b9b689e4eaf7ec
F ext/rtree/rtree2.test acbb3a4ce0f4fbc2c304d2b4b784cfa161856bba F ext/rtree/rtree2.test acbb3a4ce0f4fbc2c304d2b4b784cfa161856bba
@ -1552,7 +1552,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 510933cb24c5bf883265af3a6075e60a4b5ffa37 P 82fcd54a5941c20895ffc22d8009c1ebdae44eda
R 13778722a0ec2443f74339612adbbcf9 R 2d19b39f1fe0fa1365532dfac9c432a7
U drh U drh
Z b1e5b219635e474580d8132f194fff51 Z 1d3fc5da859da34aeaaaf616405346d2

View File

@ -1 +1 @@
82fcd54a5941c20895ffc22d8009c1ebdae44eda f1f3c8cc733a05c12dd980f2dfa0ab4ccd76c04b