From b92236f9676c0c0d9b0e8aded520606af30fdae0 Mon Sep 17 00:00:00 2001 From: Peter Stace Date: Fri, 6 Nov 2026 14:10:42 +1100 Subject: [PATCH 1/2] Require Go 1.19 Raise the go directive in go.mod and the golang image for the unit, pgscan, and cmppg CI jobs from 1.18 to 1.19. The geos and proj jobs already build with Go 1.21, and golangci-lint v1.61.0 in the lint job supports 1.19. --- .ci/compose-cmppg.yaml | 2 +- .ci/compose-pgscan.yaml | 2 +- .ci/compose-unit.yaml | 2 +- CHANGELOG.md | 3 +++ go.mod | 2 +- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.ci/compose-cmppg.yaml b/.ci/compose-cmppg.yaml index f9d0642b..175fb670 100644 --- a/.ci/compose-cmppg.yaml +++ b/.ci/compose-cmppg.yaml @@ -9,7 +9,7 @@ services: timeout: '1s' retries: 50 tests: - image: golang:1.18 + image: golang:1.19 working_dir: /go/src/github.com/peterstace/simplefeatures entrypoint: go test -test.count=1 -test.timeout=30m -test.run=. ./internal/cmprefimpl/cmppg volumes: diff --git a/.ci/compose-pgscan.yaml b/.ci/compose-pgscan.yaml index 211e7070..03030cdb 100644 --- a/.ci/compose-pgscan.yaml +++ b/.ci/compose-pgscan.yaml @@ -9,7 +9,7 @@ services: timeout: '1s' retries: 50 tests: - image: golang:1.18 + image: golang:1.19 working_dir: /go/src/github.com/peterstace/simplefeatures entrypoint: go test -test.count=1 -test.timeout=30m -test.run=. ./internal/pgscan volumes: diff --git a/.ci/compose-unit.yaml b/.ci/compose-unit.yaml index 801251ea..6c084ddf 100644 --- a/.ci/compose-unit.yaml +++ b/.ci/compose-unit.yaml @@ -1,6 +1,6 @@ services: tests: - image: golang:1.18 + image: golang:1.19 working_dir: /go/src/github.com/peterstace/simplefeatures entrypoint: >- go test -covermode=count -coverpkg=./... -coverprofile=coverage.out -test.count=1 -test.run=. diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bcfe79d..cf413340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- **Breaking change:** The minimum required Go version is now 1.19 (previously + 1.18). + - Fix a panic in `UnmarshalTWKB` when parsing a TWKB whose element count (point count, ring point count, or ID list length) is far larger than the remaining input. These counts are untrusted varints, so a crafted value would cause an diff --git a/go.mod b/go.mod index 061f591b..1c196de6 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/peterstace/simplefeatures -go 1.18 +go 1.19 retract v0.45.0 // Due to bug: https://github.com/peterstace/simplefeatures/pull/554 From d1b4b1bcd6f6872c35226478b5657377a3fe1cb1 Mon Sep 17 00:00:00 2001 From: Peter Stace Date: Fri, 6 Nov 2026 14:10:42 +1100 Subject: [PATCH 2/2] geom: use encoding/binary append functions in WKB and TWKB encoders Go 1.19 added AppendUint32 and AppendUint64 to binary.LittleEndian and binary.BigEndian through the binary.AppendByteOrder interface, along with binary.AppendVarint and binary.AppendUvarint. The WKB and TWKB encoders now append to the output buffer directly instead of encoding each value into a temporary array first. The WKB encoder passed its temporary array to a method on the binary.ByteOrder interface, which made the array escape to the heap. Each header field and each Point or MultiPoint coordinate cost an allocation. The TWKB bbox buffer is created with room for the largest possible header, so appending its varints one at a time never grows it. Add a multipoint case to BenchmarkMarshalWKB and a new BenchmarkMarshalTWKB. The existing polygon case writes coordinates with one bulk copy, so it doesn't exercise the per-value code. --- CHANGELOG.md | 7 +++++++ geom/perf_test.go | 33 +++++++++++++++++++++++++++++++++ geom/twkb_write.go | 31 +++++++++---------------------- geom/wkb_marshal.go | 16 ++++++---------- 4 files changed, 55 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf413340..3386c659 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ - **Breaking change:** The minimum required Go version is now 1.19 (previously 1.18). +- Improve the performance of WKB and TWKB marshalling by appending encoded + values directly to the output buffer using the `encoding/binary` append + functions added in Go 1.19. `AsBinary` and `AppendWKB` make fewer heap + allocations (previously, each coordinate of a `Point` or `MultiPoint` and each + WKB header field caused an allocation), and `MarshalTWKB` is around 20% to 30% + faster for geometries with 1,000 or more points. + - Fix a panic in `UnmarshalTWKB` when parsing a TWKB whose element count (point count, ring point count, or ID list length) is far larger than the remaining input. These counts are untrusted varints, so a crafted value would cause an diff --git a/geom/perf_test.go b/geom/perf_test.go index 4c81d8e4..2ccd1843 100644 --- a/geom/perf_test.go +++ b/geom/perf_test.go @@ -41,6 +41,39 @@ func BenchmarkMarshalWKB(b *testing.B) { }) } }) + b.Run("multipoint", func(b *testing.B) { + for _, sz := range []int{10, 100, 1000, 10000} { + b.Run(fmt.Sprintf("n=%d", sz), func(b *testing.B) { + rnd := rand.New(rand.NewSource(0)) + pts := make([]geom.Point, sz) + for i := range pts { + pts[i] = geom.XY{X: rnd.Float64(), Y: rnd.Float64()}.AsPoint() + } + mp := geom.NewMultiPoint(pts) + b.ResetTimer() + for i := 0; i < b.N; i++ { + mp.AsBinary() + } + }) + } + }) +} + +func BenchmarkMarshalTWKB(b *testing.B) { + b.Run("polygon", func(b *testing.B) { + for _, sz := range []int{10, 100, 1000, 10000} { + b.Run(fmt.Sprintf("n=%d", sz), func(b *testing.B) { + poly := regularPolygon(geom.XY{}, 1.0, sz).AsGeometry() + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := geom.MarshalTWKB(poly, 6, geom.TWKBBoundingBoxHeader(), geom.TWKBSizeHeader()) + if err != nil { + b.Fatal(err) + } + } + }) + } + }) } func BenchmarkUnmarshalWKB(b *testing.B) { diff --git a/geom/twkb_write.go b/geom/twkb_write.go index 9954d942..e27a4aea 100644 --- a/geom/twkb_write.go +++ b/geom/twkb_write.go @@ -516,7 +516,6 @@ func (w *twkbWriter) writeSinglePointArray(coords ...float64) { // Utilise and update the running memory of the previous reference point. // The input coords must contain numPoints * the number of dimensions values. func (w *twkbWriter) writePointArray(numPoints int, coords []float64) { - var buf [binary.MaxVarintLen64]byte c := 0 for i := 0; i < numPoints; i++ { for d := 0; d < w.dimensions; d++ { @@ -534,9 +533,7 @@ func (w *twkbWriter) writePointArray(numPoints int, coords []float64) { } // Perform coord differencing to find the int value. ival -= w.refpoint[d] - n := binary.PutVarint(buf[:], ival) - - w.twkbContents = append(w.twkbContents, buf[:n]...) + w.twkbContents = binary.AppendVarint(w.twkbContents, ival) w.refpoint[d] += ival c++ } @@ -560,23 +557,17 @@ func (w *twkbWriter) writeAdditionalHeaders() { func (w *twkbWriter) writeBBoxHeader() { // Store bbox min and delta for each dimension. - var buf [twkbMaxDimensions * 2 * binary.MaxVarintLen64]byte - n := 0 + w.twkbBBox = make([]byte, 0, 2*w.dimensions*binary.MaxVarintLen64) for d := 0; d < w.dimensions; d++ { - n += binary.PutVarint(buf[n:], w.bboxMin[d]) - n += binary.PutVarint(buf[n:], w.bboxMax[d]-w.bboxMin[d]) + w.twkbBBox = binary.AppendVarint(w.twkbBBox, w.bboxMin[d]) + w.twkbBBox = binary.AppendVarint(w.twkbBBox, w.bboxMax[d]-w.bboxMin[d]) } - // Insert the bbox varints into the appropriate header. - w.twkbBBox = append(w.twkbBBox, buf[:n]...) } func (w *twkbWriter) writeSizeHeader(bboxLength, contentsLength int) { - // Form size header as a varint covering all data after - var buf [binary.MaxVarintLen64]byte - n := binary.PutUvarint(buf[:], uint64(bboxLength+contentsLength)) - - // Insert the size header after any other headers. - w.twkbHeaders = append(w.twkbHeaders, buf[:n]...) + // The size header covers all data after the headers, and goes after any + // other headers. + w.twkbHeaders = binary.AppendUvarint(w.twkbHeaders, uint64(bboxLength+contentsLength)) } func (w *twkbWriter) writeIDList(num int) error { @@ -593,15 +584,11 @@ func (w *twkbWriter) writeIDList(num int) error { } func (w *twkbWriter) writeSignedVarint(val int64) { - var buf [binary.MaxVarintLen64]byte - n := binary.PutVarint(buf[:], val) - w.twkbContents = append(w.twkbContents, buf[:n]...) + w.twkbContents = binary.AppendVarint(w.twkbContents, val) } func (w *twkbWriter) writeUnsignedVarint(val uint64) { - var buf [binary.MaxVarintLen64]byte - n := binary.PutUvarint(buf[:], val) - w.twkbContents = append(w.twkbContents, buf[:n]...) + w.twkbContents = binary.AppendUvarint(w.twkbContents, val) } func (w *twkbWriter) writeHeaderByte(b byte) { diff --git a/geom/wkb_marshal.go b/geom/wkb_marshal.go index 734f57db..25530848 100644 --- a/geom/wkb_marshal.go +++ b/geom/wkb_marshal.go @@ -7,7 +7,9 @@ import ( "unsafe" ) -var nativeOrder binary.ByteOrder +// nativeOrder is the byte order of the host. It's safe to share because it's +// only assigned in init. +var nativeOrder binary.AppendByteOrder func init() { var buf [2]byte @@ -41,21 +43,15 @@ func (m *wkbMarshaler) writeByteOrder() { func (m *wkbMarshaler) writeGeomType(geomType GeometryType, ctype CoordinatesType) { gt := [...]uint32{7, 1, 2, 3, 4, 5, 6}[geomType] - var buf [4]byte - nativeOrder.PutUint32(buf[:], uint32(ctype)*1000+gt) - m.buf = append(m.buf, buf[:]...) + m.buf = nativeOrder.AppendUint32(m.buf, uint32(ctype)*1000+gt) } func (m *wkbMarshaler) writeFloat64(f float64) { - var buf [8]byte - nativeOrder.PutUint64(buf[:], math.Float64bits(f)) - m.buf = append(m.buf, buf[:]...) + m.buf = nativeOrder.AppendUint64(m.buf, math.Float64bits(f)) } func (m *wkbMarshaler) writeCount(n int) { - var buf [4]byte - nativeOrder.PutUint32(buf[:], uint32(n)) - m.buf = append(m.buf, buf[:]...) + m.buf = nativeOrder.AppendUint32(m.buf, uint32(n)) } func (m *wkbMarshaler) writeCoordinates(c Coordinates) {