Closed x4d3 closed 2 years ago
Thank you for the detailed/reproducible issue. I'm seeing a similar performance regression when I tested your script. I think I have an idea what it might be, but I'll do some digging and report progress as it comes up.
So I looked into it and the cause is what I suspected. In newer versions of proj, you need to create a crs_to_crs projection and then transform coordinates with that, whereas in older versions you would pass 2 separate CRS's into the transform function. I'm not sure exactly how the internals worked, but I assume they cached the mapping somehow, but now they don't since they give us the crs_to_crs object back.
It takes about 0.002s for me to create the new crs_to_crs, so doing this 1000 times is ~2s.
I think the best solution to this is to do the following:
1) Create a new class RGeo::CoordSys::CRSToCRS
that is instantiated from 2 RGeo::CoordSys::Proj4
objects and stores the crs_to_crs.
2) Create a singleton class RGeo::CoordSys::CRSStore
that maps some combination of Proj4
objects to a CRSToCRS
object.
3) When running transform_coords
, check the CRSStore
to see if the mapping exists, if not create it and store it, else get it.
4) Change the underlying C function to accept the crs_to_crs instead of 2 projections and just transform the coordinates.
Reference to see differences in C library between versions: https://proj.org/development/migration.html
Thanks @keithdoggett, I had a stab at fixing it in #20
Issue encountered
I've tried to update to the 3.0.1 version but I've observed that using
RGeo::CoordSys::Proj4.transform_coords
to project the coordinates of my geometry was slower than the 2.0.1 version.Step to reproduce
Here is a minimal script to reproduce, you can uncomment to alternate between the 2 versions.
With 2.0.1
With 3.0.1
Versions
7.2.1
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
x86_64-darwin19
Note
I've tried to use WKT definition as well, it was faster but it was still slower than the
2.0.1
versionResult
Docker image to reproduce
Here is a gist with a docker image where I've reproduced the issue with proj version:
8.1.0
https://gist.github.com/x4d3/1dff5f2348a0a6ec1a6fd440d0d64ac7