From: Paul Ramsey Date: Sun, 12 Oct 2008 17:47:37 +0000 (+0000) Subject: Performance boost: only detoast the front of the tuple first and X-Git-Tag: 1.4.0b1~634 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=57586b2893de193801692710e08f133a0cb4ff87;p=postgis Performance boost: only detoast the front of the tuple first and extract the bbox from that. git-svn-id: http://svn.osgeo.org/postgis/trunk@3091 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/lwgeom/lwgeom_gist.c b/lwgeom/lwgeom_gist.c index 880983e17..924e0513e 100644 --- a/lwgeom/lwgeom_gist.c +++ b/lwgeom/lwgeom_gist.c @@ -551,6 +551,7 @@ Datum LWGEOM_gist_consistent(PG_FUNCTION_ARGS) 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 @@ Datum LWGEOM_gist_consistent(PG_FUNCTION_ARGS) 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 @@ Datum LWGEOM_gist_consistent(PG_FUNCTION_ARGS) 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))