paulmach / orb

Types and utilities for working with 2d geometry in Golang
MIT License
886 stars 103 forks source link

geojson: marshal/unmarshal BSON #123

Closed paulmach closed 1 year ago

paulmach commented 1 year ago

Requested in https://github.com/paulmach/orb/issues/122

This PR adds BSON marshaling and unmarshaling support to the geojson types. The goal is to mimic the functionality of the JSON. This should allow the types to work seamlessly with the mongodb driver.

Benchmarks for JSON vs. BSON are interesting. BSON is faster but uses much more memory.

BenchmarkFeatureMarshalJSON-10           2916        2038514 ns/op        748287 B/op     7234 allocs/op
BenchmarkFeatureMarshalBSON-10           4378        1369295 ns/op       1100787 B/op    12993 allocs/op
BenchmarkFeatureUnmarshalJSON-10         1477        4060850 ns/op        766427 B/op    23047 allocs/op
BenchmarkFeatureUnmarshalBSON-10         2944        2074189 ns/op       1490398 B/op    32654 allocs/op
BenchmarkGeometryMarshalJSON-10         23251         258126 ns/op         49462 B/op        3 allocs/op
BenchmarkGeometryMarshalBSON-10         49639         121049 ns/op         69519 B/op      909 allocs/op
BenchmarkGeometryUnmarshalJSON-10        8845         675808 ns/op         79992 B/op     2046 allocs/op
BenchmarkGeometryUnmarshalBSON-10       19322         312273 ns/op        272869 B/op     6020 allocs/op
jclaessens97 commented 1 year ago

Works like a charm 👌

Thanks a lot, @paulmach 🙏