Repository navigation
Improve WpcomChecklist: reduce re-renders and load dynamically in stats - #27926
Conversation
| siteSlug = getSiteSlug( state, siteId ), | ||
| posts = getPostsForQuery( state, siteId, query ), | ||
| contactPage = getContactPage( posts ) | ||
| ) => [ siteId, siteSlug, posts, contactPage ] |
There was a problem hiding this comment.
The only dependency we really need here is posts. Why? The contract of createSelector is:
- I'll keep a map of
args -> return_valueand return a memoized value for everyargsI know. In our case,argsare[ 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.
There was a problem hiding this comment.
Thanks, that's an excellent explanation! That will definitely help in the future, whenever I need to write another createSelector-based selector.
Also, fixed.
There was a problem hiding this comment.
@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.
|
|
||
| const sitesNeverShowBanner = store.get( storeKeyForNeverShow ); | ||
| if ( get( sitesNeverShowBanner, String( this.props.siteId ) ) === true ) { | ||
| if ( getNeverShowBannerStatus( this.props.siteId ) ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Looks like it isn't set anywhere else. Keeping a module-local cache, then :)
| { config.isEnabled( 'onboarding-checklist' ) && <WpcomChecklist viewMode="banner" /> } | ||
| { this.props.isEligibleForChecklist && | ||
| ! getNeverShowBannerStatus( this.props.siteId ) && ( | ||
| <AsyncLoad require="my-sites/checklist/wpcom-checklist" viewMode="banner" /> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Great suggestion! Implemented in the latest version.
|
#27275 by @sixhours introduced some complications for this PR. |
|
Marking as WIP while the issues introduced in #27275 aren't fixed. |
c19d4f8 to
2e1a13e
Compare
|
This PR is currently waiting on #28057. Once that's merged, I'll refactor this PR on top of that. |
9ec0368 to
343b9d2
Compare
|
This PR has been rewritten on top of the current state of |
3fec959 to
6130f47
Compare
|
Note to reviewers: #28303 may affect the logic in this PR. |
|
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 😞 |
0cb70ba to
e9f312b
Compare
|
Should be fixed and ready for review again. Fingers crossed no more merges will happen before this PR is reviewed and merged 🤞 |
|
The latest measurements seem to indicate an even larger impact: 25% improvement in |
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.
e9f312b to
810418a
Compare
|
More rebasing, due to further conflicts. @jsnajdr: Any chance for a review soon, to avoid more rebasing? 😅 |
jsnajdr
left a comment
There was a problem hiding this comment.
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 ] |
There was a problem hiding this comment.
Does ( state, siteId ) => [ getPostsForQuery( ... ) ] also work? The current selector can be easily confused by calling it with extra parameter: getTaskUrls( state, siteId, null ) 🙂
There was a problem hiding this comment.
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( '/' ), |
There was a problem hiding this comment.
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 }` : nulllooks better to me.
| site_id: siteId, | ||
| } ); | ||
| if ( this.props.track ) { | ||
| this.props.track( 'calypso_checklist_banner_close', { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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' ] ), | ||
| }; |
There was a problem hiding this comment.
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.
| ( some( post.metadata, { key: '_headstart_post', value: '_hs_contact_page' } ) || | ||
| post.slug === 'contact' ) | ||
| ); | ||
| return <AsyncLoad require="./component.jsx" { ...ownProps } />; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Oh, didn't know about that. Done in #28678.
|
|
||
| const userLib = userFactory(); | ||
|
|
||
| const query = { type: 'any', number: 10, order_by: 'ID', order: 'ASC' }; |
There was a problem hiding this comment.
This could be a constant with a nice uppercase name: FIRST_TEN_SITE_POSTS_QUERY.
|
Thanks for the review, @jsnajdr! I'll follow up on the fixes in a separate PR, then. Merging! 🎉 |
* 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.
This PR optimizes performance around
WpcomChecklista bit by:WpcomChecklistcomponent, caused by failing shallow comparisons in a prop coming from Redux. Applies memoization to the selector to fix the issue.WpcomChecklistin 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
createSelectorlibrary, 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:
perfmonmetric 👍Measured deltas in stats page, for sites with a checklist banner:
perfmonmetric (< 5% change)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:
perfmonenabledperfmonvalues 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.