Skip to content

amp-bind: Expression complexity limit#8321

Merged
dreamofabear merged 7 commits intoampproject:masterfrom
dreamofabear:bind-expr-constraints
Mar 24, 2017
Merged

amp-bind: Expression complexity limit#8321
dreamofabear merged 7 commits intoampproject:masterfrom
dreamofabear:bind-expr-constraints

Conversation

@dreamofabear
Copy link
Copy Markdown

@dreamofabear dreamofabear commented Mar 22, 2017

Partial for #8057.

  • Add complexity limit for BindExpression to 50 nodes in the AST. "50" is a magic number that's double the size of expressions used in examples/bind/performance.amp.html.
  • Ignore this limit when testing manually, otherwise "game" and "turing" examples won't work. 😛

@dreamofabear dreamofabear changed the title Bind expr constraints amp-bind: Expression complexity limit Mar 22, 2017
@dreamofabear dreamofabear force-pushed the bind-expr-constraints branch from 3a4e204 to c1ba131 Compare March 22, 2017 22:57
@dreamofabear
Copy link
Copy Markdown
Author

/to @aghassemi @kmh287 PTAL

Copy link
Copy Markdown
Contributor

@kmh287 kmh287 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

const size = this.numberOfNodesInAst_(this.ast_);
const maxSize = opt_maxAstSize || DEFAULT_MAX_AST_SIZE;
const skipConstraint = getMode().localDev && !getMode().test;
if (size > maxSize && !skipConstraint) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be the message printed in the console right? If so, I'm wondering if we should give more context here. Someone unfamiliar with the amp-bind implementation might not know what to make of this message.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this error will eventually be passed to reportError(), which highlights the element that contains this expression in dev console.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nonetheless, I'm not sure that the error gives a clear picture of the problem to the uninitiated. Maybe add something like "Expression contains too many literals, variables, or function calls". Up to you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, I thought you meant contextual location rather than content. Done!

/** @const @private {!./bind-expr-defines.AstNode} */
this.ast_ = parser.parse(this.expressionString);

// Check if this expression string is too large (for performance).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: what's the performance impact of doing this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Looks like a few ms in total on examples/bind/performance.amp.html with 5x throttling.

Copy link
Copy Markdown
Contributor

@aghassemi aghassemi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool!

@dreamofabear
Copy link
Copy Markdown
Author

Thanks for the reviews!

@dreamofabear dreamofabear merged commit 6a85d8b into ampproject:master Mar 24, 2017
@dreamofabear dreamofabear deleted the bind-expr-constraints branch March 24, 2017 18:25
// Check if this expression string is too large (for performance).
const size = this.numberOfNodesInAst_(this.ast_);
const maxSize = opt_maxAstSize || DEFAULT_MAX_AST_SIZE;
const skipConstraint = getMode().localDev && !getMode().test;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a warning if we're in dev mode and we make it too big.

lironzluf pushed a commit to lironzluf/amphtml that referenced this pull request Mar 28, 2017
* master: (34 commits)
  Prevent amp-carousel next/previous icons fade away on desktop (ampproject#8428)
  Turn on flag slidescroll-disable-css-snap” (ampproject#8436)
  Revert "temporarily turn off yarn (ampproject#8356)" (ampproject#8384)
  initial commit (ampproject#8404)
  Upgrades for Index Exchange amp-ad tags to report load statistics (ampproject#8054)
  amp-bind validation tweak (ampproject#8414)
  Fix an amp-instagram race condition (ampproject#8192)
  Use whitelist to restrict urlReplacement for scoped analytics element (ampproject#8360)
  Report active experiments in error logs (ampproject#8108)
  amp-bind: Catch exceptions in mutatedAttributesCallback (ampproject#8383)
  Fixing custom scroll-snap on IOS (ampproject#8391)
  Add experiment for using AmpContext class in integration.js (ampproject#8348)
  add (ampproject#8349)
  swipe api (ampproject#8357)
  skip 3 flaky tests (ampproject#8388)
  amp-bind: Expression complexity limit (ampproject#8321)
  add margin-bottom (ampproject#8350)
  Flying carpet: make container full viewport and center content (ampproject#8292)
  Service Registration: Document Click (ampproject#7882)
  Add a8ad (ampproject#8036)
  ...
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
* initial commit for expr size constraint

* rename test-bind-expr.js to test-bind-expression.js

* unit test

* ignore constraint in localDev

* also ignore binding constraint in localDev

* don't skip constraints while testing

* improve error message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants