Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/wkt: improve unmarshalling performance #142

Merged
merged 2 commits into from
Jan 11, 2024
Merged

Conversation

paulmach
Copy link
Owner

back in #128 we started to split strings by regexp to handle white space around parenthesis. This reduced the WKT unmarshalling performance significantly as described in #132

This PR fine tunes the unmarshalling by making a number of changes, most notably:

  • pre-allocating slices
  • manually splitting points, not using a regexp
  • using strings.Cut to split point coordinates
  • better searching of the "type".

The current tests, and a few more, continue to pass, so I think these changes are safe.

Benchmarks show a big improvements.

Benchmarks vs the current master

benchmark                                   old ns/op     new ns/op     delta
BenchmarkUnmarshalPoint-10                  202           97.1          -51.97%
BenchmarkUnmarshalLineString-10             4021          505           -87.43%
BenchmarkUnmarshalLineString_small-10       688           145           -78.96%
BenchmarkUnmarshalPolygon-10                4159          594           -85.71%
BenchmarkUnmarshalMultiPolygon_small-10     2874          1111          -61.34%
BenchmarkUnmarshalMultiPolygon-10           240517        31122         -87.06%

benchmark                                   old allocs     new allocs     delta
BenchmarkUnmarshalPoint-10                  3              1              -66.67%
BenchmarkUnmarshalLineString-10             14             2              -85.71%
BenchmarkUnmarshalLineString_small-10       9              2              -77.78%
BenchmarkUnmarshalPolygon-10                16             3              -81.25%
BenchmarkUnmarshalMultiPolygon_small-10     35             11             -68.57%
BenchmarkUnmarshalMultiPolygon-10           615            36             -94.15%

benchmark                                   old bytes     new bytes     delta
BenchmarkUnmarshalPoint-10                  80            16            -80.00%
BenchmarkUnmarshalLineString-10             779           88            -88.70%
BenchmarkUnmarshalLineString_small-10       459           56            -87.80%
BenchmarkUnmarshalPolygon-10                819           112           -86.32%
BenchmarkUnmarshalMultiPolygon_small-10     2127          788           -62.95%
BenchmarkUnmarshalMultiPolygon-10           51058         5932          -88.38%

Benchmarks vs v0.9.0 (before we started using regexp)

benchmark                                   old ns/op     new ns/op     delta
BenchmarkUnmarshalPoint-10                  201           97.1          -51.78%
BenchmarkUnmarshalLineString_small-10       311           145           -53.44%
BenchmarkUnmarshalLineString-10             796           505           -36.55%
BenchmarkUnmarshalPolygon-10                874           594           -32.00%
BenchmarkUnmarshalMultiPolygon_small-10     1113          1111          -0.18%
BenchmarkUnmarshalMultiPolygon-10           43018         31122         -27.65%

benchmark                                   old allocs     new allocs     delta
BenchmarkUnmarshalPoint-10                  3              1              -66.67%
BenchmarkUnmarshalLineString_small-10       7              2              -71.43%
BenchmarkUnmarshalLineString-10             10             2              -80.00%
BenchmarkUnmarshalPolygon-10                12             3              -75.00%
BenchmarkUnmarshalMultiPolygon_small-10     25             11             -56.00%
BenchmarkUnmarshalMultiPolygon-10           345            36             -89.57%

benchmark                                   old bytes     new bytes     delta
BenchmarkUnmarshalPoint-10                  80            16            -80.00%
BenchmarkUnmarshalLineString_small-10       184           56            -69.57%
BenchmarkUnmarshalLineString-10             440           88            -80.00%
BenchmarkUnmarshalPolygon-10                480           112           -76.67%
BenchmarkUnmarshalMultiPolygon_small-10     752           788           +4.79%
BenchmarkUnmarshalMultiPolygon-10           29664         5932          -80.00%

benchmarks run on my m1 macbook.

@paulmach paulmach merged commit f03c361 into master Jan 11, 2024
2 checks passed
@paulmach paulmach deleted the wkt-unmarshalling branch January 11, 2024 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant