Skip to content

Commit

Permalink
encoding/wkt: improve unmarshalling performance
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Jan 10, 2024
1 parent 3ecd611 commit 0054d7c
Show file tree
Hide file tree
Showing 4 changed files with 821 additions and 276 deletions.
115 changes: 115 additions & 0 deletions encoding/wkt/benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package wkt

import (
"encoding/json"
"io/ioutil"
"testing"

"github.com/paulmach/orb"
)

func BenchmarkUnmarshalPoint(b *testing.B) {
var mp orb.MultiPolygon
loadJSON(b, "testdata/polygon.json", &mp)

text := MarshalString(orb.Point{-81.60644531, 41.51377887})

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Unmarshal(text)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}

func BenchmarkUnmarshalLineString_small(b *testing.B) {
ls := orb.LineString{{1, 2}, {3, 4}}

text := MarshalString(ls)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Unmarshal(text)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}

func BenchmarkUnmarshalLineString(b *testing.B) {
var mp orb.MultiPolygon
loadJSON(b, "testdata/polygon.json", &mp)

text := MarshalString(orb.LineString(mp[0][0]))

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Unmarshal(text)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}

func BenchmarkUnmarshalPolygon(b *testing.B) {
var mp orb.MultiPolygon
loadJSON(b, "testdata/polygon.json", &mp)

text := MarshalString(mp[0])

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Unmarshal(text)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}

func BenchmarkUnmarshalMultiPolygon_small(b *testing.B) {
mp := orb.MultiPolygon{{{{1, 2}, {3, 4}}}, {{{5, 6}, {7, 8}}, {{1, 2}, {5, 4}}}}

text := MarshalString(mp)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Unmarshal(text)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}

func BenchmarkUnmarshalMultiPolygon(b *testing.B) {
var mp orb.MultiPolygon
loadJSON(b, "testdata/polygon.json", &mp)

text := MarshalString(mp)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Unmarshal(text)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}

