Skip to content

Commit dd6c3e2

Browse files
committed
Optimize allocs
1 parent db7eaa4 commit dd6c3e2

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/coloring.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,27 @@ Encode a set of 2-colored stars resulting from the [`star_coloring`](@ref) algor
138138
139139
$TYPEDFIELDS
140140
"""
141-
struct StarSet
141+
struct StarSet{V<:AbstractVector{Int}}
142142
"a mapping from edges (pair of vertices) to their star index"
143143
star::Vector{Int}
144144
"a mapping from star indices to their hub (undefined hubs for single-edge stars are the negative value of one of the vertices, picked arbitrarily)"
145145
hub::Vector{Int}
146146
"a mapping from star indices to the vector of their spokes"
147-
spokes::Vector{Vector{Int}}
147+
spokes::Vector{V}
148148
end
149149

150150
function StarSet(
151151
g::AdjacencyGraph, star::Vector{Int}, hub::Vector{Int}, nb_spokes::Vector{Int}
152152
)
153153
(; S, edgeindex) = g
154154
# Create a list of spokes for each star, preallocating their sizes based on nb_spokes
155-
spokes = [Vector{Int}(undef, ns) for ns in nb_spokes]
155+
spokes_storage = Vector{Int}(undef, nb_edges(g))
156+
spokes = Vector{typeof(view(spokes_storage, 1:0))}(undef, length(nb_spokes))
157+
cum_nb_spokes = 0
158+
for (s, ns) in enumerate(nb_spokes)
159+
spokes[s] = view(spokes_storage, (cum_nb_spokes + 1):(cum_nb_spokes + ns))
160+
cum_nb_spokes += ns
161+
end
156162

157163
# Reuse nb_spokes as counters to track the current index while filling the spokes
158164
fill!(nb_spokes, 0)

src/order.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ function vertices(
209209
u = pop_next_candidate!(db; direction)
210210
direction == :low2high ? push!(π, u) : pushfirst!(π, u)
211211
for v in neighbors(g, u)
212+
v == u && continue
212213
already_ordered(db, v) && continue
213214
update_bucket!(db, v; degtype, direction)
214215
end

test/order.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ rng = StableRNG(63)
3535
end;
3636

3737
@testset "RandomOrder" begin
38-
A = sprand(rng, Bool, 10, 10, 0.5)
38+
A = sparse(Symmetric(sprand(rng, Bool, 10, 10, 0.5)))
3939
ag = AdjacencyGraph(A)
4040
@test sort(vertices(ag, RandomOrder(rng))) == 1:10
4141
@test sort(vertices(ag, RandomOrder())) == 1:10
@@ -63,7 +63,7 @@ end;
6363
@testset "LargestFirst" begin
6464
A = sparse([
6565
0 1 0
66-
1 0 0
66+
1 0 1
6767
0 1 0
6868
])
6969
ag = AdjacencyGraph(A)

0 commit comments

Comments
 (0)