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

[Utilities] use foldl to avoid StackOverflow #2290

Merged
merged 4 commits into from
Sep 24, 2023
Merged
Changes from all commits
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
17 changes: 5 additions & 12 deletions src/Utilities/operate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,7 @@ end
### 1c: operate(+, T, args...)

function operate(::typeof(+), ::Type{T}, f, g, h, args...) where {T<:Number}
ret = operate(+, T, f, g)
ret = operate!(+, T, ret, h)
for a in args
ret = operate!(+, T, ret, a)
end
return ret
return operate!(+, T, operate(+, T, f, g), h, args...)
end

### 2a: operate(::typeof(-), ::Type{T}, ::F)
Expand Down Expand Up @@ -1173,12 +1168,10 @@ end
### 1c: operate!(+, T, args...)

function operate!(::typeof(+), ::Type{T}, f, g, h, args...) where {T<:Number}
ret = operate!(+, T, f, g)
ret = operate!(+, T, ret, h)
for a in args
ret = operate!(+, T, ret, a)
end
return ret
# In order to improve performance and avoid a `StackOverflow` if there are
# too many arguments, `foldl` does not do recursion and fall back to a
# `for` loop if there are too many arguments.
return foldl((x, y) -> operate!(+, T, x, y), (f, g, h, args...))
end

### 2a: operate!(::typeof(-), ::Type{T}, ::F)
Expand Down
Loading