-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Improve performance of foreach for tuples #31901
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
Conversation
Are these CI errors expected? They don't seem related to my changes... |
Perhaps rebase and force push to rerun the CI? |
d9758e6
to
1a7c72a
Compare
Hmm, that cleared a few up, but I can't seem to be able to get rid of that last failure. The build keeps timing out. |
3a07e80
to
103520b
Compare
103520b
to
430f814
Compare
430f814
to
f90ef60
Compare
f90ef60
to
270d1c4
Compare
foreach(f, xs) = foldl((_, x) -> (f(x); nothing), xs; init=nothing) It will then automatically provide specializations for |
Good idea! I was just mimicking the Isn't it possible to implement many of the higher order functions using Also, I just noticed that there's no method for |
Yes! In fact, But I suppose core devs would think adding transducers to Base is too radical change ATM. Manually implementing |
OK, I'll start small. : ) I'm hitting some snags with this approach right now because |
270d1c4
to
5428e19
Compare
OK, I just pushed my first cut at it. I started chasing the things to include in the bootstrap process, but ended up just creating a dummy I'll look at adding a And one more question. Does anyone know why the following method exists: foreach(f) = (f(); nothing) That seems really strange to me. It is especially strange that it calls |
FWIW I find |
5428e19
to
7ff3869
Compare
7ff3869
to
0892dce
Compare
0892dce
to
54b0f02
Compare
54b0f02
to
72641d3
Compare
72641d3
to
dc67c96
Compare
dc67c96
to
06a8341
Compare
I have to say I'm puzzled by the motivation for this change. |
My original motivation was to improve the performance of If you look at the Then, @tkf suggested I could just use If you dislike the |
How could more collection types could implement |
That's the whole point of my JuliaCon talk 😉. It's more natural and efficient to implement But I don't want to stall @danielmatz's PR; even supporting tuples for now is a nice improvement. |
cf9b296
to
e398b38
Compare
It seems that I've conflated what should really be two separate PRs. I've just reworked this PR to add a specialization of The separate issue that got mixed in here is whether we'd like to rework various higher-order functions on top of My only remaining question is whether folks are OK with me including the deletion of
in this PR, or if that needs further discussion and should be split out as a separate PR. |
Yes, that would be fine if |
I understand that Having said that, I think it is conceivable to (re)structure Julia code base such that
With this scheme, it is OK to overload This is partially already true. For example, |
That's entirely possible as an approach but I don't think this PR is the place to try to sneak that into one single function. Instead, what should happen is there should be a Julep-style issue to propose and discuss (can be roughly just what you wrote here), then if people agree to it, we can come up with a plan to change things around so that this is the arrangement. |
👍 for fast In the long run, it would be better to have faster (unrolled) generic iteration on tuples, but I feel like |
base/abstractarray.jl
Outdated
@@ -1916,7 +1916,6 @@ julia> foreach(x -> println(x^2), a) | |||
49 | |||
``` | |||
""" | |||
foreach(f) = (f(); nothing) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this method is necessary to (1) resolve the ambiguity between foreach(f, itrs...)
and foreach(f, itrs::Tuple...)
in the empty-iterator case and (2) be non-breaking (note that for z in zip()
doesn't currently work: #43821).
If we want to change the foreach(f)
behavior, that should be a separate PR.
e398b38
to
40bb9fc
Compare
I'm sorry that I neglected this PR for so long. Thanks for motivating me to look at it again! I've restored |
It's called left fold. |
It would be nicer if for x in sometuple
....do something...
end were unrolled and fast, but currently Julia's compiler gets confused if the tuple elements are of different types. |
We can lower a |
Yes, but in the 1.x time frame? |
I just noticed that this only improves the performance of |
For the expressibility of the protocol, I think I proved it's possible. A generalized version of
Yeah, we need specialization in |
Fixes #31869