Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: Specialize fn in rmap and rmaptype #80

Merged
merged 4 commits into from
Aug 3, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/RecursiveApply/RecursiveApply.jl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,29 @@ export ⊞, ⊠, ⊟

Recursively apply `fn` to each element of `X`
"""
rmap(fn, X) = fn(X)
rmap(fn, X, Y) = fn(X, Y)
rmap(fn, X::Tuple) = map(x -> rmap(fn, x), X)
rmap(fn, X::Tuple, Y::Tuple) = map((x, y) -> rmap(fn, x, y), X, Y)
rmap(fn, X::NamedTuple) = map(x -> rmap(fn, x), X)
rmap(fn, X::NamedTuple{names}, Y::NamedTuple{names}) where {names} =
map((x, y) -> rmap(fn, x, y), X, Y)
rmap(fn::F, X) where {F} = fn(X)
rmap(fn::F, X, Y) where {F} = fn(X, Y)
rmap(fn::F, X::Tuple) where {F} = map(x -> rmap(fn, x), X)
rmap(fn::F, X::Tuple, Y::Tuple) where {F} = map((x, y) -> rmap(fn, x, y), X, Y)
rmap(fn::F, X::NamedTuple{names}) where {F, names} =
NamedTuple{names}(rmap(fn, Tuple(X)))
rmap(fn::F, X::NamedTuple{names}, Y::NamedTuple{names}) where {F, names} =
NamedTuple{names}(rmap(fn, Tuple(X), Tuple(Y)))

"""
rmaptype(fn, T)

The return type of `rmap(fn, X::T)`.
"""
rmaptype(fn, ::Type{T}) where {T} = fn(T)
rmaptype(fn, ::Type{T}) where {T <: Tuple} =
rmaptype(fn::F, ::Type{T}) where {F, T} = fn(T)
rmaptype(fn::F, ::Type{T}) where {F, T <: Tuple} =
Tuple{map(fn, tuple(T.parameters...))...}
rmaptype(fn, ::Type{T}) where {T <: NamedTuple{names, tup}} where {names, tup} =
rmaptype(
fn::F,
::Type{T},
) where {F, T <: NamedTuple{names, tup}} where {names, tup} =
NamedTuple{names, rmaptype(fn, tup)}


"""
rmul(w, X)
w ⊠ X
Expand Down Expand Up @@ -66,7 +69,6 @@ const ⊟ = rsub

rdiv(X, w::Number) = rmap(x -> x / w, X)


"""
rmuladd(w, X, Y)

Expand All @@ -76,7 +78,6 @@ rmuladd(w::Number, X, Y) = rmap((x, y) -> muladd(w, x, y), X, Y)
rmuladd(X, w::Number, Y) = rmap((x, y) -> muladd(x, w, y), X, Y)
rmuladd(w::Number, x::Number, y::Number) = muladd(w, x, y)


"""
rmatmul1(W, S, i, j)

Expand Down