-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Optimize generics to a dynamic dispatch for size #114598
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
Comments
|
@marcospb19 maybe? Assuming #77960 works without traits too, it would probably be. You can easily optimize generics that rely on traits to something computed at runtime, but not ones that don't use traits. That's why I made this request. |
Without the background in // Callee side
enum Input {
A(i32),
B(f64),
}
fn convert_to_string(input: Input) -> String {
match input {
Input::A(a) => a.to_string(),
Input::B(b) => b.to_string(),
}
}
// Caller side
convert_to_string(Input::A(0i32)); This would possibly be already possible with a proc macro attribute 🤔. But with the background of the RFC, we may have other reasons for a type_of! macro, but then there's the question how this would work with for example closures... |
Sorry, I got confused because your example uses the trait |
You can just get rid of the traits in the example; iirc I put them in there after some quick discussions and it was noted that this would be feasible at first only for those that use traits. |
Edit: I think this only works for types that don't need Drop or ones that take Drop types but never call drop (do those exist?) |
IDK if rustc supports variable length arrays. fn example(t: T) {
// 1. VLA
let array: [T; 8] = ...; // Variable size in runtime
// 2. Variable memory layout
let tuple = (0, t, 100);
&tuple.1; // What's the offset to access this? Variable in runtime
} |
When looking at my enum representation, this can be fairly easy answered. The Offset is the biggest of all used types for T |
Generic currently generate two similar functions to do something - for some cases, this isn't desired, like in "all I care about is size" cases. For this, it seems reasonable for the compiler to instead check the input types at runtime and figure out what to do based off of that. I'm not quite sure how much it would help, but it would help nonetheless.
So, for a theoretical example:
Given two inputs, an
i32
and af64
in this example, it would get turned into:But, if the compiler were to be told to optimize it as much as possible for size, this would instead get turned into something like:
Obviously, the example code is a bit weird and isn't valid, but the idea is there. Instead of creating two different functions, make one that matches on the types at runtime. The point here is to work without traits, which is why I did not suggest converting generics that rely on traits to a dynamic dispatch instead.
This would also be useful with variadic generics, I think.
The text was updated successfully, but these errors were encountered: