refactor(compiler): allow strings with certain length to be included into `ConstantPool` (#38213)

Prior to this commit, the `ConstantPool` ignored all primitive values. It turned out that it's
beneficial to include strings above certain length to the pool as well. This commit updates the
`ConstantPool` logic to allow such strings to be shared across multiple instances if needed.
For instance, this is helpful for component styles that might be reused across multiple components
in the same file.

PR Close #38213
This commit is contained in:
Andrew Kushnir 2020-07-23 18:47:33 -07:00 committed by Misko Hevery
parent 2fdc18b42c
commit 8fdfa7b604
1 changed files with 14 additions and 1 deletions

View File

@ -36,6 +36,13 @@ export const enum DefinitionKind {
*/ */
const KEY_CONTEXT = {}; const KEY_CONTEXT = {};
/**
* Generally all primitive values are excluded from the `ConstantPool`, but there is an exclusion
* for strings that reach a certain length threshold. This constant defines the length threshold for
* strings.
*/
const POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS = 50;
/** /**
* A node that is a place-holder that allows the node to be replaced when the actual * A node that is a place-holder that allows the node to be replaced when the actual
* node is known. * node is known.
@ -96,7 +103,8 @@ export class ConstantPool {
private nextNameIndex = 0; private nextNameIndex = 0;
getConstLiteral(literal: o.Expression, forceShared?: boolean): o.Expression { getConstLiteral(literal: o.Expression, forceShared?: boolean): o.Expression {
if (literal instanceof o.LiteralExpr || literal instanceof FixupExpression) { if ((literal instanceof o.LiteralExpr && !isLongStringExpr(literal)) ||
literal instanceof FixupExpression) {
// Do no put simple literals into the constant pool or try to produce a constant for a // Do no put simple literals into the constant pool or try to produce a constant for a
// reference to a constant. // reference to a constant.
return literal; return literal;
@ -305,3 +313,8 @@ function invalid<T>(this: o.ExpressionVisitor, arg: o.Expression|o.Statement): n
function isVariable(e: o.Expression): e is o.ReadVarExpr { function isVariable(e: o.Expression): e is o.ReadVarExpr {
return e instanceof o.ReadVarExpr; return e instanceof o.ReadVarExpr;
} }
function isLongStringExpr(expr: o.LiteralExpr): boolean {
return typeof expr.value === 'string' &&
expr.value.length > POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS;
}