Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
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.
WKB header field caused an allocation), and `AsBinary` now allocates its
output buffer once, at the exact size of the WKB. `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
Expand Down
1 change: 1 addition & 0 deletions geom/marshal_unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func TestMarshalUnmarshal(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
original := test.FromWKT(t, wkt)
wkb := original.AsBinary()
test.Eq(t, cap(wkb), len(wkb))
reconstructed, err := geom.UnmarshalWKB(wkb)
test.NoErr(t, err)
reconstructedWKT := reconstructed.AsText()
Expand Down
2 changes: 2 additions & 0 deletions geom/perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func BenchmarkMarshalWKB(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)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
poly.AsBinary()
Expand All @@ -50,6 +51,7 @@ func BenchmarkMarshalWKB(b *testing.B) {
pts[i] = geom.XY{X: rnd.Float64(), Y: rnd.Float64()}.AsPoint()
}
mp := geom.NewMultiPoint(pts)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mp.AsBinary()
Expand Down
12 changes: 11 additions & 1 deletion geom/type_geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Geometry struct {
MarshalJSON() ([]byte, error)
AppendWKT(dst []byte) []byte
AppendWKB(dst []byte) []byte
bytesInWKB() int
IsEmpty() bool
Envelope() Envelope
Centroid() Point
Expand Down Expand Up @@ -288,7 +289,16 @@ func (g Geometry) AppendWKT(dst []byte) []byte {

// AsBinary returns the WKB (Well Known Text) representation of the geometry.
func (g Geometry) AsBinary() []byte {
return g.AppendWKB(nil)
return g.AppendWKB(make([]byte, 0, g.bytesInWKB()))
}

// bytesInWKB returns the number of bytes in the WKB representation of the
// geometry.
func (g Geometry) bytesInWKB() int {
if g.impl == nil {
return GeometryCollection{}.bytesInWKB()
}
return g.impl.bytesInWKB()
}

// AppendWKB appends the WKB (Well Known Text) representation of the geometry
Expand Down
12 changes: 11 additions & 1 deletion geom/type_geometry_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,17 @@ func (c *GeometryCollection) Scan(src any) error {

// AsBinary returns the WKB (Well Known Text) representation of the geometry.
func (c GeometryCollection) AsBinary() []byte {
return c.AppendWKB(nil)
return c.AppendWKB(make([]byte, 0, c.bytesInWKB()))
}

// bytesInWKB returns the number of bytes in the WKB representation of the
// geometry.
func (c GeometryCollection) bytesInWKB() int {
n := wkbHeaderBytes + wkbCountBytes
for i := 0; i < c.NumGeometries(); i++ {
n += c.GeometryN(i).bytesInWKB()
}
return n
}

// AppendWKB appends the WKB (Well Known Text) representation of the geometry
Expand Down
3 changes: 2 additions & 1 deletion geom/type_geometry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func TestZeroGeometry(t *testing.T) {
test.Eq(t, z.IsEmpty(), true)
z = geom.Geometry{}

_ = z.AsBinary() // Doesn't crash
zWKB := z.AsBinary() // Doesn't crash
test.Eq(t, cap(zWKB), len(zWKB))

_, err = z.Value()
test.NoErr(t, err)
Expand Down
8 changes: 7 additions & 1 deletion geom/type_line_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,13 @@ func (s *LineString) Scan(src any) error {

// AsBinary returns the WKB (Well Known Text) representation of the geometry.
func (s LineString) AsBinary() []byte {
return s.AppendWKB(nil)
return s.AppendWKB(make([]byte, 0, s.bytesInWKB()))
}

// bytesInWKB returns the number of bytes in the WKB representation of the
// geometry.
func (s LineString) bytesInWKB() int {
return wkbHeaderBytes + wkbSequenceBytes(s.seq)
}

// AppendWKB appends the WKB (Well Known Text) representation of the geometry
Expand Down
12 changes: 11 additions & 1 deletion geom/type_multi_line_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,17 @@ func (m *MultiLineString) Scan(src any) error {

// AsBinary returns the WKB (Well Known Text) representation of the geometry.
func (m MultiLineString) AsBinary() []byte {
return m.AppendWKB(nil)
return m.AppendWKB(make([]byte, 0, m.bytesInWKB()))
}

// bytesInWKB returns the number of bytes in the WKB representation of the
// geometry.
func (m MultiLineString) bytesInWKB() int {
n := wkbHeaderBytes + wkbCountBytes
for _, ls := range m.lines {
n += ls.bytesInWKB()
}
return n
}

// AppendWKB appends the WKB (Well Known Text) representation of the geometry
Expand Down
9 changes: 8 additions & 1 deletion geom/type_multi_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ func (m *MultiPoint) Scan(src any) error {

// AsBinary returns the WKB (Well Known Text) representation of the geometry.
func (m MultiPoint) AsBinary() []byte {
return m.AppendWKB(nil)
return m.AppendWKB(make([]byte, 0, m.bytesInWKB()))
}

// bytesInWKB returns the number of bytes in the WKB representation of the
// geometry.
func (m MultiPoint) bytesInWKB() int {
ptBytes := wkbHeaderBytes + 8*m.ctype.Dimension()
return wkbHeaderBytes + wkbCountBytes + len(m.points)*ptBytes
}

// AppendWKB appends the WKB (Well Known Text) representation of the geometry
Expand Down
12 changes: 11 additions & 1 deletion geom/type_multi_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,17 @@ func (m *MultiPolygon) Scan(src any) error {

// AsBinary returns the WKB (Well Known Text) representation of the geometry.
func (m MultiPolygon) AsBinary() []byte {
return m.AppendWKB(nil)
return m.AppendWKB(make([]byte, 0, m.bytesInWKB()))
}

// bytesInWKB returns the number of bytes in the WKB representation of the
// geometry.
func (m MultiPolygon) bytesInWKB() int {
n := wkbHeaderBytes + wkbCountBytes
for i := 0; i < m.NumPolygons(); i++ {
n += m.PolygonN(i).bytesInWKB()
}
return n
}

// AppendWKB appends the WKB (Well Known Text) representation of the geometry
Expand Down
8 changes: 7 additions & 1 deletion geom/type_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,13 @@ func (p *Point) Scan(src any) error {

// AsBinary returns the WKB (Well Known Text) representation of the geometry.
func (p Point) AsBinary() []byte {
return p.AppendWKB(nil)
return p.AppendWKB(make([]byte, 0, p.bytesInWKB()))
}

// bytesInWKB returns the number of bytes in the WKB representation of the
// geometry.
func (p Point) bytesInWKB() int {
return wkbHeaderBytes + 8*p.coords.Type.Dimension()
}

// AppendWKB appends the WKB (Well Known Text) representation of the geometry
Expand Down
12 changes: 11 additions & 1 deletion geom/type_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,17 @@ func (p *Polygon) Scan(src any) error {

// AsBinary returns the WKB (Well Known Text) representation of the geometry.
func (p Polygon) AsBinary() []byte {
return p.AppendWKB(nil)
return p.AppendWKB(make([]byte, 0, p.bytesInWKB()))
}

// bytesInWKB returns the number of bytes in the WKB representation of the
// geometry.
func (p Polygon) bytesInWKB() int {
n := wkbHeaderBytes + wkbCountBytes
for _, ring := range p.rings {
n += wkbSequenceBytes(ring.seq)
}
return n
}

// AppendWKB appends the WKB (Well Known Text) representation of the geometry
Expand Down
12 changes: 12 additions & 0 deletions geom/wkb_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func init() {
}
}

// wkbHeaderBytes is the size of the byte order and geometry type fields that
// prefix every WKB geometry.
const wkbHeaderBytes = 1 + 4

// wkbCountBytes is the size of a uint32 count field.
const wkbCountBytes = 4

// wkbSequenceBytes is the number of bytes written by writeSequence for seq.
func wkbSequenceBytes(seq Sequence) int {
return wkbCountBytes + 8*len(seq.floats)
}

type wkbMarshaler struct {
buf []byte
}
Expand Down
1 change: 1 addition & 0 deletions geom/wkb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ func TestWKBMarshalValid(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
g := test.FromWKT(t, wkt)
buf := g.AsBinary()
test.Eq(t, cap(buf), len(buf))
readBackGeom, err := geom.UnmarshalWKB(buf)
test.NoErr(t, err)
test.ExactEquals(t, readBackGeom, g)
Expand Down
Loading