func loadJSON(tb testing.TB, filename string, obj interface{}) {
data, err := ioutil.ReadFile(filename)
if err != nil {
tb.Fatalf("failed to load mvt file: %v", err)
}

err = json.Unmarshal(data, obj)
if err != nil {
tb.Fatalf("unmarshal error: %v", err)
}
}
1 change: 1 addition & 0 deletions encoding/wkt/testdata/polygon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[[-81.60644531,41.51377887],[-81.60639143,41.51373958],[-81.60644531,41.51369176],[-81.60644531,41.51377887]]],[[[-81.60644531,41.51367126],[-81.6062813,41.51356772],[-81.60629702,41.51355379],[-81.60626037,41.51353058],[-81.60628858,41.51350556],[-81.60598468,41.51331372],[-81.60566371,41.51359859],[-81.60597318,41.51379406],[-81.6059553,41.51381001],[-81.60601091,41.51384512],[-81.60602843,41.51382951],[-81.60618132,41.51392611],[-81.60618635,41.5139216],[-81.60622929,41.51394851],[-81.60623998,41.5139405],[-81.60629505,41.51397366],[-81.60630574,41.51396357],[-81.60644531,41.51405409],[-81.60644531,41.51425983],[-81.60639305,41.5143673],[-81.60644531,41.51439781],[-81.60644531,41.51475154],[-81.60588452,41.51475938],[-81.60587113,41.51480956],[-81.60586314,41.51486378],[-81.60586844,41.51493212],[-81.60588452,41.51500436],[-81.60591668,41.5150727],[-81.60595961,41.51513492],[-81.60601872,41.51519317],[-81.60608574,41.51524342],[-81.60617422,41.51528546],[-81.6062654,41.51531768],[-81.60635658,41.51533981],[-81.60643168,41.51534781],[-81.60644531,41.51534813],[-81.60644531,41.51584208],[-81.60601827,41.51584402],[-81.60601585,41.51523467],[-81.60549563,41.51523992],[-81.60551064,41.51584617],[-81.60477168,41.51585316],[-81.60474204,41.51443308],[-81.60470063,41.51230842],[-81.6047009,41.51210689],[-81.604738,41.51208018],[-81.60480645,41.51207272],[-81.60506247,41.51219568],[-81.605367,41.5123212],[-81.60554531,41.51237737],[-81.60552124,41.51245479],[-81.60556409,41.51251197],[-81.60570629,41.51253612],[-81.60577869,41.51250202],[-81.6058431,41.51249186],[-81.60589134,41.51245681],[-81.60623064,41.51248789],[-81.60644531,41.51248933],[-81.60644531,41.51367126]],[[-81.6056876,41.5138203],[-81.60559005,41.51375969],[-81.60557478,41.51377348],[-81.6055401,41.51375189],[-81.60555699,41.51373662],[-81.60545719,41.5136746],[-81.6053793,41.51374489],[-81.6053961,41.51375532],[-81.60537849,41.51377119],[-81.60536367,41.51378458],[-81.6053493,41.51377557],[-81.60527465,41.51384317],[-81.60537149,41.5139033],[-81.60538649,41.51388978],[-81.60542242,41.51391212],[-81.60540652,41.51392638],[-81.60550345,41.51398658],[-81.60557801,41.51391918],[-81.60556202,41.51390929],[-81.60557927,41.51389368],[-81.60559661,41.51387801],[-81.6056126,41.51388797],[-81.6056876,41.5138203]],[[-81.60579783,41.51453089],[-81.6057133,41.51447506],[-81.60569533,41.5144908],[-81.60564817,41.51446006],[-81.60566793,41.51444358],[-81.6055852,41.51439279],[-81.60551145,41.51445959],[-81.60553759,41.51447358],[-81.60549402,41.51451077],[-81.60546922,41.51449517],[-81.60539341,41.51456048],[-81.60548063,41.51461618],[-81.60549528,41.5146032],[-81.60554504,41.51463131],[-81.60552618,41.51464631],[-81.60561134,41.51469898],[-81.60561538,41.51469548],[-81.60568707,41.51463131],[-81.60566901,41.51461968],[-81.60570916,41.51458456],[-81.60572668,41.51459559],[-81.60579783,41.51453089]],[[-81.60521015,41.51392402],[-81.60511349,41.51386422],[-81.60509939,41.51387626],[-81.60506462,41.51385521],[-81.6050807,41.51384209],[-81.60497937,41.51378041],[-81.60490158,41.51384707],[-81.60491972,41.51385871],[-81.60488891,41.51388581],[-81.60487355,41.5138768],[-81.60479702,41.51394662],[-81.60489287,41.51400629],[-81.60490966,41.51399378],[-81.60494452,41.51401577],[-81.60492637,41.51402936],[-81.60502429,41.5140897],[-81.60509939,41.51402142],[-81.6050834,41.51401126],[-81.60511762,41.51398321],[-81.60513164,41.51399277],[-81.60521015,41.51392402]],[[-81.6054783,41.51421017],[-81.6053864,41.51415387],[-81.60537643,41.51416449],[-81.60533484,41.51413988],[-81.60534687,41.51412931],[-81.60525569,41.51407261],[-81.6051779,41.51414189],[-81.60519937,41.51415636],[-81.60516784,41.51418252],[-81.60514439,41.51416947],[-81.60506992,41.51424131],[-81.60515984,41.51429896],[-81.60517116,41.514289],[-81.60521078,41.51431308],[-81.60519874,41.51432311],[-81.60529064,41.51438041],[-81.60537104,41.5143071],[-81.60535217,41.514296],[-81.60537769,41.51427091],[-81.60539853,41.51428201],[-81.6054783,41.51421017]],[[-81.6055534,41.51291369],[-81.60533879,41.51277909],[-81.6052503,41.51285846],[-81.60527312,41.51287447],[-81.60523692,41.51290871],[-81.60512562,41.51283936],[-81.60497937,41.51297396],[-81.60509067,41.51304432],[-81.60505178,41.51307936],[-81.60502097,41.51306329],[-81.6048775,41.51319688],[-81.60509337,41.51333047],[-81.60523961,41.51320091],[-81.6052168,41.5131808],[-81.60525704,41.51314569],[-81.60536969,41.51321302],[-81.60551055,41.51308239],[-81.6054032,41.51301506],[-81.60544075,41.512979],[-81.60546761,41.512994],[-81.6055534,41.51291369]],[[-81.60622058,41.51403689],[-81.60612473,41.51397319],[-81.60610865,41.51398819],[-81.6060711,41.51396761],[-81.6060878,41.51395308],[-81.60599402,41.51389429],[-81.60592018,41.5139586],[-81.60593293,41.51396862],[-81.60589871,41.51399727],[-81.60588604,41.5139892],[-81.60580825,41.514057],[-81.60590679,41.51411828],[-81.60592153,41.5141057],[-81.60595638,41.51412528],[-81.60593904,41.5141394],[-81.60603552,41.51420008],[-81.60611197,41.51413227],[-81.60609589,41.51412232],[-81.60613083,41.51409366],[-81.60614485,41.51410221],[-81.60622058,41.51403689]],[[-81.60540868,41.51522539],[-81.6053979,41.51474371],[-81.60496778,41.51474909],[-81.60496455,41.51459936],[-81.6047998,41.51460138],[-81.60480788,41.51496219],[-81.60521123,41.51495707],[-81.60521401,41.515084],[-81.60486062,41.51508837],[-81.60487193,41.51559662],[-81.60530133,41.51559117],[-81.60529819,41.51545416],[-81.60500345,41.51545786],[-81.60500219,41.51540351],[-81.60507145,41.5154027],[-81.60506867,41.51528061],[-81.60499258,41.51528156],[-81.6049915,41.51523057],[-81.60540868,41.51522539]],[[-81.60527995,41.51574897],[-81.60527555,41.51565258],[-81.60492709,41.51566139],[-81.6049314,41.51575778],[-81.60527995,41.51574897]],[[-81.60543671,41.51256827],[-81.6052194,41.51243172],[-81.60502905,41.51259437],[-81.60524905,41.51273496],[-81.60543671,41.51256827]],[[-81.60583708,41.51515396],[-81.60579962,41.51497456],[-81.6057053,41.51498566],[-81.60574294,41.51516506],[-81.60583708,41.51515396]],[[-81.60598117,41.51270879],[-81.60575588,41.51256827],[-81.60557621,41.51273597],[-81.60580007,41.51287447],[-81.60598117,41.51270879]],[[-81.60599061,41.51309046],[-81.6057733,41.51295189],[-81.60558421,41.51311959],[-81.60580421,41.51325722],[-81.60599061,41.51309046]],[[-81.60639018,41.51283229],[-81.60616281,41.51269157],[-81.60597309,41.51285927],[-81.60620512,41.513001],[-81.60639018,41.51283229]]]]
Loading

0 comments on commit 0054d7c

Please sign in to comment.