Skip to content

Commit 47fa3f4

Browse files
Rearrange some ops to reduce allocs and increase memory locality
1 parent 66d1ebf commit 47fa3f4

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/topologies/halfedge.jl

+15-10
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,23 @@ struct HalfEdgeTopology <: Topology
9898
end
9999

100100
function HalfEdgeTopology(halves::AbstractVector{Tuple{HalfEdge,HalfEdge}})
101-
# make sure that first half-edge is in the interior
102-
ordered = [isnothing(h₁.elem) ? (h₂, h₁) : (h₁, h₂) for (h₁, h₂) in halves]
101+
halfedges = Vector{HalfEdge}(undef, 2 * length(halves))
102+
edge4pair = Dict{Tuple{Int,Int},Int}()
103+
sizehint!(edge4pair, length(halves))
103104

104105
# flatten pairs of half-edges into a vector
105-
halfedges = [half for pair in ordered for half in pair]
106+
for (i, (h₁, h₂)) in enumerate(halves)
107+
# make sure that first half-edge is in the interior
108+
(h₁, h₂) = isnothing(h₁.elem) ? (h₂, h₁) : (h₁, h₂)
109+
110+
j = (i - 1) * 2 + 1
111+
halfedges[j] = h₁
112+
halfedges[j + 1] = h₂
113+
114+
# map pair of vertices to an edge (i.e. two halves)
115+
u, v = h₁.head, h₂.head
116+
edge4pair[minmax(u, v)] = i
117+
end
106118

107119
# map element and vertex to a half-edge
108120
half4elem = Dict{Int,Int}()
@@ -118,13 +130,6 @@ function HalfEdgeTopology(halves::AbstractVector{Tuple{HalfEdge,HalfEdge}})
118130
end
119131
end
120132

121-
# map pair of vertices to an edge (i.e. two halves)
122-
edge4pair = Dict{Tuple{Int,Int},Int}()
123-
for (i, (h₁, h₂)) in enumerate(ordered)
124-
u, v = h₁.head, h₂.head
125-
edge4pair[minmax(u, v)] = i
126-
end
127-
128133
HalfEdgeTopology(halfedges, half4elem, half4vert, edge4pair)
129134
end
130135

0 commit comments

Comments
 (0)