Skip to content

Allow side-effect-free functions to be called at compile time (in constants) #29277

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

Closed
Hixie opened this issue Apr 6, 2017 · 2 comments
Closed
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-stale Closed as the issue or PR is assumed stale type-enhancement A request for a change that isn't a bug

Comments

@Hixie
Copy link
Contributor

Hixie commented Apr 6, 2017

I have a class in Flutter (Flow) that takes a List<Widget> and passes a new list to its superclass constructor, the new list consisting of the incoming list mapped in a well-defined manner (each entry is wrapped in an instance of another class). All the information is available to evaluate this entire operation at compile time, and being able to do it then would allow big performance wins (because it would allow us to build the entire widget subtree once as a constant, which would then be == to itself each time it was referenced and thus we could avoid doing any work rebuilding that part of the tree).

@lrhn
Copy link
Member

lrhn commented Apr 6, 2017

There are limits to what operations we can reasonably allow as compile-time constant operations.
I don't see applying a map operation to a constant list as something that we will allow. You don't get to iterate at compile-time. You don't get to call arbitrary function.

Even if we are able to declare some kind of "side-effect free" functions (which will probably have to be just a return followed by a "potentially const function"), and we allow calling these at compile time, with some way to avoid recursion, then you still won't be able to iterate a list.

Could you define what behavior your really need, not in terms of const (which is a hodge-podge of features - immutability, evaluate-once and canonicalization) but, perhaps, in terms of those individually.
Do you need immutability? Do you need canonicalization (sounds like it)?

@vsmenon vsmenon added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Apr 6, 2017
@Hixie
Copy link
Contributor Author

Hixie commented Apr 6, 2017

The key feature is being able to run build functions many times, and be confident that the leaf subtree that is built the same way each time will be considered == very fast when we compare it to the last one.

So for example if you have a dialog box that always shows the same message, then while the dialog box is being animated onto the screen, the entire subtree representing the contents of the dialog should cost nothing to display. Ideally, the entire thing would be pre-built at compile time and just inserted when we display the dialog box. Sometimes, building these subtrees requires some manipulation, like wrapping every item in a list with another, or checking for nulls, or whatever.

We get huge wins if we can avoid walking the tree of widgets when it "hasn't changed". The problem is that it's typically very difficult for us to tell that it hasn't changed, since we return new instances each time.

@lrhn lrhn added the type-enhancement A request for a change that isn't a bug label Jun 25, 2018
@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Apr 9, 2025
@lrhn lrhn added the closed-stale Closed as the issue or PR is assumed stale label Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-stale Closed as the issue or PR is assumed stale type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants