Skip to content

Conversation

@mlechu
Copy link
Member

@mlechu mlechu commented Nov 21, 2025

We currently use keyword arguments as arbitrary key-value pairs in a number of
places, which I've realized isn't really how they're intended to be used.
This change fixes this and reduces the JuliaLowering package image size by
about 20% on my machine from 26.910 MiB -> 21.701 MiB, or when compiled
into sysbase.dylib, +55MB -> +45MB. Unfortunately no change to
precompile time as I had hoped, but I figured this is worth doing anyway.
Lowering performance looks unchanged.

I've just used Vector{Pair} or explicit setattr calls, but let me know if
there's a better way of doing this.

Also let me know if you have a reason for the difference in size when compiled
into Base---I don't think I'm accidentally producing two copies, but you
never know.

We currently use keyword arguments as arbitrary key-value pairs in a number of
     places, which I've realized isn't really how they're intended to be used.
      This change fixes this and reduces the JuliaLowering package image size by
     about 20% on my machine from `26.910 MiB -> 21.701 MiB`, or when compiled
     into sysbase.dylib, `+55MB -> +45MB`.  Unfortunately no change to
     precompile time as I had hoped, but I figured this is worth doing anyway.

I've just used `Vector{Pair}` or explicit `setattr` calls, but let me know if
     there's a better way of doing this.

Also let me know if you have a reason for the difference in size when compiled
     into Base---I don't think I'm accidentally producing two copies, but you
     never know.
@mlechu mlechu requested review from c42f and topolarity November 21, 2025 18:40
@mlechu mlechu added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Nov 21, 2025
function set_scope_layer(ctx, ex, layer_id, force)
k = kind(ex)
scope_layer = force ? layer_id : get(ex, :scope_layer, layer_id)
attrs = [:scope_layer=>(force ? layer_id : get(ex, :scope_layer, layer_id))]
Copy link
Member

@topolarity topolarity Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attrs = [:scope_layer=>(force ? layer_id : get(ex, :scope_layer, layer_id))]
attrs = Pair{Symbol,LayerId}[
:scope_layer=>(force ? layer_id : get(ex, :scope_layer, layer_id))
]

if you're really trying to be strict about specializations, it can be a good idea to explicitly type most arrays, since:

  1. Pair over-specializes just like NamedTuple (so the strict array type will either convert this away or push you to fix the type instability), and
  2. Whether this vector ends up well-typed depends on inference / Base._return_type (esp. bad for recursive functions)

elseif k == K"."
makenode(ctx, ex, ex, set_scope_layer(ctx, ex[1], layer_id, force), ex[2],
scope_layer=scope_layer)
children = [set_scope_layer(ctx, ex[1], layer_id, force), ex[2]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
children = [set_scope_layer(ctx, ex[1], layer_id, force), ex[2]]
children = NodeId[set_scope_layer(ctx, ex[1], layer_id, force), ex[2]]

see above

elseif !is_leaf(ex)
mapchildren(e->set_scope_layer(ctx, e, layer_id, force), ctx, ex;
scope_layer=scope_layer)
mapchildren(e->set_scope_layer(ctx, e, layer_id, force), ctx, ex, attrs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this method doesn't exist, unless I misread something?

function mapchildren(f::Function, ctx, ex::SyntaxTree, mapped_children::AbstractVector{<:Integer};
extra_attrs...)
function mapchildren(f::Function, ctx, ex::SyntaxTree,
mapped_children::AbstractVector{<:Integer})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

narrowing this to Vector{NodeId} would be a good idea, if it's possible


function makenode(ctx, srcref, proto, children...; attrs...)
_makenode(ctx, srcref, proto, _node_ids(syntax_graph(ctx), children...); attrs...)
function makeleaf(ctx, srcref, proto, @nospecialize(attrs::AbstractVector))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another normalization trick you might consider trying out at some point is:

@noinline function _makeleaf(..., attrs::Vector{Any})
    # ... (complicated function)
end
@inline function makeleaf(..., @nospecialize(attrs::AbstractVector))
    return _makeleaf(..., convert(Vector{Any}, attrs))
end

This preserves "good" codegen, while trying to limit specialized code to the caller side. Ofc, it's even better if we can avoid the Vector type explosion to begin with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants