Skip to content

applyreduce has 1 allocation where sum does not #244

Closed
@asinghvi17

Description

@asinghvi17
using Chairmarks
import GeometryOps as GO, GeoInterface as GI


function _area(p::GI.LinearRing)
    first_point = p.geom[1]
    area = 0.0
    for next_point in view(p.geom, 2:length(p.geom))
        area += first_point[1] * next_point[2] - first_point[2] * next_point[1]
        first_point = next_point
    end
    return area
end

_area(p::GI.Polygon) = if length(p.geom) == 1
    _area(p.geom[1])
else
    _area(p.geom[1]) - sum(_area, view(p.geom, 2:length(p.geom)))
end


p = GI.Polygon([GI.LinearRing([(0, 0), (0,1), (1, 1), (1, 0), (0, 0)])])

@be GO.area($p) # ~6 ns
@be _area($p)   # ~7 ns
# These two are at least equivalent - showing that `apply` and `applyreduce` are indeed cost-free.

@be GO.area($([p, p])) # ~17 ns, 1 allocation
@be sum($_area, $([p, p])) # ~15 ns, no allocations
# So there is work to do here.

We need to isolate and remove this allocation!

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions