-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: allow splatting in hvcat syntax #39249
Conversation
I wonder if it would be better just to wrap |
Do you mean during lowering? That would always be allocating though and wouldn' t work for tuples of arrays, for example. Could you clarify what you mean by the pathological tuple cases here? |
Oh right it would actually just be |
Would something like a fallback method for |
ac66ca0
to
510049b
Compare
I have no clue what is going on here: > (expand-forms '(vcat (row a (|...| b)) (row c d)))
(call (top hvcat_rows) (call (core _apply_iterate) (top iterate) (core tuple)
(call (core tuple) a) b)
(call (core tuple) c d))
> (expand-forms '(vcat (row a (|...| b)) (row c d)))
(call (top hvcat_rows) (call (core _apply_iterate) (top iterate) (core tuple)
(call (core tuple) a) b)
(call (core tuple) c d) (call (core _apply_iterate) (top iterate) (core
tuple)
(call (core tuple) a) b)
(call (core tuple) c d))
> (expand-forms '(vcat (row a (|...| b)) (row c d)))
(call (top hvcat_rows) (call (core _apply_iterate) (top iterate) (core tuple)
(call (core tuple) a) b)
(call (core tuple) c d) (call (core _apply_iterate) (top iterate) (core
tuple)
(call (core tuple) a) b)
(call (core tuple) c d) (call (core _apply_iterate) (top iterate) (core
tuple)
(call (core tuple) a) b)
(call (core tuple) c d)) It's probably just a dumb mistake I made, but I am running out of ideas. Weirdly enough, it works just fine with |
Just saw you're working on this! Cool, looking at it myself. Based on the checks, it looks like your PR is not compatible with this line from base\uuid.jl, line 84: |
Yes, that's the problem I described above. I am increasingly suspecting this might actually be a femtolisp bug. Perhaps something similar to JeffBezanson/femtolisp#43? |
Ah! I wasn't clear on what you were showing. Is it that repeated execution of the same line is causing different output? |
Exactly. This is taken from Julia's flisp repl |
Aside: What files need to be loaded to prepare the flisp REPL, and how do I do that? (That is, flisp equivalent of include/using) |
I think found a parser solution that doesn't require new functions.
It's effectively doing this:
Wondering if there's a way to get rid of the extra allocations. How does it compare to yours?
|
The problem with that is that not all iterators define |
Though you'd need iterators to have matching numbers of elements for an hvcat operation to work at all... Is there an example of where this would be an issue? |
I will admit, this is a little contrived for
|
So I don't know why it's a problem, but Julia/flisp doesn't like that (Also I don't know what this does, but I think the last line should have |
I basically adapted your solution into FLISP, looks like it is a bit more efficient. Let me know what you think.
One reason I'm interested in a solution that doesn't require extra methods is so I can adapt it to my N-dimensional syntax PR. Checked it with your
|
Hmm, I think the problem with that approach is that you are iterating the splatted iterator twice, which is a problem if the iterator is stateful. I am actually surprised this works for |
Seems to, but let's see what it's doing under the hood. Maybe some problem is getting obscured?
Lowered:
I rewrote it to explicitly do the splatting only once and the result was the same. |
Ah, I see. What happens if you try this instead though? write("foo", "foo\nbar")
itr = eachline("foo")
[itr... 1; 3 "foo" "bar"] We always want to be careful in lowering that we don't accidentally call the same function twice. |
Ah, yep, there's the issue. That's disappointing. |
For my next attempt to help, I think you can get rid of tuple_cat and shave some allocations? Does work with the stateful iterator.
|
Wait, what!?! My mind just got blown!!! 🤯 I would never have thought of nesting splatting like this, but you are right, this does work! |
It might be possible for hvcat_rows to accept all hvcats without additional overhead, but I'm not familiar with the pathological tuple cases that Jeff mentioned or how that could come into play. I've tried a couple examples and |
src/julia-syntax.scm
Outdated
;; in case there is splatting inside `hvcat`, collect each row as a | ||
;; separate tuple and pass those to `hvcat_rows` instead (ref #38844) | ||
(if (any (lambda (row) (any vararg? row)) rows) | ||
`(call ,.hvcat_rows ,.(map (lambda (x) `(tuple ,.x)) rows)) |
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.
Use ,@
everywhere instead of ,.
.
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.
Are the differences between the two explained anywhere? I always thought they were the same. I typically turn to the Racket manual for stuff like this, but they seem to only have ,@
for unquote-splicing
.
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.
They do the same thing except ,.
is mutating, so it's a bit of an archaic micro-optimization.
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.
Ah, I see. So that's probably why it caused problems when dealing with multiple splicing interpolations.
510049b
to
c13569a
Compare
This should be GTG from my side, if we like this approach. |
This changes the lowering of hvcat syntax to properly support splatting, which also simplified lowering slightly. I was a bit worried about introducing another function call here in terms of type inference/constant prop, but in my initial micro benchmarks, I don't see any regression:
master:
This PR:
fixes #38844