postgis.git / summary / log / commit / refs

commit    5a8548cef1237bdffd9f65edba48b34970af4e23
Author:   Raúl Marín <git@rmr.ninja>
Date:     Tue Jul 21 14:24:46 2020 +0000

    Reduce the overhead of reading multiple values from the headers


libpgcommon/gserialized_gist.c | 36 ------------------ libpgcommon/gserialized_gist.h | 4 +- libpgcommon/lwgeom_pg.h | 6 +-- postgis/gserialized_gist_2d.c | 84 ++++++++++++++++++++---------------------- postgis/lwgeom_geos.c | 7 ++-- 5 files changed, 45 insertions(+), 92 deletions(-) diff --git a/libpgcommon/gserialized_gist.c b/libpgcommon/gserialized_gist.c index 89a391d35..41db342d1 100644 --- a/libpgcommon/gserialized_gist.c +++ b/libpgcommon/gserialized_gist.c @@ -70,12 +70,6 @@ GIDX* gidx_new(int ndims) return g; } -static lwflags_t -gserialized_datum_get_flags(Datum gsdatum) -{ - GSERIALIZED *gpart = (GSERIALIZED *)PG_DETOAST_DATUM_SLICE(gsdatum, 0, gserialized_max_header_size()); - return gserialized_get_lwflags(gpart); -} /* Convert a double-based GBOX into a float-based GIDX, ensuring the float box is larger than the double box */ @@ -144,36 +138,6 @@ void gbox_from_gidx(GIDX *a, GBOX *gbox, int flags) } -/** -* Given a #GSERIALIZED datum, as quickly as possible (peaking into the top -* of the memory) return the gbox extents. Does not deserialize the geometry, -* but <em>WARNING</em> returns a slightly larger bounding box than actually -* encompasses the objects. For geography objects returns geocentric bounding -* box, for geometry objects returns cartesian bounding box. -*/ -int -gserialized_datum_get_gbox_p(Datum gsdatum, GBOX *gbox) -{ - char gboxmem[GIDX_MAX_SIZE]; - GIDX *gidx = (GIDX*)gboxmem; - - if( LW_FAILURE == gserialized_datum_get_gidx_p(gsdatum, gidx) ) - return LW_FAILURE; - - gbox->flags = gserialized_datum_get_flags(gsdatum); - gbox_from_gidx(gidx, gbox, gbox->flags); - - return LW_SUCCESS; -} - -void -gserialized_datum_get_type_and_srid_p(Datum gsdatum, uint8_t *type, int32_t *srid) -{ - GSERIALIZED *gpart = (GSERIALIZED *)PG_DETOAST_DATUM_SLICE(gsdatum, 0, gserialized_max_header_size()); - *type = gserialized_get_type(gpart); - *srid = gserialized_get_srid(gpart); -} - /** * Peak into a #GSERIALIZED datum to find the bounding box. If the * box is there, copy it out and return it. If not, calculate the box from the diff --git a/libpgcommon/gserialized_gist.h b/libpgcommon/gserialized_gist.h index 91a961ba3..b3e0ca75a 100644 --- a/libpgcommon/gserialized_gist.h +++ b/libpgcommon/gserialized_gist.h @@ -100,8 +100,9 @@ int box2df_to_gbox_p(BOX2DF *a, GBOX *box); ** Fast functions for pulling boxes out of serializations. */ -/* Pull out the #GIDX bounding box with a absolute minimum system overhead */ +/* Pull out the #GIDX bounding box and flags with a absolute minimum system overhead */ int gserialized_datum_get_gidx_p(Datum gserialized_datum, GIDX *gidx); +int gserialized_datum_get_box2df_p(Datum gsdatum, BOX2DF *box2df); bool box2df_contains(const BOX2DF *a, const BOX2DF *b); void box2df_set_empty(BOX2DF *a); @@ -123,4 +124,3 @@ void gidx_set_unknown(GIDX *a); bool gidx_overlaps(GIDX *a, GIDX *b); bool gidx_equals(GIDX *a, GIDX *b); bool gidx_contains(GIDX *a, GIDX *b); -int gserialized_datum_get_box2df_p(Datum gsdatum, BOX2DF *box2df); diff --git a/libpgcommon/lwgeom_pg.h b/libpgcommon/lwgeom_pg.h index 032678d71..8908d9b0d 100644 --- a/libpgcommon/lwgeom_pg.h +++ b/libpgcommon/lwgeom_pg.h @@ -185,11 +185,7 @@ void gserialized_error_if_srid_mismatch_reference(const GSERIALIZED *g1, const i * Fails on empty. */ int gserialized_datum_get_gbox_p(Datum gsdatum, GBOX *gbox); - -/** - * Pull the type and srid of a datum (containing a GSERIALIZEDs) - */ -void gserialized_datum_get_type_and_srid_p(Datum gsdatum, uint8_t *type, int32_t *srid); +int gserialized_datum_get_internals_p(Datum gsdatum, GBOX *gbox, uint8_t *type, int32_t *srid); /* PG-exposed */ Datum BOX2D_same(PG_FUNCTION_ARGS); diff --git a/postgis/gserialized_gist_2d.c b/postgis/gserialized_gist_2d.c index e5b2c2702..5193b2724 100644 --- a/postgis/gserialized_gist_2d.c +++ b/postgis/gserialized_gist_2d.c @@ -450,21 +450,17 @@ static double box2df_distance(const BOX2DF *a, const BOX2DF *b) return FLT_MAX; } - /** -* Peak into a #GSERIALIZED datum to find the bounding box. If the -* box is there, copy it out and return it. If not, calculate the box from the -* full object and return the box based on that. If no box is available, -* return #LW_FAILURE, otherwise #LW_SUCCESS. -*/ + * Peak into a #GSERIALIZED datum to find its bounding box and some other metadata. If the box is there, copy it out and + * return it. If not, calculate the box from the full object and return the box based on that. If no box is available, + * return #LW_FAILURE, otherwise #LW_SUCCESS. + */ int -gserialized_datum_get_box2df_p(Datum gsdatum, BOX2DF *box2df) +gserialized_datum_get_internals_p(Datum gsdatum, GBOX *gbox, uint8_t *type, int32_t *srid) { GSERIALIZED *gpart; int result = LW_SUCCESS; - POSTGIS_DEBUG(4, "entered function"); - /* ** Because geometry is declared as "storage = main" anything large ** enough to take serious advantage of PG_DETOAST_DATUM_SLICE will have @@ -476,50 +472,48 @@ gserialized_datum_get_box2df_p(Datum gsdatum, BOX2DF *box2df) ** which makes slicing worthwhile. */ gpart = (GSERIALIZED *)PG_DETOAST_DATUM_SLICE(gsdatum, 0, gserialized_max_header_size()); - - POSTGIS_DEBUGF(4, "got flags %d", gpart->gflags); - - /* Do we even have a serialized bounding box? */ - if (gserialized_has_bbox(gpart)) + if (!gserialized_has_bbox(gpart) && LWSIZE_GET(gpart->size) >= gserialized_max_header_size()) { - /* Yes! Copy it out into the box! */ - size_t box_ndims; - const float *f = gserialized_get_float_box_p(gpart, &box_ndims); - - POSTGIS_DEBUG(4, "copying box out of serialization"); - memcpy(box2df, f, sizeof(BOX2DF)); - result = LW_SUCCESS; - } - else - { - /* No, we need to calculate it from the full object. */ - GBOX gbox; - gbox_init(&gbox); - - /* If we haven't, read the whole gserialized object */ - if (LWSIZE_GET(gpart->size) >= gserialized_max_header_size()) - { - POSTGIS_FREE_IF_COPY_P(gpart, gsdatum); - gpart = (GSERIALIZED *)PG_DETOAST_DATUM(gsdatum); - } - - result = gserialized_get_gbox_p(gpart, &gbox); - if ( result == LW_SUCCESS ) - { - result = box2df_from_gbox_p(&gbox, box2df); - } - else - { - POSTGIS_DEBUG(4, "could not calculate bbox"); - } + /* The headers don't contain a bbox and there is the object is larger than what we retrieved, so + * we now detoast it completely */ + POSTGIS_FREE_IF_COPY_P(gpart, gsdatum); + gpart = (GSERIALIZED *)PG_DETOAST_DATUM(gsdatum); } + result = gserialized_get_gbox_p(gpart, gbox); POSTGIS_FREE_IF_COPY_P(gpart, gsdatum); - POSTGIS_DEBUGF(4, "result = %d, got box2df %s", result, result == LW_SUCCESS ? box2df_to_string(box2df) : "NONE"); + + *srid = gserialized_get_srid(gpart); + *type = gserialized_get_type(gpart); return result; } +/** + * Given a #GSERIALIZED datum, as quickly as possible (peaking into the top + * of the memory) return the gbox extents. Does not deserialize the geometry, + * but <em>WARNING</em> returns a slightly larger bounding box than actually + * encompasses the objects. For geography objects returns geocentric bounding + * box, for geometry objects returns cartesian bounding box. + */ +int +gserialized_datum_get_gbox_p(Datum gsdatum, GBOX *gbox) +{ + uint8_t type; + int32_t srid; + + return gserialized_datum_get_internals_p(gsdatum, gbox, &type, &srid); +} + +int +gserialized_datum_get_box2df_p(Datum gsdatum, BOX2DF *box2df) +{ + GBOX gbox; + if (gserialized_datum_get_gbox_p(gsdatum, &gbox) == LW_FAILURE) + return LW_FAILURE; + + return box2df_from_gbox_p(&gbox, box2df); +} /** * Support function. Based on two datums return true if diff --git a/postgis/lwgeom_geos.c b/postgis/lwgeom_geos.c index 9c328d748..a303e50ae 100644 --- a/postgis/lwgeom_geos.c +++ b/postgis/lwgeom_geos.c @@ -1507,8 +1507,10 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) LWGEOM *lwgeom1, *lwresult ; GBOX bbox1 = {0}; GBOX *bbox2; + uint8_t type; + int32_t srid; - if (!gserialized_datum_get_gbox_p(PG_GETARG_DATUM(geom_idx), &bbox1)) + if (!gserialized_datum_get_internals_p(PG_GETARG_DATUM(geom_idx), &bbox1, &type, &srid)) { /* empty clips to empty, no matter rect */ PG_RETURN_DATUM(PG_GETARG_DATUM(geom_idx)); @@ -1527,9 +1529,6 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) /* If bbox1 outside of bbox2, return empty */ if (!gbox_overlaps_2d(&bbox1, bbox2)) { - uint8_t type; - int32_t srid; - gserialized_datum_get_type_and_srid_p(PG_GETARG_DATUM(geom_idx), &type, &srid); /* Get type and srid from datum */ lwresult = lwgeom_construct_empty(type, srid, 0, 0); result = geometry_serialize(lwresult) ; [parent: 7b3effebd8b0]