Make a sanitize_geos_geometry() to replace as_geos_geometry() (because the latter is used for almost every function to sanitize the input). Probably small improvement but also probably not hard.
Use the global handler instead of running GEOS_init_r()/GEOS_finish_r() for each function. This is also better for safety because there are some places where a failed R alloc will longjmp and leak the handler.
library(geos)
g <- geos_geometry()
san_geom <- function(x) {
if (inherits(x, "geos_geometry")) x else as_geos_geometry(x)
}
bench::mark(geos_geometry(g))
#> # A tibble: 1 x 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 geos_geometry(g) 6.17µs 10.2µs 69568. 0B 20.9
bench::mark(as_geos_geometry(g), san_geom(g))
#> # A tibble: 2 x 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 as_geos_geometry(g) 1.37µs 1.61µs 557033. 3.51KB 0
#> 2 san_geom(g) 496ns 666ns 814815. 0B 0
Two improvements:
sanitize_geos_geometry()
to replaceas_geos_geometry()
(because the latter is used for almost every function to sanitize the input). Probably small improvement but also probably not hard.GEOS_init_r()
/GEOS_finish_r()
for each function. This is also better for safety because there are some places where a failed R alloc will longjmp and leak the handler.Created on 2021-05-02 by the reprex package (v0.3.0)
Created on 2021-05-02 by the reprex package (v0.3.0)