geo icon indicating copy to clipboard operation
geo copied to clipboard

Experiment with wasm bindings for rust-geo

Open frewsxcv opened this issue 7 years ago • 8 comments

We could use https://github.com/rustwasm/wasm-bindgen to create a JavaScript library that utilizes rust-geo via wasm. Could be fun to see how performance compares to libraries like Turf.js

frewsxcv avatar Jun 03 '18 17:06 frewsxcv

Related to using rust-geo with wasm (at least as a dependency), spade is currently on v1.5.1, which depends on the now deprecated rustc-serialize through num=0.1. The num dependency has been updated, and from https://github.com/Stoeoef/spade/commit/4dc1f7f190e4bc0bdff5e59592c2001e2b4fca32, it looks like a version change is all it took.

If and when spade releases an update, it would probably become possible to use geo with wasm projects.

nevi-me avatar Oct 12 '18 01:10 nevi-me

I just published Spade v1.6.0, which should by default use num version 0.2.0 .

Stoeoef avatar Nov 01 '18 15:11 Stoeoef

Related to this, I used rust-geo and the r-star crate to make a site that does point in polygon aggregation in the browser really quickly using WASM.

code: https://github.com/stuartlynn/wasm_geo_agg live demo: https://stuartlynn.github.io/wasm_geo_agg/

It's surprisingly fast and I think there is a lot of other usecases that can be supported by more generally exposing the rust-geo ecosystem to the browser through WASM.

stuartlynn avatar Nov 04 '19 21:11 stuartlynn

@stuartlynn nicely done! I am currently working on something very similar. I am implementing the same interface as this function http://turfjs.org/docs/#pointsWithinPolygon and benchmark GeoRust WASM vs Turf.js using benchmark.js. I was planning on writing a blog post about it actually. Will definitely credit and link to you your demo and github.

guidorice avatar Nov 04 '19 21:11 guidorice

Thanks! Would be very interested in seeing those benchmarks. A rust-geo backed port of turf would be really cool. One thing I am curious about is how to handle large datasets like the 2Gb taxi pickup table I was playing with. My instinct was to keep the data in memory mostly on the WASM side to avoid continually transporting lots of data back and forth between JS and WASM. I know that can incur a pretty big penalty. Be interested in how that plays out in the benchmarks.

stuartlynn avatar Nov 04 '19 22:11 stuartlynn

I'd be really interested in seeing some benchmarks too…

urschrei avatar Nov 05 '19 11:11 urschrei

Here is my crate, although be warned it's likely amateurish and un-idiomatic 😄

https://github.com/guidorice/points-within-polygons

I was hoping for 10X faster than JavaScript across the board, but alas. The good news is there are six benchmark datasets and Rust was faster in 5/6 of them. It also seems to have a nice scaling pattern where the more points you throw at it, the better it does. The algorithm is very sensitive to the complexity of the polygons as you can see.

I am going to write a blog post about it. Mostly exploring what it's like to learn Rust, and my approach to this particular experiment. And acknowledging that it could be optimized quite a lot, for example by using a Rtree as in Stuart's wasm_geo_agg crate. Or even further by parallelizing it, when wasm-bindgen gets support for WebWorkers. There are a few papers floating around about parallel PIP algorithm. Maybe it will become 3 part blog series. 💭

guidorice avatar Nov 07 '19 22:11 guidorice

Nice. Will check it out. My code was similarly pretty amateurish :-) Yeah I think the main wins here are going to be when you have a lot of data. I might try and benchmark the r-tree + point in poly approach in JS vs Rust to see how it scales as you add more points / polygons. Will keep you posted if I get any interesting results.

Looking forward to reading the blogs and let me know if there is anything I can do to add to them!

stuartlynn avatar Nov 08 '19 15:11 stuartlynn

I'm going to close this since there's nothing really actionable here. If you want to see geo in use in wasm, check out https://rgis.app (https://github.com/frewsxcv/rgis)

frewsxcv avatar Dec 27 '22 20:12 frewsxcv