Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions src/syntax_graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ one or several syntax trees.

TODO: Global attributes!
"""
struct SyntaxGraph{Attrs}
struct SyntaxGraph
Copy link
Member

Choose a reason for hiding this comment

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

What I had in mind here is too not remove the Attrs type parameter, but simply set it to Dict during construction.

This is what the existing design has in mind: we can have either a fully typed version with NamedTuple, or a flexible dynamic version with Dict.

That way we get both options within the same exact code base.

Copy link
Member Author

@mlechu mlechu Apr 30, 2025

Choose a reason for hiding this comment

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

I see; pushed a change doing this. I unfortunately couldn't achieve the same performance (from the original PR message, lower(1) is the same as before, and Pkg.test() takes about 60 seconds).

Copy link
Member Author

Choose a reason for hiding this comment

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

Alternative: marking structs mutable in syntax_graph.jl, which should change some stack allocations to heap allocations. This gives me 18 seconds and 68 seconds. Updated in case you'd like to try it.

edge_ranges::Vector{UnitRange{Int}}
edges::Vector{NodeId}
attributes::Attrs
attributes::Dict{Symbol, Any}
end

SyntaxGraph() = SyntaxGraph{Dict{Symbol,Any}}(Vector{UnitRange{Int}}(),
Vector{NodeId}(), Dict{Symbol,Any}())

# "Freeze" attribute names and types, encoding them in the type of the returned
# SyntaxGraph.
function freeze_attrs(graph::SyntaxGraph)
frozen_attrs = (; pairs(graph.attributes)...)
SyntaxGraph(graph.edge_ranges, graph.edges, frozen_attrs)
end
SyntaxGraph() = SyntaxGraph(Vector{UnitRange{Int}}(),
Vector{NodeId}(), Dict{Symbol,Any}())

function _show_attrs(io, attributes::Dict)
show(io, MIME("text/plain"), attributes)
Expand Down Expand Up @@ -56,15 +49,14 @@ end
function ensure_attributes(graph::SyntaxGraph; kws...)
g = SyntaxGraph(graph.edge_ranges, graph.edges, Dict(pairs(graph.attributes)...))
ensure_attributes!(g; kws...)
freeze_attrs(g)
end

function delete_attributes(graph::SyntaxGraph, attr_names...)
attributes = Dict(pairs(graph.attributes)...)
for name in attr_names
delete!(attributes, name)
end
SyntaxGraph(graph.edge_ranges, graph.edges, (; pairs(attributes)...))
SyntaxGraph(graph.edge_ranges, graph.edges, attributes)
end

function newnode!(graph::SyntaxGraph)
Expand Down Expand Up @@ -103,14 +95,10 @@ function child(graph::SyntaxGraph, id::NodeId, i::Integer)
graph.edges[graph.edge_ranges[id][i]]
end

function getattr(graph::SyntaxGraph{<:Dict}, name::Symbol)
function getattr(graph::SyntaxGraph, name::Symbol)
getfield(graph, :attributes)[name]
end

function getattr(graph::SyntaxGraph{<:NamedTuple}, name::Symbol)
getfield(getfield(graph, :attributes), name)
end

function getattr(graph::SyntaxGraph, name::Symbol, default)
get(getfield(graph, :attributes), name, default)
end
Expand Down Expand Up @@ -408,7 +396,7 @@ const SourceAttrType = Union{SourceRef,LineNumberNode,NodeId,Tuple}
function SyntaxTree(graph::SyntaxGraph, node::SyntaxNode)
ensure_attributes!(graph, kind=Kind, syntax_flags=UInt16, source=SourceAttrType,
value=Any, name_val=String)
id = _convert_nodes(freeze_attrs(graph), node)
id = _convert_nodes(graph, node)
return SyntaxTree(graph, id)
end

Expand Down
Loading