You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
and then makeMapLiteral is inlined. The incorrect type checks (issue #12891) is because makeLiteralMap creates a LinkedHashMap<dynamic,dynamic>, calls fillLiteralMap to insert the values (missing the required checks) and returns the map.
The attaching of runtime type info is done on the result of makeLiteralMap by code specially emitted in the builder, which adds more code than necessary at each literal.
Suggestion
What we should do instead is generate code that calls a factory constructor.
Add a factory constructor...
LinkedHashMap._literal(keysAndValues) {
var map = new LinkedHashMap<K, V>();
return fillLiteralMap(keysAndValues, map);
}
... then generate <T1,T2>{e1: e2, e3: e3, ...} exactly as if it had been written:
new LinkedHashMap<T1,T2>._literal([e1, e2, e3, e4, ...])
If the factory constructor is not inlined, the generated code for the would look like:
We could make code slightly smaller and avoid allocating temporary arrays by having some extra constructors for common small sizes:
LinkedHashMap._literal2(k1, v1, k2, v2) {
var map = new LinkedHashMap<K, V>();
map[k1] = v1;
map[k2] = v2;
return map;
}
new LinkedHashMap<String, int>._literal2("a", 1, "b", x)
In some programs there are many map literals with the same keys (but different values). This can be handled by splitting the keys and values in the constructor. The keys list could be shared by converting it to a const List where possible. Our running example would then be generated as if it had been written
new LinkedHashMap<String, int>._literal(const ["a", "b"], [1, x])
The text was updated successfully, but these errors were encountered:
Map literals are large and have incorrect type checks (issue #12891)
For
<String,int>{"a": 1, "b": x}
we currently generate:
H.setRuntimeTypeInfo(H.fillLiteralMap(["a", 1, "b", x], P.LinkedHashMap_LinkedHashMap(null, null, null, null, null)), [J.JSString, J.JSInt])
The smallest this can be (counting all identifiers as length=1, except "null") is 66 characters.
Is is orginally generated as
H.setRuntimeTypeInfo(H.makeLiteralMap(["a", 1, "b", x]), [J.JSString, J.JSInt])
and then makeMapLiteral is inlined. The incorrect type checks (issue #12891) is because makeLiteralMap creates a LinkedHashMap<dynamic,dynamic>, calls fillLiteralMap to insert the values (missing the required checks) and returns the map.
The attaching of runtime type info is done on the result of makeLiteralMap by code specially emitted in the builder, which adds more code than necessary at each literal.
Suggestion
What we should do instead is generate code that calls a factory constructor.
Add a factory constructor...
LinkedHashMap._literal(keysAndValues) {
var map = new LinkedHashMap<K, V>();
return fillLiteralMap(keysAndValues, map);
}
... then generate <T1,T2>{e1: e2, e3: e3, ...} exactly as if it had been written:
new LinkedHashMap<T1,T2>._literal([e1, e2, e3, e4, ...])
If the factory constructor is not inlined, the generated code for the would look like:
P.LinkedHashMap_LinkedHashMap$_literal(["a", 1, "b", x], J.JSString, J.JSInt)
... which could minify to 25 characters.
Smaller improvements
We could make code slightly smaller and avoid allocating temporary arrays by having some extra constructors for common small sizes:
LinkedHashMap._literal2(k1, v1, k2, v2) {
var map = new LinkedHashMap<K, V>();
map[k1] = v1;
map[k2] = v2;
return map;
}
new LinkedHashMap<String, int>._literal2("a", 1, "b", x)
In some programs there are many map literals with the same keys (but different values). This can be handled by splitting the keys and values in the constructor. The keys list could be shared by converting it to a const List where possible. Our running example would then be generated as if it had been written
new LinkedHashMap<String, int>._literal(const ["a", "b"], [1, x])
The text was updated successfully, but these errors were encountered: