Skip to content

Commit 9a2ebbf

Browse files
lkdvosclaude
andcommitted
Fill a reused buffer with the splitting tree degeneracies in degeneracystructure
Collecting them with `map` made the subblock loop 10-15% slower than on main, independently of how the strided structures are stored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dfe026d commit 9a2ebbf

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

‎src/spaces/structure.jl‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,32 +225,37 @@ See also [`sectorstructure`](@ref), [`blockstructure`](@ref), [`subblockstructur
225225

226226
ss = sectorstructure(W)
227227
ts = ss.fusiontrees
228-
structurevalues = Vector{StridedStructure{N}}(undef, length(ts))
228+
structurevalues = sizehint!(Vector{StridedStructure{N}}(), length(ts))
229229
blockvalues = Vector{Tuple{Tuple{Int, Int}, UnitRange{Int}}}(undef, length(ss.blocksectors))
230230

231-
# the subblocks of every coupled sector, from the degeneracies of the trees of both sides
231+
# the subblocks of every coupled sector, from the degeneracies of the trees of both sides;
232+
# filling a reused buffer is noticeably faster than `map` for the subblock loop below
233+
splittingstructure = Vector{NTuple{numout(W), Int}}()
232234
blockoffset = 0
233-
tree_index = 0
234235
for (block_index, (r₁, r₂)) in enumerate(zip(ts.ranges₁, ts.ranges₂))
235-
d₁ss = map(i -> dims(codom, gettokenvalue(ts.trees₁, i).uncoupled), r₁)
236-
d₂ss = map(i -> dims(dom, gettokenvalue(ts.trees₂, i).uncoupled), r₂)
237-
blockdim₁ = sum(prod, d₁ss; init = 0)
238-
blockdim₂ = sum(prod, d₂ss; init = 0)
236+
empty!(splittingstructure)
237+
blockdim₁ = 0
238+
for i in r₁
239+
d₁s = dims(codom, gettokenvalue(ts.trees₁, i).uncoupled)
240+
blockdim₁ += prod(d₁s)
241+
push!(splittingstructure, d₁s)
242+
end
239243
strides = (1, blockdim₁)
240-
offset₂ = 0
241-
for d₂s in d₂ss
244+
blockdim₂ = 0
245+
for i in r₂
246+
d₂s = dims(dom, gettokenvalue(ts.trees₂, i).uncoupled)
242247
d₂ = prod(d₂s)
243248
offset₁ = 0
244-
for d₁s in d₁ss
249+
for d₁s in splittingstructure
245250
d₁ = prod(d₁s)
246-
totaloffset = blockoffset + offset₂ * blockdim₁ + offset₁
251+
totaloffset = blockoffset + blockdim₂ * blockdim₁ + offset₁
247252
subsz = (d₁s..., d₂s...)
248253
@assert !any(==(0), subsz)
249254
substr = _subblock_strides(subsz, (d₁, d₂), strides)
250-
structurevalues[tree_index += 1] = (subsz, substr, totaloffset)
255+
push!(structurevalues, (subsz, substr, totaloffset))
251256
offset₁ += d₁
252257
end
253-
offset₂ += d₂
258+
blockdim₂ += d₂
254259
end
255260
blockrange = (blockoffset + 1):(blockoffset + blockdim₁ * blockdim₂)
256261
blockvalues[block_index] = ((blockdim₁, blockdim₂), blockrange)

0 commit comments

Comments
 (0)