[postgis-commits] svn - r3091 - trunk/lwgeom

postgis-commits at postgis.refractions.net postgis-commits at postgis.refractions.net
Sun Oct 12 10:47:37 PDT 2008


Author: pramsey
Date: 2008-10-12 10:47:37 -0700 (Sun, 12 Oct 2008)
New Revision: 3091

Modified:
   trunk/lwgeom/lwgeom_gist.c
Log:
Performance boost: only detoast the front of the tuple first and 
extract the bbox from that.


Modified: trunk/lwgeom/lwgeom_gist.c
===================================================================
--- trunk/lwgeom/lwgeom_gist.c	2008-10-10 18:17:42 UTC (rev 3090)
+++ trunk/lwgeom/lwgeom_gist.c	2008-10-12 17:47:37 UTC (rev 3091)
@@ -551,6 +551,7 @@
 	PG_LWGEOM *query ; /* lwgeom serialized form */
 	StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
 	bool result;
+	uchar *serialized_lwgeom;
 	BOX2DFLOAT4  box;
 
 #if POSTGIS_PGSQL_VERSION >= 84
@@ -571,7 +572,11 @@
 		PG_RETURN_BOOL(false); /* null query - this is screwy! */
 	}
 
-	query = (PG_LWGEOM*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+	/* 
+	** First pull only a small amount of the tuple, enough to 
+	** get the bounding box, if one exists.
+	*/
+	query = (PG_LWGEOM*)PG_DETOAST_DATUM_SLICE(PG_GETARG_DATUM(1), 0, VARHDRSZ + 1 + sizeof(BOX2DFLOAT4) );
 
 	if ( ! (DatumGetPointer(entry->key) != NULL && query) )
 	{
@@ -580,10 +585,24 @@
 		PG_RETURN_BOOL(FALSE);
 	}
 
-	if ( ! getbox2d_p(SERIALIZED_FORM(query), &box) )
+	/*
+	** If the bounding box exists, copy it into the working variable.
+	** If not, pull the full toasted data out, and call the standard box 
+	** retrieval function, which will calculate the box from scratch.
+	*/
+	serialized_lwgeom = SERIALIZED_FORM(query);
+	if( lwgeom_hasBBOX(serialized_lwgeom[0]) ) 
 	{
-		PG_FREE_IF_COPY(query, 1);
-		PG_RETURN_BOOL(FALSE);
+		memcpy(&box, serialized_lwgeom + 1, sizeof(BOX2DFLOAT4));
+	} 
+	else 
+	{
+		query = (PG_LWGEOM*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); 
+		if ( ! getbox2d_p(SERIALIZED_FORM(query), &box) )
+		{
+			PG_FREE_IF_COPY(query, 1);
+			PG_RETURN_BOOL(FALSE);
+		}
 	}
 
 	if (GIST_LEAF(entry))



More information about the postgis-commits mailing list