-
Notifications
You must be signed in to change notification settings - Fork 213
Compiler should try to make everything const #1410
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
What about the cases where you don't want |
I tried to think about those cases but couldn't find any that I wouldn't want |
Any case where you want your classes to be separate instances and refer to different objects although with the same parameters. |
Could you give me an example? This sounds a bit like bad practice. The kind of thing Value Objects ( |
Maybe we can have |
In a OO context it is not a bad practice, although me myself never use it this way, as I usually program in a funcional way. |
One example is Also, what about I guess that's cheating because |
I see... So could we have a shortcut for This could make things a lot more convenient and comfortable in Flutter. Also we would get a performance increase as more people won't ignore const for lazyness or readability/80 characters limit. |
To offer my perspective on this within Flutter: I find const annoying when I'm rapidly prototyping w/ hot reload as I have to recompile to change something declared as const. For this reason I think automatically making everything const would be a pain, but it would definitely offer performance benefits in production. |
I've seen that being claimed from time to time. Does it really have a non negligible impact ? Has someone done some testing ? Maybe one of the command can while in to shine some light on that |
Yes it does, specially for complex animations. Imagine you have to recreate a complex widget tree 60 times per second, with const you can optimize a lot of it. |
Have you tested it ? Is there somewhere I can see the results ? I'm not trying to be condescending, I just would like proof of those claims. In my tests which could have very well been wrong, I didn't see a difference so I assumed it was a myth and preemptive optimisation. However my tests might have been faulty as I tried that while learning flutter, so very newbie to the language. |
I didn't, but I remember seeing something a while ago. |
Just did a v small preliminary test with a few Text & Icon widgets, rebuilding the widget tree very fast. Bear in mind this was very minimal compared to the widget tree of a complete app. However, with Flutter's profile build I saw an average of about 5fps increase when using const. I'll see if I can write a more complete example later today. |
It would be nice to have the source @jaddison06 . I could see const having an impact at the top of the tree. |
I reckon my test is flawed for the same reason. As I said, I'll have a look at doing a complete example, and post the source of that. |
It doesn't need to. The benefit of using
This is because even if its parent rebuild, a const widget will not rebuild. Since widgets are immutable, Flutter is able to know that this widget did not change and won't rebuild it. So yes, using const for widgets can have a significant impact |
This is false. Here's a demonstration: void main() {
runApp(
StreamBuilder<void>(
stream: Stream.periodic(Duration(seconds: 1), (_) {}),
builder: (context, value) {
return MyWidget();
},
),
);
}
class MyWidget extends StatelessWidget {
const MyWidget();
@override
Widget build(context) {
print('did build MyWidget');
return Container();
}
} This snippet will print Change the code to That means without That's a significant cost. |
Yes, that's why I am pointing out that |
I might be late to the discussion but there are some tools that can help to avoid manually fixing const warnings and speed up prototyping. If using VSCode, you can add P.S. Originally discovered the solution here: https://www.youtube.com/watch?v=_RGgJFCRyaM&list=WL&index=16&ab_channel=FunwithFlutter |
@OlegBezr unconditionally applying |
For history, during the Dart 2.0 language design, we considered automatically making all expressions that could be constant also be constant. Examples included things like The reason we ended up not doing so was that the failure modes were scary, and that it wasn't clear how to handle list/map literals. If you write With a rule like that, every time you forget to write that A feature which an easy-to-hit failure case, and with almost zero ability for tools to to catch that failure (because it's still valid code with the same static type and at least the same capabilities), meant that we ended up preferring the current approach where you have to ask for something to be constant. |
Very insightful, thanks for the clarification Lasse! |
Why not autoconst only objects with all final literals and work your way up from there. A(int, int) could be const The pain of having to fix code during rapid prototyping is the problem. I'd rather have things that are obviously const be const for me without the warning and without the keyword. Sacrificing const in more grey areas is worth the price. |
We considered making "potentially constant expression with all constant arguments/elements/types" be automatically constant. The problem was collections. Should Should It was then considered too dangerous to make Maybe it would be more viable to say that in the context of a That is the main argument against implicit So if you want something to be constant, you need to say that it's constant, so that the compiler can warn you if it isn't. |
@lrhn thanks for the thorough explanation, as usual, I think those are fair considerations and the lint solution should be enough. |
We could just have different compiler/IDE configurations for development and production modes, right? |
I mainly use Dart for Flutter and declaring Widgets as
const
brings performance improvements. On the other hand it's boring to typeconst
all the time, or verifying which constructors areconst
, or even having to fix a constructor call that just got broken because it was declared asconst
and now can't be evaluated at compile time.A linter warning helps to remember to use
const
, but actually using them isn't much comfortable.It would be amazing if the compiler could automatically understand what can be promoted to
const
and optimize the code asconst
behind the scenes.The text was updated successfully, but these errors were encountered: