From 389648f466840130e0bea1e9c5f7c0a221cdeca0 Mon Sep 17 00:00:00 2001 From: Maarten Pronk Date: Mon, 27 Jul 2026 22:39:22 +0200 Subject: [PATCH 1/3] Fix allocations due to sizehint. --- src/wkb.jl | 4 ++-- test/runtests.jl | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/wkb.jl b/src/wkb.jl index 2b6dcd3..599a7ea 100644 --- a/src/wkb.jl +++ b/src/wkb.jl @@ -65,7 +65,7 @@ Push WKB to `data` for a Pointlike `type` of `geom`. function getwkb!(data::Vector{UInt8}, type::GI.PointTrait, geom, first::Bool) ncoord = GI.ncoord(type, geom) if first - sizehint!(data, 21) + isempty(data) && sizehint!(data, 21) push!(data, 0x01) # endianness wkbtype = geometry_code(type) ncoord == 3 && (wkbtype |= wkbZ) @@ -110,7 +110,7 @@ a geometrycollection. """ function _getwkb!(data::Vector{UInt8}, type, geom, first::Bool, repeat::Bool) if first - sizehint!(data, 42) # smallest non-point geometry is a line with 2 points + isempty(data) && sizehint!(data, 42) # smallest non-point geometry is a line with 2 points push!(data, 0x01) # endianness wkbtype = geometry_code(type) ncoord = GI.ncoord(type, geom) diff --git a/test/runtests.jl b/test/runtests.jl index 669e393..e2bdcc5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -252,4 +252,15 @@ import LibGEOS wkbc = WKG.wkb"01010000000000000000003e400000000000002440" @test wkba == wkbb == wkbc end + + @testset "Allocation" begin + ring(n) = GI.LinearRing([(Float64(i), Float64(i)) for i in 1:n]) + mpoly(k) = GI.MultiPolygon([GI.Polygon([ring(100_000 ÷ k)]) for _ in 1:k]) # 100k vertices total + + for k in (1, 25, 50, 100) + mp = mpoly(k) + a = @allocated WKG.getwkb(mp) + @test a == 6313136 + end + end end From d8ec4817f227f324a43b8ef49f7f398070532df0 Mon Sep 17 00:00:00 2001 From: Maarten Pronk Date: Tue, 28 Jul 2026 09:08:39 +0200 Subject: [PATCH 2/3] Warmup. --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index e2bdcc5..575b9cb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -257,6 +257,7 @@ import LibGEOS ring(n) = GI.LinearRing([(Float64(i), Float64(i)) for i in 1:n]) mpoly(k) = GI.MultiPolygon([GI.Polygon([ring(100_000 ÷ k)]) for _ in 1:k]) # 100k vertices total + WKG.getwkb(mpoly(1)) # warmup for k in (1, 25, 50, 100) mp = mpoly(k) a = @allocated WKG.getwkb(mp) From ab02d31d556d7db4767913917cca27d6de896f46 Mon Sep 17 00:00:00 2001 From: Maarten Pronk Date: Tue, 28 Jul 2026 11:43:37 +0200 Subject: [PATCH 3/3] Different way of testing allocs. --- test/runtests.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 575b9cb..47e5097 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -257,11 +257,13 @@ import LibGEOS ring(n) = GI.LinearRing([(Float64(i), Float64(i)) for i in 1:n]) mpoly(k) = GI.MultiPolygon([GI.Polygon([ring(100_000 ÷ k)]) for _ in 1:k]) # 100k vertices total - WKG.getwkb(mpoly(1)) # warmup + mp = mpoly(1) + WKG.getwkb(mp) # warmup + a = @allocated WKG.getwkb(mp) for k in (1, 25, 50, 100) mp = mpoly(k) - a = @allocated WKG.getwkb(mp) - @test a == 6313136 + b = @allocated WKG.getwkb(mp) + @test a == b end end end