Skip to content

Improve WpcomChecklist: reduce re-renders and load dynamically in stats - #27926

Merged
sgomes merged 5 commits into
masterfrom
update/improve-wpcom-checklist
Nov 19, 2018
Merged

sgomes merged 5 commits into
masterfrom
update/improve-wpcom-checklist

Conversation

@sgomes

@sgomes sgomes commented Oct 18, 2018 •

Copy link
Copy Markdown
Contributor

This PR optimizes performance around WpcomChecklist a bit by:

  • Reducing re-renders in the WpcomChecklist component, caused by failing shallow comparisons in a prop coming from Redux. Applies memoization to the selector to fix the issue.
  • Dynamically loading WpcomChecklist in stats, and only doing so when the site qualifies and the checklist hasn't been dismissed.

Note to reviewers: please pay special attention to the memoization code added in the first commit. I still haven't fully grokked the createSelector library, so I want to make sure I avoid introducing any subtle bugs. I couldn't find any, but, well, that's why they'd be subtle 😄

Measured deltas in stats page, for sites without a checklist banner:

  • ~12% improvement in the perfmon metric 👍
  • ~8KB less JS (gzipped) 👍

Measured deltas in stats page, for sites with a checklist banner:

  • No apparent performance improvement in the perfmon metric (< 5% change)
  • ~500B more JS (gzipped) 👎 Seems acceptable, though.

Testing instructions:

  • Select a site with no active checklist banner

  • Open the stats page and ensure there are no issues

  • Select a site with an active checklist banner

  • Open the stats page and ensure there are no issues

Performance analysis:

  • Create a build with perfmon enabled
  • Set CPU throttling to 6x, to reduce sample noise
  • Load the stats page once
  • Alternate between the activity and stats pages, taking note of the perfmon values for the stats page. If possible, automate this, to reduce sample noise introduced by mouse pointer movement.

Perform the process above for a site with a checklist banner, and one without.

@sgomes
sgomes requested review from dmsnell and jsnajdr October 18, 2018 11:30
@matticbot

Copy link
Copy Markdown
Contributor

@sgomes sgomes added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. [Type] Performance Checklist Enhancement Changes to an existing feature — removing, adding, or changing parts of it labels Oct 18, 2018
@Automattic Automattic deleted a comment Oct 18, 2018
siteSlug = getSiteSlug( state, siteId ),
posts = getPostsForQuery( state, siteId, query ),
contactPage = getContactPage( posts )
) => [ siteId, siteSlug, posts, contactPage ]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The only dependency we really need here is posts. Why? The contract of createSelector is:

  • I'll keep a map of args -> return_value and return a memoized value for every args I know. In our case, args are [ siteId ].
  • every time dependencies change (i.e., the deps array is not shallow-equal to the previous value), it means the memoized values are invalidated. Therefore I'll clear the memoization map and recalculate the results when requested.

This means that siteId and siteSlug are not good dependencies to have: calling the selectors with a different siteId value will forget the previous memoized value.

selector( 'site1' );
selector( 'site2' );
selector( 'site1' );

won't remember the value for site1.

contactPage depends on posts and nothing else. Therefore it's not an independent dependency. Harmless, but redundant.

That leaves us with posts. Theoretically, siteSlug could also change for the same siteId if we received a new, different state.sites.items value. But it practice that will probably never happen -- except edge cases like having just bought and mapped a new primary domain.

@sgomes sgomes Oct 22, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that's an excellent explanation! That will definitely help in the future, whenever I need to write another createSelector-based selector.

Also, fixed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@sgomes in case you haven't come across it yet either there's treeSelect which @samouri created to address a sneaky problem particularly on site-dependent selectors: once that cache flushes it's gone even if we flip back and forth between sites and return to the first one.

probably not worth the changeover in this PR, but if it would be helpful to have real memoization instead of a last-used-cache we can think about that one too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @dmsnell ! I'll take a look.


const sitesNeverShowBanner = store.get( storeKeyForNeverShow );
if ( get( sitesNeverShowBanner, String( this.props.siteId ) ) === true ) {
if ( getNeverShowBannerStatus( this.props.siteId ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This reads from local storage on every render -- not nice. It's a preexisting issue, not introduced by this patch at all, you just put it into spotlight 💡 There's plenty of other places in Calypso that implement the "hide and never show again" logic. Usually by storing a preference bit on backend.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I could cache the value in memory. I'll take a look to see if I can find all the points in the code where this property is changed, and make them go through the new module.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Looks like it isn't set anywhere else. Keeping a module-local cache, then :)

Comment thread client/my-sites/stats/site.jsx Outdated
{ config.isEnabled( 'onboarding-checklist' ) && <WpcomChecklist viewMode="banner" /> }
{ this.props.isEligibleForChecklist &&
! getNeverShowBannerStatus( this.props.siteId ) && (
<AsyncLoad require="my-sites/checklist/wpcom-checklist" viewMode="banner" />

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here's an idea how to implement the async loading in a more reusable way:

wpcom-checklist/index.js becomes just a stub that does the async load:

const ChecklistLoader = ( { isVisible, ...restProps } ) => isVisible && (
  <AsyncLoad { ...restProps } require="./component.js" />
);
export connect( state => ( { isVisible: isEligible( state ) } ) )( ChecklistLoader );

wpcom-checklist/component.js now contains the "real" code:

class Checklist extends Component {
  /* load lot of code and render complex UI */
}

Now everyone who imports my-sites/checklist/wpcom-checklist will get it async-loaded by default, for free. No room for making mistakes like loading it synchronously.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great suggestion! Implemented in the latest version.

@jsnajdr

jsnajdr commented Oct 22, 2018

Copy link
Copy Markdown
Member

#27275 by @sixhours introduced some complications for this PR. WpcomChecklist can now be 4 different components depending on the viewMode prop. Some depend on the isEligible... selector, some shouldn't. Namely, the "notification" one is rendered even for non-eligible sites. It doesn't render any UI, just calls some callbacks in lifecycle methods. Maybe it shouldn't render on non-eligible sites? Something to untangle and update this PR accordingly 🙂

@sgomes

sgomes commented Oct 22, 2018

Copy link
Copy Markdown
Contributor Author

Marking as WIP while the issues introduced in #27275 aren't fixed.

@sgomes sgomes added [Status] In Progress and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Oct 22, 2018
@sgomes
sgomes force-pushed the update/improve-wpcom-checklist branch from c19d4f8 to 2e1a13e Compare October 22, 2018 15:57
@sgomes

sgomes commented Oct 25, 2018 •

Copy link
Copy Markdown
Contributor Author

This PR is currently waiting on #28057. Once that's merged, I'll refactor this PR on top of that.

@sgomes
sgomes force-pushed the update/improve-wpcom-checklist branch 2 times, most recently from 9ec0368 to 343b9d2 Compare November 2, 2018 16:44
@sgomes sgomes added [Status] Ready to Merge [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress [Status] Ready to Merge labels Nov 2, 2018
@sgomes

sgomes commented Nov 2, 2018

Copy link
Copy Markdown
Contributor Author

This PR has been rewritten on top of the current state of master and is ready for another review!

@sgomes

sgomes commented Nov 6, 2018

Copy link
Copy Markdown
Contributor Author

Note to reviewers: #28303 may affect the logic in this PR.

@sgomes

sgomes commented Nov 15, 2018

Copy link
Copy Markdown
Contributor Author

Setting this to "In progress". The branch has grown stale and I'm trying to rebase it on top of master, but it's tricky 😞

@sgomes sgomes added [Status] In Progress and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Nov 15, 2018
@sgomes
sgomes force-pushed the update/improve-wpcom-checklist branch 2 times, most recently from 0cb70ba to e9f312b Compare November 15, 2018 14:53
@sgomes

sgomes commented Nov 15, 2018

Copy link
Copy Markdown
Contributor Author

Should be fixed and ready for review again. Fingers crossed no more merges will happen before this PR is reviewed and merged 🤞

@sgomes sgomes added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress labels Nov 15, 2018
@sgomes

sgomes commented Nov 15, 2018

Copy link
Copy Markdown
Contributor Author

The latest measurements seem to indicate an even larger impact: 25% improvement in perfmon and 9KB less JS. This is likely due to the new features that have been added to WpcomChecklist since the PR was first published.

This prop wasn't being memoized in the Redux selector, which was causing
large numbers of unnecessary re-renders.
WpcomChecklist loads a sizeable amount of code, but it's often only
needed in sites that qualify and haven't completed the checklist.

Instead of statically including and instancing the component every
time, this PR instead extracts some minimal exports to determine
whether it makes sense to display the WpcomChecklist content. Only
if these checks are passed does WpcomChecklistComponent then get
asynchronously loaded and instanced.
This commit also removes the "CheckListNotification" class, moving that
functionality to the main WpcomChecklist class, and moves propTypes and
defaultProps to the WpcomChecklist class.
@sgomes
sgomes force-pushed the update/improve-wpcom-checklist branch from e9f312b to 810418a Compare November 16, 2018 13:12
@sgomes

sgomes commented Nov 16, 2018

Copy link
Copy Markdown
Contributor Author

More rebasing, due to further conflicts.

@jsnajdr: Any chance for a review soon, to avoid more rebasing? 😅

@jsnajdr jsnajdr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm reporting a lot of issues, but the PR is ready to merge anyway. The best course of action is probably to merge now and do a few followup PRs. Otherwise we might end up resolving a lot of conflicts again.

contact_page_updated: contactPageUrl,
};
},
( state, siteId, posts = getPostsForQuery( state, siteId, query ) ) => [ posts ]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does ( state, siteId ) => [ getPostsForQuery( ... ) ] also work? The current selector can be easily confused by calling it with extra parameter: getTaskUrls( state, siteId, null ) 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, interesting! As far as I can tell it works fine, but I'd appreciate a close look at the behaviour if you get a chance. createSelector still leaves me nervous 😄 Done in #28678


const taskUrls = {
return {
post_published: compact( [ '/post', siteSlug, get( firstPost, [ 'ID' ] ) ] ).join( '/' ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I know it's existing unmodified code, but the published post URL only makes sense when both siteSlug and firstPost.ID are valid.

siteSlug && firstPost ? `/post/${ siteSlug }/${ firstPost.ID }` : null

looks better to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in #28678.

site_id: siteId,
} );
if ( this.props.track ) {
this.props.track( 'calypso_checklist_banner_close', {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where do we (or don't) pass the track prop? I can't find it. <ChecklistComponent> doesn't have this prop when being rendered in WpcomChecklistComponent.

Usually we do analytics by dispatching actions from state/analytics/actions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can't find any usage of it anywhere; must be old code. I remember I added this check because it kept failing; the error must have been there for a while. I'll remove it. Done in #28678.

isEligibleForDotcomChecklist: isEligibleForDotcomChecklist( state, siteId ),
siteId,
taskStatuses: get( getSiteChecklist( state, siteId ), [ 'tasks' ] ),
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of a bag of rather random properties, this mapStateToProps should return the two booleans the component acually cares about: shouldRender and shouldShowNotification. I.e., the calculation should be moved from the React component into the connect call.

Then the React component will get updated (and the setNotification side effects fired) if and only if one of these booleans changed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great idea! Done in #28678.

( some( post.metadata, { key: '_headstart_post', value: '_hs_contact_page' } ) ||
post.slug === 'contact' )
);
return <AsyncLoad require="./component.jsx" { ...ownProps } />;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I recommend to use the "canonical" import name: require="my-sites/checklist/wpcom-checklist". The import name is used to generate the webpack chunk name. Which is now async-load-component-jsx.js

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, didn't know about that. Done in #28678.


const userLib = userFactory();

const query = { type: 'any', number: 10, order_by: 'ID', order: 'ASC' };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could be a constant with a nice uppercase name: FIRST_TEN_SITE_POSTS_QUERY.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in #28678.

@sgomes

sgomes commented Nov 19, 2018

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @jsnajdr! I'll follow up on the fixes in a separate PR, then.

Merging! 🎉

@sgomes
sgomes merged commit 422b401 into master Nov 19, 2018
@sgomes
sgomes deleted the update/improve-wpcom-checklist branch November 19, 2018 10:47
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Nov 19, 2018
sgomes added a commit that referenced this pull request Nov 19, 2018
* Add some error checking to URL generation

* Move render/no-render and notify/no-notify logic to connect.

* Rename query constant in component.jsx

* Use canonical name for component loading

* Remove unused track prop from banner-mode checklist.

* Tweak  in component.js to make it less fragile.

* Re-enable tracking for banner close.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Checklist Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Type] Performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants