Allocations when splatting Triangle points

I’m (finally) adding some distance functions to GeometryBasics.jl and I’m having a problem avoiding allocations when splatting to get the points of a triangle

using GeometryBasics
using BenchmarkTools
vect_to_center(p,a,b,c) = p-(a+b+c)/3
> @btime vect_to_center(p,a,b,c) setup=(p = Point3f0(rand(3)); a = Point3f0(rand(3)); b = Point3f0(rand(3)); c= Point3f0(rand(3)));
  2.700 ns (0 allocations: 0 bytes)
> @btime vect_to_center(p,t...) setup=(p = Point3f0(rand(3)); t = Triangle(Point3f0(rand(3)),Point3f0(rand(3)),Point3f0(rand(3))));
  298.459 ns (13 allocations: 384 bytes)

But if I grab them “manually” it works

> @btime vect_to_center(p,t.points.data...) setup=(p = Point3f0(rand(3)); t = Triangle(Point3f0(rand(3)),Point3f0(rand(3)),Point3f0(rand(3))));
  2.499 ns (0 allocations: 0 bytes)

What is the splat doing in my first example that my manual version is avoiding? Is there a cleaner approach?

It would be helpful if you posted enough code so that the timings can be reproduced with a copy paste.

I’ve added the GeometryBasics dependency in the example.

On Julia 1.6 I get the same timings.

1 Like