Skip to content
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

Edit Post: Select blocks only once multiple verified #12477

Merged
merged 1 commit into from
Nov 30, 2018

Conversation

aduth
Copy link
Member

@aduth aduth commented Nov 30, 2018

Related: #5031

This pull request seeks to optimize the multiple-use validation to avoid calling the getBlocks selector until after verifying that a non-multiple-supporting block is present in the post. This should help alleviate some performance bottleneck for a currently-underperforming selector (getBlocks), particularly when the the condition will not apply for the vast majority of blocks.

This is effectively a refactoring to reorganize code to only assign a variable value at a point where it is certain to be relevant for use by the remainder of the function. As an aside, it would be very interesting to try to develop a lint rule to verify that a variable is used before the next return statement.

Testing instructions:

Repeat testing instructions from #5031

Add a console.count at the point the getBlocks function is called, and verify that it is not called while authoring paragraph content (unlike before).

@aduth aduth added [Type] Performance Related to performance efforts [Type] Code Quality Issues or PRs that relate to code quality labels Nov 30, 2018
Copy link
Member

@jorgefilipecosta jorgefilipecosta 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 in my tests, as expected no behavior changes were noticed 👍
As referred, this would for sure be an excellent lint rule. Maybe it was already implemented by someone and is available.
The end 2 end tests fail seems totally unrelated and an intermittent problem, I restarted them.

@aduth
Copy link
Member Author

aduth commented Nov 30, 2018

As referred, this would for sure be an excellent lint rule. Maybe it was already implemented by someone and is available.

I looked around a bit, and unfortunately couldn't find much. I started working on a rule, and found a few edge cases, mostly stylistic:

  • We often destructure-assign a variable which may not be used before the return (example)
    • Solution Exempt destructuring?
  • Sometimes it's not a "big deal" to assign a variable (example)
    • Solution: Limit to expensive operations; namely when initialized to CallExpression (a function call)?

Here's my work-in-progress:

https://astexplorer.net/#/gist/dc29d9096eac1664d8eb287139fa55ac/94f70e52c05456e1092f3109e5f882d60b68f076

It finds a lot of very interesting faults!

Full report
/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/bin/packages/watch.js
  40:5  error  Assigned variable `filePath` needlessly assigned  rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/block-library/src/cover/index.js
329:5 error Assigned variable classes needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/block-library/src/image/edit.js
419:4 error Assigned variable imageSizeOptions needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/blocks/src/api/factory.js
125:3 error Assigned variable sourceBlock needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
131:3 error Assigned variable sourceBlock needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/blocks/src/api/serializer.js
271:4 error Assigned variable saveAttributes needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/core-data/src/queried-data/selectors.js
33:3 error Assigned variable page needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
33:3 error Assigned variable perPage needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
38:3 error Assigned variable page needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
38:3 error Assigned variable perPage needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/dom/src/dom.js
395:4 error Assigned variable selection needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
399:3 error Assigned variable selection needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/edit-post/src/hooks/validate-multiple-use/index.js
42:4 error Assigned variable blocks needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/editor/src/components/block-drop-zone/index.js
101:4 error Assigned variable srcRootClientId needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
101:4 error Assigned variable srcIndex needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/editor/src/components/block-switcher/index.js
61:5 error Assigned variable blockType needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/editor/src/components/post-saved-state/index.js
69:4 error Assigned variable hasPublishAction needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
78:4 error Assigned variable hasPublishAction needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
82:4 error Assigned variable hasPublishAction needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
86:4 error Assigned variable hasPublishAction needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/editor/src/components/rich-text/index.js
246:5 error Assigned variable item needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
621:4 error Assigned variable record needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/editor/src/editor-styles/ast/parse.js
179:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
189:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
236:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
242:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
304:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
323:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
330:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
335:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
347:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
367:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
372:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
372:4 error Assigned variable supports needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
378:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
378:4 error Assigned variable supports needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
378:4 error Assigned variable style needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
397:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
401:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
407:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
407:4 error Assigned variable style needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
425:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
430:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
430:4 error Assigned variable media needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
436:4 error Assigned variable media needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
436:4 error Assigned variable style needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
436:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
454:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
472:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
478:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
491:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
509:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
516:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
516:4 error Assigned variable vendor needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
516:4 error Assigned variable doc needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
522:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
522:4 error Assigned variable vendor needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
522:4 error Assigned variable doc needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
522:4 error Assigned variable style needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
541:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
545:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
558:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
595:5 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
634:4 error Assigned variable pos needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/editor/src/editor-styles/ast/stringify/identity.js
250:3 error Assigned variable indent needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/editor/src/store/effects.js
167:4 error Assigned variable blockB needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/editor/src/store/effects/posts.js
58:3 error Assigned variable post needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

/Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/packages/editor/src/store/selectors.js
298:4 error Assigned variable edits needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return
355:3 error Assigned variable password needlessly assigned rule-no-unused-vars-before-return/no-unused-vars-before-return

@aduth aduth added this to the 4.7 milestone Nov 30, 2018
@aduth aduth merged commit 459984e into master Nov 30, 2018
@aduth aduth deleted the update/validate-multiple-fast-path-return branch November 30, 2018 19:48
@jorgefilipecosta
Copy link
Member

jorgefilipecosta commented Nov 30, 2018

Nice exploration 👍

We often destructure-assign a variable which may not be used before the return (example)
Solution Exempt destructuring?

For the destructuring case, I guess an error should be thrown if none of the variables destructured is used before the return if at least one is used I think it is fine.

Solution: Limit to expensive operations; namely when initialized to CallExpression (a function call)?

Yes, I think only limiting to function calls makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Code Quality Issues or PRs that relate to code quality [Type] Performance Related to performance efforts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants