From 4eb2b3b8cae5cd12ab7d7bba5065b8bec2a08010 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ra=C3=BAl=20Mar=C3=ADn=20Rodr=C3=ADguez?= Date: Wed, 19 Jun 2019 15:16:15 +0000 Subject: [PATCH] PROJ: Avoid double lookups References #4372 git-svn-id: http://svn.osgeo.org/postgis/trunk@17543 b70326c6-7e19-0410-871a-916f4a2858ee --- libpgcommon/lwgeom_transform.c | 40 ++++++++++------------------------ 1 file changed, 11 insertions(+), 29 deletions(-) diff --git a/libpgcommon/lwgeom_transform.c b/libpgcommon/lwgeom_transform.c index c65d29d0b..caf0474df 100644 --- a/libpgcommon/lwgeom_transform.c +++ b/libpgcommon/lwgeom_transform.c @@ -89,9 +89,7 @@ static LWPROJ *GetPJHashEntry(MemoryContext mcxt); static void AddPJHashEntry(MemoryContext mcxt, LWPROJ *projection); /* Internal Cache API */ -/* static PROJPortalCache *GetPROJSRSCache(FunctionCallInfo fcinfo) ; */ -static bool IsInPROJSRSCache(PROJPortalCache *PROJCache, int32_t srid_from, int32_t srid_to); -static void AddToPROJSRSCache(PROJPortalCache *PROJCache, int32_t srid_from, int32_t srid_to); +static LWPROJ *AddToPROJSRSCache(PROJPortalCache *PROJCache, int32_t srid_from, int32_t srid_to); static void DeleteFromPROJSRSCache(PROJPortalCache *PROJCache, int32_t srid_from, int32_t srid_to); /* Search path for PROJ.4 library */ @@ -339,29 +337,11 @@ static void DeletePJHashEntry(MemoryContext mcxt) * Per-cache management functions */ -static bool -IsInPROJSRSCache(PROJPortalCache *cache, int32_t srid_from, int32_t srid_to) -{ - /* - * Return true/false depending upon whether the item - * is in the SRS cache. - */ - uint32_t i; - for (i = 0; i < PROJ_CACHE_ITEMS; i++) - { - if (cache->PROJSRSCache[i].srid_from == srid_from && - cache->PROJSRSCache[i].srid_to == srid_to) - return true; - } - /* Otherwise not found */ - return false; -} - static LWPROJ * GetProjectionFromPROJCache(PROJPortalCache *cache, int32_t srid_from, int32_t srid_to) { uint32_t i; - for (i = 0; i < PROJ_CACHE_ITEMS; i++) + for (i = 0; i < cache->PROJSRSCacheCount; i++) { if (cache->PROJSRSCache[i].srid_from == srid_from && cache->PROJSRSCache[i].srid_to == srid_to) @@ -622,7 +602,7 @@ GetProj4String(int32_t srid) * we must make sure the entry we choose to delete does not contain other_srid * which is the definition for the other half of the transformation. */ -static void +static LWPROJ * AddToPROJSRSCache(PROJPortalCache *PROJCache, int32_t srid_from, int32_t srid_to) { MemoryContext PJMemoryContext; @@ -677,13 +657,13 @@ AddToPROJSRSCache(PROJPortalCache *PROJCache, int32_t srid_from, int32_t srid_to if (!projpj) { elog(ERROR, "could not form projection (PJ) from 'srid=%d' to 'srid=%d'", srid_from, srid_to); - return; + return NULL; } LWPROJ *projection = lwproj_from_PJ(projpj, srid_from == srid_to); if (!projection) { elog(ERROR, "could not form projection (LWPROJ) from 'srid=%d' to 'srid=%d'", srid_from, srid_to); - return; + return NULL; } #endif @@ -766,6 +746,8 @@ AddToPROJSRSCache(PROJPortalCache *PROJCache, int32_t srid_from, int32_t srid_to PROJCache->PROJSRSCache[PROJCache->PROJSRSCacheCount].projection = projection; PROJCache->PROJSRSCache[PROJCache->PROJSRSCacheCount].projection_mcxt = PJMemoryContext; PROJCache->PROJSRSCacheCount++; + + return projection; } static void @@ -858,11 +840,11 @@ GetPJUsingFCInfo(FunctionCallInfo fcinfo, int32_t srid_from, int32_t srid_to, LW return LW_FAILURE; /* Add the output srid to the cache if it's not already there */ - if (!IsInPROJSRSCache(proj_cache, srid_from, srid_to)) - AddToPROJSRSCache(proj_cache, srid_from, srid_to); - - /* Get the projections */ *pj = GetProjectionFromPROJCache(proj_cache, srid_from, srid_to); + if (*pj == NULL) + { + *pj = AddToPROJSRSCache(proj_cache, srid_from, srid_to); + } return LW_SUCCESS; } -- 2.50.0