From 621dc97ff77b5e52b9717d836aa29f10d5e490e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 23 Sep 2023 09:13:07 +0200 Subject: [PATCH 1/4] Use afoldl to avoid StackOverflow --- src/Utilities/operate.jl | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/Utilities/operate.jl b/src/Utilities/operate.jl index 9656a1875b..3ad7b92617 100644 --- a/src/Utilities/operate.jl +++ b/src/Utilities/operate.jl @@ -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) @@ -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 + return Base.afoldl( + (f, g) -> operate!(+, T, f, g), + args..., + ) end ### 2a: operate!(::typeof(-), ::Type{T}, ::F) From a8dc447628318ee59639636f7bc2d48cee586846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 23 Sep 2023 09:15:03 +0200 Subject: [PATCH 2/4] Add comment --- src/Utilities/operate.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Utilities/operate.jl b/src/Utilities/operate.jl index 3ad7b92617..465a292845 100644 --- a/src/Utilities/operate.jl +++ b/src/Utilities/operate.jl @@ -1168,6 +1168,9 @@ end ### 1c: operate!(+, T, args...) function operate!(::typeof(+), ::Type{T}, f, g, h, args...) where {T<:Number} + # In order to improve performance and avoid a `StackOverflow` if there are + # too many arguments, `afoldl` does not do recursion and fall back to a + # `for` loop if there are too many arguments. return Base.afoldl( (f, g) -> operate!(+, T, f, g), args..., From c5350a7300ec9d148e5403f79ffa466d89b7f292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 23 Sep 2023 09:42:52 +0200 Subject: [PATCH 3/4] Fix closure perf --- src/Utilities/operate.jl | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Utilities/operate.jl b/src/Utilities/operate.jl index 465a292845..e7f7664f2d 100644 --- a/src/Utilities/operate.jl +++ b/src/Utilities/operate.jl @@ -1171,12 +1171,19 @@ function operate!(::typeof(+), ::Type{T}, f, g, h, args...) where {T<:Number} # In order to improve performance and avoid a `StackOverflow` if there are # too many arguments, `afoldl` does not do recursion and fall back to a # `for` loop if there are too many arguments. - return Base.afoldl( - (f, g) -> operate!(+, T, f, g), - args..., - ) + let T = T + add!(f, g) = operate!(+, T, f, g) + return Base.afoldl( + (f, g) -> add!, + f, + g, + h, + args..., + ) + end end + ### 2a: operate!(::typeof(-), ::Type{T}, ::F) function operate!( From 8b1d047e2fb435c517772634bbbfb8480f21f5fa Mon Sep 17 00:00:00 2001 From: odow Date: Sun, 24 Sep 2023 14:36:30 +1300 Subject: [PATCH 4/4] Switch to public foldl function --- src/Utilities/operate.jl | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/Utilities/operate.jl b/src/Utilities/operate.jl index e7f7664f2d..209289c770 100644 --- a/src/Utilities/operate.jl +++ b/src/Utilities/operate.jl @@ -1169,21 +1169,11 @@ end function operate!(::typeof(+), ::Type{T}, f, g, h, args...) where {T<:Number} # In order to improve performance and avoid a `StackOverflow` if there are - # too many arguments, `afoldl` does not do recursion and fall back to a + # too many arguments, `foldl` does not do recursion and fall back to a # `for` loop if there are too many arguments. - let T = T - add!(f, g) = operate!(+, T, f, g) - return Base.afoldl( - (f, g) -> add!, - f, - g, - h, - args..., - ) - end + return foldl((x, y) -> operate!(+, T, x, y), (f, g, h, args...)) end - ### 2a: operate!(::typeof(-), ::Type{T}, ::F) function operate!(