Skip to content

Commit 18bd84c

Browse files
committed
changes from code review
1 parent 5a3c0c1 commit 18bd84c

2 files changed

Lines changed: 21 additions & 26 deletions

File tree

‎src/auxiliary/caches.jl‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ function _cached_error(msg, ex)
6868
throw(ArgumentError("@cached: $msg, got `$ex`"))
6969
end
7070

71+
# the arguments form the cache key, so they must be positional; unnamed ones get a name
72+
function _cached_canonicalize_arg(arg)
73+
Meta.isexpr(arg, :parameters) && _cached_error("keyword arguments are not supported", arg)
74+
Meta.isexpr(arg, :kw) && _cached_error("default values are not supported", arg)
75+
Meta.isexpr(arg, :...) && _cached_error("varargs are not supported", arg)
76+
arg isa Symbol && return arg
77+
Meta.isexpr(arg, :(::)) || _cached_error("unsupported argument", arg)
78+
length(arg.args) == 1 && return Expr(:(::), gensym(:arg), arg.args[1])
79+
return arg
80+
end
81+
7182
function _cached(mod::Module, ex)
7283
if !Meta.isexpr(ex, :function)
7384
Meta.isexpr(ex, :(=)) && Meta.isexpr(ex.args[1], (:call, :where, :(::))) &&
@@ -95,25 +106,12 @@ function _cached(mod::Module, ex)
95106
basename = Meta.isexpr(fname, :.) ? fname.args[end].value : fname
96107
basename isa Symbol ||
97108
_cached_error("expected a function name or a qualified name such as `Module.f`", fname)
98-
# the arguments form the cache key, so they must all be named positional arguments
99-
for arg in fcall.args[2:end]
100-
Meta.isexpr(arg, :parameters) && _cached_error("keyword arguments are not supported", fcall)
101-
Meta.isexpr(arg, :kw) && _cached_error("default values are not supported", arg)
102-
Meta.isexpr(arg, :...) && _cached_error("varargs are not supported", arg)
103-
(arg isa Symbol || (Meta.isexpr(arg, :(::)) && length(arg.args) == 2)) ||
104-
_cached_error("all arguments must be named, since they form the cache key", arg)
105-
end
109+
fargs = map(_cached_canonicalize_arg, fcall.args[2:end])
110+
fcall = Expr(:call, fname, fargs...)
111+
fargnames = map(arg -> arg isa Symbol ? arg : arg.args[1], fargs)
106112
# timer labels for the cache lookup and the miss-path construction
107113
lookuplabel = string("bookkeeping: cache ", basename)
108114
misslabel = string(_cached_category(basename), ": compute ", basename)
109-
fargs = fcall.args[2:end]
110-
fargnames = map(fargs) do arg
111-
if Meta.isexpr(arg, :(::))
112-
return arg.args[1]
113-
else
114-
return arg
115-
end
116-
end
117115
_fbody = ex.args[2]
118116

119117
# actual implenetation, with underscore name
@@ -131,7 +129,7 @@ function _cached(mod::Module, ex)
131129
end
132130
cachestylevar = gensym(:cachestyle)
133131
cachestyleex = Expr(
134-
:(=), cachestylevar, Expr(:call, GlobalRef(@__MODULE__, :CacheStyle), fname, fargnames...)
132+
:(=), cachestylevar, Expr(:call, CacheStyle, fname, fargnames...)
135133
)
136134
newfbody = Expr(
137135
:block, cachestyleex, Expr(:call, fname, fargnames..., cachestylevar)

‎src/tensors/treetransformers.jl‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function GenericTreeTransformer(::Type{A}, transform, p, Vdst, Vsrc, conjsrc::Bo
112112
@timeit_debug GLOBAL_TIMER "symmetry: recoupling matrices" begin
113113
taskforeach(1:nblocks, nthreads) do i
114114
fs_src = fblocks[i]
115-
fs_dst, U₀ = transform(fs_src)
115+
fs_dst, U = transform(fs_src)
116116
@timeit_debug GLOBAL_TIMER "bookkeeping: subblock positions" begin
117117
# the token into the fusion tree `Indices` is the subblock position
118118
inds_src = map(fusiontrees(fs_src)) do (f₁, f₂)
@@ -124,14 +124,13 @@ function GenericTreeTransformer(::Type{A}, transform, p, Vdst, Vsrc, conjsrc::Bo
124124
return idst
125125
end
126126
end
127-
U = convert(Matrix{T}, U₀)
128-
data[i] = (U, inds_dst, inds_src)
127+
data[i] = (convert(Matrix{T}, U), inds_dst, inds_src)
129128
# cost model: L input blocks each going to L output blocks of a given length
130-
weights[i] = length(U₀) * prod(structure_dst[first(inds_dst)][1])
129+
weights[i] = length(U) * prod(structure_dst[first(inds_dst)][1])
131130

132131
@debug(
133132
lazy"Created recoupling block for uncoupled: $(fs_src.uncoupled)",
134-
sz = size(U₀), sparsity = count(!iszero, U₀) / length(U₀)
133+
sz = size(U), sparsity = count(!iszero, U) / length(U)
135134
)
136135
end
137136
end
@@ -163,17 +162,15 @@ buffersize(::UniqueTreeTransformer) = 0
163162
function buffersize(transformer::GenericTreeTransformer)
164163
structure_src = transformer.structure_src
165164
return maximum(transformer.data; init = 0) do (U, _, inds_src)
166-
length(U) == 1 && return 0
167-
return prod(structure_src[first(inds_src)][1]) * sum(size(U))
165+
return length(U) == 1 ? 0 : prod(structure_src[first(inds_src)][1]) * sum(size(U))
168166
end
169167
end
170168

171169
function treetransformertype(::Type{A}, Vdst, Vsrc) where {A}
172170
I = sectortype(Vdst)
173171
T = recoupling_scalartype(A, sectorscalartype(I))
174172
N = numind(Vdst)
175-
FusionStyle(I) == UniqueFusion() && return UniqueTreeTransformer{T, N}
176-
return GenericTreeTransformer{T, N}
173+
return FusionStyle(I) == UniqueFusion() ? UniqueTreeTransformer{T, N} : GenericTreeTransformer{T, N}
177174
end
178175

179176
function TreeTransformer(

0 commit comments

Comments
 (0)