Skip to content

Commit 7fad682

Browse files
authored
move some object construction out of build_eq_classes1! to avoid creating them over and over in a loop (#4475)
1 parent b6e4785 commit 7fad682

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/Resolve/graphtype.jl

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,15 @@ function compute_eq_classes!(graph::Graph)
12851285

12861286
np = graph.np
12871287
sumspp = sum(graph.spp)
1288+
1289+
# Preallocate workspace matrix - make it large enough for worst case
1290+
max_rows = maximum(1 + sum(size(m, 1) for m in graph.gmsk[p0]; init = 0) for p0 in 1:np; init = 1)
1291+
max_cols = maximum(graph.spp; init = 1)
1292+
cmat_workspace = BitMatrix(undef, max_rows, max_cols)
1293+
cvecs_workspace = [BitVector(undef, max_rows) for _ in 1:max_cols]
1294+
12881295
for p0 in 1:np
1289-
build_eq_classes1!(graph, p0)
1296+
build_eq_classes1!(graph, p0, cmat_workspace, cvecs_workspace)
12901297
end
12911298

12921299
log_event_global!(graph, "computed version equivalence classes, stats (total n. of states): before = $(sumspp) after = $(sum(graph.spp))")
@@ -1296,7 +1303,7 @@ function compute_eq_classes!(graph::Graph)
12961303
return graph
12971304
end
12981305

1299-
function build_eq_classes1!(graph::Graph, p0::Int)
1306+
function build_eq_classes1!(graph::Graph, p0::Int, cmat_workspace::BitMatrix, cvecs_workspace::Vector{BitVector})
13001307
np = graph.np
13011308
spp = graph.spp
13021309
gadj = graph.gadj
@@ -1312,8 +1319,24 @@ function build_eq_classes1!(graph::Graph, p0::Int)
13121319

13131320
# concatenate all the constraints; the columns of the
13141321
# result encode the behavior of each version
1315-
cmat = vcat(BitMatrix(permutedims(gconstr[p0])), gmsk[p0]...)
1316-
cvecs = [cmat[:, v0] for v0 in 1:spp[p0]]
1322+
# cmat = vcat(BitMatrix(permutedims(gconstr[p0])), gmsk[p0]...)
1323+
ncols = spp[p0]
1324+
cmat_workspace[1, 1:ncols] = gconstr[p0]
1325+
row_idx = 2
1326+
for j1 in 1:length(gmsk[p0])
1327+
msk = gmsk[p0][j1]
1328+
nrows_msk = size(msk, 1)
1329+
cmat_workspace[row_idx:(row_idx + nrows_msk - 1), 1:ncols] = msk
1330+
row_idx += nrows_msk
1331+
end
1332+
1333+
# cvecs = [cmat[:, v0] for v0 in 1:spp[p0]]
1334+
nrows = row_idx - 1
1335+
cvecs = view(cvecs_workspace, 1:ncols)
1336+
for v0 in 1:ncols
1337+
resize!(cvecs[v0], nrows)
1338+
copy!(cvecs[v0], view(cmat_workspace, 1:nrows, v0))
1339+
end
13171340

13181341
# find unique behaviors
13191342
repr_vecs = unique(cvecs)

0 commit comments

Comments
 (0)