Skip to content

Commit aae6602

Browse files
committed
reduce allocations with sizehints
1 parent f27547e commit aae6602

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/fusiontrees/fusiontreeblocks.jl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ struct FusionTreeBlock{I,N₁,N₂,F<:FusionTreePair{I,N₁,N₂}}
33
end
44

55
function FusionTreeBlock{I}(uncoupled::Tuple{NTuple{N₁,I},NTuple{N₂,I}},
6-
isdual::Tuple{NTuple{N₁,Bool},NTuple{N₂,Bool}}) where {I<:Sector,
7-
N₁,N₂}
8-
F = fusiontreetype(I, N₁)
9-
F₂ = fusiontreetype(I, N₂)
10-
trees = Vector{Tuple{F₁,F₂}}(undef, 0)
6+
isdual::Tuple{NTuple{N₁,Bool},NTuple{N₂,Bool}};
7+
sizehint::Int=0) where {I<:Sector,N₁,N₂}
8+
F = fusiontreetype(I, N₁, N₂)
9+
trees = Vector{F}(undef, 0)
10+
sizehint > 0 && sizehint!(trees, sizehint)
1111

1212
if N₁ == N₂ == 0
1313
return FusionTreeBlock(trees)
@@ -97,7 +97,7 @@ function bendright(src::FusionTreeBlock)
9797
N₂ = numin(src)
9898
@assert N₁ > 0
9999

100-
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst)
100+
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst; sizehint=length(src))
101101
indexmap = treeindex_map(dst)
102102
U = zeros(sectorscalartype(I), length(dst), length(src))
103103

@@ -156,7 +156,7 @@ function bendleft(src::FusionTreeBlock)
156156
N₂ = numout(src)
157157
@assert N₁ > 0
158158

159-
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst)
159+
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst; sizehint=length(src))
160160
indexmap = treeindex_map(dst)
161161
U = zeros(sectorscalartype(I), length(dst), length(src))
162162

@@ -211,9 +211,8 @@ function foldright(src::FusionTreeBlock)
211211
N₁ = numout(src)
212212
N₂ = numin(src)
213213
@assert N₁ > 0
214-
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst)
215214

216-
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst)
215+
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst; sizehint=length(src))
217216
indexmap = treeindex_map(dst)
218217
U = zeros(sectorscalartype(I), length(dst), length(src))
219218

@@ -282,7 +281,7 @@ function foldleft(src::FusionTreeBlock)
282281
N₂ = numout(src)
283282
@assert N₁ > 0
284283

285-
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst)
284+
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst; sizehint=length(src))
286285
indexmap = treeindex_map(dst)
287286
U = zeros(sectorscalartype(I), length(dst), length(src))
288287

@@ -464,7 +463,7 @@ function artin_braid(src::FusionTreeBlock{I,N,0}, i; inv::Bool=false) where {I,N
464463
isdual = src.isdual[1]
465464
isdual′ = TupleTools.setindex(isdual, isdual[i], i + 1)
466465
isdual′ = TupleTools.setindex(isdual′, isdual[i + 1], i)
467-
dst = FusionTreeBlock{I}((uncoupled′, ()), (isdual′, ()))
466+
dst = FusionTreeBlock{I}((uncoupled′, ()), (isdual′, ()); sizehint=length(src))
468467

469468
oneT = one(sectorscalartype(I))
470469

@@ -596,7 +595,7 @@ function braid(src::FusionTreeBlock{I,N,0}, p::NTuple{N,Int},
596595
if FusionStyle(I) isa UniqueFusion && BraidingStyle(I) isa SymmetricBraiding
597596
uncoupled′ = TupleTools._permute(src.uncoupled[1], p)
598597
isdual′ = TupleTools._permute(src.isdual[1], p)
599-
dst = FusionTreeBlock{I}(uncoupled′, isdual′)
598+
dst = FusionTreeBlock{I}(uncoupled′, isdual′; sizehint=length(src))
600599
U = transformation_matrix(dst, src) do (f₁, f₂)
601600
return ((f₁′, f₂) => c for (f₁′, c) in braid(f₁, p, levels))
602601
end

0 commit comments

Comments
 (0)