You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently Base.@locals returns a Dict, but this is unfortunate because it's slow, allocating, and needs to box all of its contents. If Base.@locals carried no runtime cost to use, then it could be used to solve #34168.
It'd be really great if @locals (or if we don't want to change it a new macro @staticlocals) could be a NamedTuple which we should be able to construct in a type stable manner just from syntax alone.
To be concrete, if I write
functionf(x)
local y
Base.@localsend
this lowers to
CodeInfo(
1 ─ Core.NewvarNode(:(y))
│ %2= Core.apply_type(Base.Dict, Core.Symbol, Core.Any)
│ %3= (%2)()
│ %4=$(Expr(:isdefined, :(y)))
└── goto #3 if not %42 ─ Base.setindex!(%3, y, :y)
3 ┄ %7=$(Expr(:isdefined, :(x)))
└── goto #5 if not %74 ─ Base.setindex!(%3, x, :x)
5 ┄ return%3
)
But I think that we could emit code that looks more like this:
function Base.setindex(nt::NamedTuple{names}, x, ::Val{n}) where {names, n}
NamedTuple{(names..., n)}((values(nt)..., x))
endfunctionf(x)
local y
locals =NamedTuple()
if@isdefined x
locals = Base.setindex(locals, x, Val{:x}())
endif@isdefined y
locals = Base.setindex(locals, y, Val{:y}())
end
locals
end
Currently
Base.@locals
returns aDict
, but this is unfortunate because it's slow, allocating, and needs to box all of its contents. IfBase.@locals
carried no runtime cost to use, then it could be used to solve #34168.It'd be really great if
@locals
(or if we don't want to change it a new macro@staticlocals
) could be aNamedTuple
which we should be able to construct in a type stable manner just from syntax alone.To be concrete, if I write
this lowers to
But I think that we could emit code that looks more like this:
which the optimizer has no problem with:
In code with very very many local variables, this appears to slow things down, but does still get resolved in a type stable manner.
E.g.
The text was updated successfully, but these errors were encountered: