-
Notifications
You must be signed in to change notification settings - Fork 232
Optimizing for immutability with AngularDart #797
Comments
/cc @hterkelsen who started some of these optimizations, they might already exist. |
|
We already optimize in the case of fields declared final and literals, as well as string interpolations where every interpolated expression is const. However, we do not optimize getters that return const because we don't have access to the getter bodies (maybe this is wrong?). If we could access getter bodies, we could also add |
Should we bother? Should we focus instead on writing a lint that tells you to use a final field instead? |
In Flutter they use @immutable a lot. |
As a workaround, we could just require an annotation (i.e.
It's not always straight forward, for example overriding a getter of a base class: class A {
String get name => 'A';
}
class B extends A {
@override
String get name => 'B';
}
I don't know if that guarantees deep immutability |
I'm in support of adding an annotation for "effectively final" getters (for You can override a getter with a final field (or even a normal field). I think it would be a useful lint to detect getters that return literals. |
From what I understand from the React + Flutter world, the benefit of immutability is that you avoid rebuilding your component/widget (and the subsequent diff) tree if your inputs did not change. This check is fast since you can use But in the Angular world change detection is already using Isn't this what |
The optimization here is that if you have a component with, for example, this template: <b>{{message}}</b> Normally, we will generate code that detects the value of |
You can override a getter with a final field. class A {
String get name => 'A';
}
class B extends A {
@override
final name = 'B';
} If a getter has a const expression on the RHS I think it could always be a final field. Perhaps there are times we prefer a |
Okay, makes sense to me- I think I misunderstood what sort of Immutability. I was thinking of cases where @input objects were 'immutable' (built_value or something), not parts of the component class. |
Closing as not actionable right now. We expect to focus more on change detection in the coming months and might have other issues that try to tackle things like this. |
There is increasing requests to help optimize for immutable data structures in AngularDart.
(Somewhat related to documentation in #464.)
There are some optimizations we (should) do already today (and should test for):
const
objects are deeply immutable:... should be easily compilable to just
'Version v1.0.0'
without change detection.final
fields and getters that returnconst
objects are immutable:... again, similar to above, with or without a
static
keyword.In this case, we can also make some assumptions around literal types (always const):
bool
num
,int
,double
null
String
const []
const {}
We (should?) understand that APIs that return literals are effectively const:
We can't inline
version
too eagerly, but we could still only (always) check it once.Fun! But that's not everything. What about:
We could add a
package:angular_meta
and a@trustDeepImmutable
, for example.This could be used safely on types provided by
built_value
, or other value types. If we ever get@sealed
(or similar), we could infer this automatically for some types, but it's not safe yet.For example, and immutable list where individual items are not immutable. Not sure yet.
*ngIf
For example
*ngIf="true"
could just be removed, and*ngIf=false
could strip out the code.*ngFor
or a*ngForOnce
)This is a tracking issue, and we can create additional issues as needed.
The text was updated successfully, but these errors were encountered: