Skip to content

Slow lenses for nested named tuples #151

Closed
@cscherrer

Description

@cscherrer

Here's an interesting benchmark:

x = (a=1,b=(c=2,d=(e=3,f=(g=4,h=(i=5,j=(k=6,l=(m=7,n=(o=8,p=(q=9,r=(s=10,t=(u=11,v=(w=12,x=(y=13,z=14)))))))))))))

using BenchmarkTools
using Setfield

ℓ1  = @lens _.a
ℓ2  = @lens _.b.c
ℓ3  = @lens _.b.d.e
ℓ4  = @lens _.b.d.f.g
ℓ5  = @lens _.b.d.f.h.i
ℓ6  = @lens _.b.d.f.h.j.k
ℓ7  = @lens _.b.d.f.h.j.l.m
ℓ8  = @lens _.b.d.f.h.j.l.n.o
ℓ9  = @lens _.b.d.f.h.j.l.n.p.q
ℓ10 = @lens _.b.d.f.h.j.l.n.p.r.s
ℓ11 = @lens _.b.d.f.h.j.l.n.p.r.t.u
ℓ12 = @lens _.b.d.f.h.j.l.n.p.r.t.v.w
ℓ13 = @lens _.b.d.f.h.j.l.n.p.r.t.v.x.y
ℓ14 = @lens _.b.d.f.h.j.l.n.p.r.t.v.x.z


t = []
forin [ℓ1,ℓ2,ℓ3,ℓ4,ℓ5,ℓ6,ℓ7,ℓ8,ℓ9,ℓ10,ℓ11,ℓ12,ℓ13,ℓ14]
    [push!(t, @belapsed get($x, $ℓ))]
end

Then compare:

julia> t
14-element Array{Any,1}:
 1.0000000000000001e-11
 1.0000000000000001e-11
 1.0000000000000001e-11
 1.152e-9
 1.152e-9
 1.152e-9
 1.152e-9
 1.152e-9
 1.162e-9
 1.162e-9
 1.162e-9
 1.162e-9
 1.162e-9
 1.162e-9

julia> @belapsed $x.b.d.f.h.j.l.n.p.r.t.v.x.z
2.0000000000000002e-11

My big worry here was that time might be linear with depth. That doesn't seem to be the case, which is great! But Setfield still takes 50x the time of the "dotted path" approach. Do you think it's possible to close this gap?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions