-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
size_of_val
in a generator can make the generator bigger
#62321
Comments
It would be good to elaborate on why |
My thinking is that people will often want to use In particular, since you can't name the type returned by an It's definitely going to confuse people that adding |
I'm still not sure why
Shouldn't this not somehow proved by the borrow-checker? I'm obviously not into compiler details and therefore don't understand the exact approach, but I'm wondering whether
|
Why can't |
@Matthias247 #59087 tracks the problem of borrows disabling optimizations in general. In order to fix this general problem, we probably need
For However, this comes with its own challenge (3): we need to compute the size of our type before MIR inlining, or otherwise stick a "placeholder" value in MIR that gets replaced later. We'll have to tackle this challenge regardless of whether we solve the "general" problem, so this issue exists to track this particular case. EDIT: Now I see that the example in #59087 was originally using |
@withoutboats Yes, that would work, and it might be easier than the solution I'm describing. We can't do this with a desugaring today, though, which AFAIK is how we implement |
Actually, could we "inline" |
An alternative to inlining is to eliminate the move in |
Isn't there a And, yes, you can totally name the type. During and after typeck, that type parameter of (The reason you want a |
Are there plans to make this possible someday? More generically, I think this would be a matter of allowing opaque types to be named? This would also be useful for storing non-boxed Futures in a container like Vec. I managed to get this to work using type inference, but it felt a little dirty. |
Is it guaranteed that That said, I don't see how that could help with getting the size of an |
I think it could help? Here's getting the size using async fn foo() {
}
fn size_of<T>(_t: T) -> usize {
std::mem::size_of::<T>()
}
fn main() {
println!("{}", size_of(foo()));
} So if it were possible to write the type name, then a constructed value shouldn't be needed? |
That's not even using a type alias, though! I didn't know you could just call |
@jkarneges You can just return a pair of both |
Hmm, I suppose what I'm asking for is not an alias for all kinds of an opaque type, but an alias for a single kind of an opaque type. For example, if I have two async fn's foo() and bar(), each returning distinct |
Consider the following code:
Today, having the
dbg!
line roughly doubles the size of the future returned byfoo
. More precisely, it causes us to allocate storage forx
twice (once forx
, and once for thepinned
variable thatx
is moved into inside theawait
).This unfortunate state of events is caused by the fact that we cannot "peer into"
size_of_val
and see that the address ofx
never escapes the function. Without knowing this, we can't optimize away the storage ofx
once it has been moved.This was first discussed here: #59123 (comment). One promising solution (suggested by @RalfJung) is that we inline all occurrences of
size_of_val
(and possibly some other intrinsics) in a MIR pass, before we get to the generator transform.The text was updated successfully, but these errors were encountered: