Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
get/set queries #23
base: master
Are you sure you want to change the base?
get/set queries #23
Changes from all commits
624cd37
44bd20f
9ad1886
4aaacf5
0fd6a95
225d2ba
e331a11
6e9efd6
a55a709
36a7670
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Thanks a lot for pushing this further. I also played around with implementing this, see this branch:
https://github.com/JuliaObjects/Accessors.jl/tree/modify_stateful
Like with your implementation I encountered inference issues. I do not fully understand them, but it seems the compiler dislikes this switch. To explore what's going on I replaced
elseif (q::Q).descent_condition(o)
intoelseif true
but the thing would still not infer. OTOH manually deleting theelseif
branch did help the compiler to infer.Note I did this experimentation with my branch, which is somewhat different then yours, but I guess has the same underlying issues. Worth checking if things are better with the new ConstructionBase version and nightly julia.
If not I think it would be good to produce an MWE and report it upstream. I don't know if it is considered a bug, but maybe we'll get an explanation of why it is this way and how to work around it.
A quick and dirty workaround would be to introduce a more narrow query where you can only select and descent based on type. Then the
if elseif ...
could be replaced by three methods.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.
Ok interesting we both hit the same problem.
I think the differences are mostly just my code to handle context and optional construction in setall. But those aren't whats causing the instability...
It seems like the problem is calling
modify_stateful
recursively? It still seems broken even without theelseif
, and theelseif
was fine in the previous version. I don't understand exactly what is different between these implementations and my previous one or Flatten.jl, but it could be the additional complexity of the return objects? Or there is a little more happening at runtime inmodify_stateful
than I had inmapobject
or Flatten.jl, and the compiler is giving up on something?I also saw no difference between ConstructionBase versions.
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.
Seems its the layers of anonymous functions. Flatten.jl has the shared code inside the generated functions, and simple separate generated functions for getall/setall (flatten/reconstruct).
I much prefer the clarity of the current solution here. But it seems like if we want this to compile away we cant use anonymous functions inside the recursion.
I can't get this version to compile away even if you don't return any of the updated objects - so Julia can't even tell that the anonymous function is not modifying external state somehow and runs it even if it's not used.
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 does not seem to be related to anonymous functions per see. I played with eliminating all anonymous functions.
I replaced code like
with code like
It helped in very small examples, small examples still fail. Also,
@inline
everything did not help. Also eliminating all@generated
functions by manually defining allConstructionBase.f(::MyObject, ...)
did not help.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.
Damn, that's exhaustive.
It feels like the compiler is using some heuristic to give up on solving the returned state of the recursion instead of just trying. A single non-recursive
modify_stateful
compiles away completely, but it wont if called recursively.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.
Ok, think i found the issue. If you define the select and descent conditions like this it's type stable:
So the compiler optimises these methods differently to methods that are passed in in any way. Even with the function in the type instead of fields, or just passing a Type to a 2 arg version of
descend_condition
still doesn't work. It's just giving up and returningAny
. If you actually include the types to pass to 2 argdescend_condition(T, x)
in the code, it's type-stable.I had assumed previously that using a type from a type parameters was practically the same as writing the type in the code manually, but it seems like it isn't.
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.
Awesome that you found the issue! This looks like a bug to me, can you maybe produce a MWE and report it upstream?
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.
Yeah I'm cutting it down to size, but its probably 1.6 inference problems like JuliaLang/julia#35800.
It's all type stable on 1.4 - that's why this feels weird for performance intuition. These things used to just work.
Actually no its still all #35800, even on 1.4!! if you compile if with the types hard coded once then it knows when you use revise with the types as parameters, and is type stable. Then it's not on restart.