Skip to content

refactor: avoid unnecessary render cycles in the data provider callback - #5647

Merged
tomivirkki merged 2 commits into
mainfrom
refactor/render-tree-in-one-iteration
Mar 10, 2023
Merged

tomivirkki merged 2 commits into
mainfrom
refactor/render-tree-in-one-iteration

Conversation

@tomivirkki

Copy link
Copy Markdown
Member

Description

This change improves how a data provider callback is processed in vaadin-grid-data-provider-mixin. It makes sure that the cache gets fully updated with all synchronously available data before actually rendering the grid rows. The updated data provider causes significantly fewer row rendering cycles when hierarchical data is applied to the grid.

This will be followed by a related PR to flow-component.

Type of change

Performance enhancement

Comment on lines -418 to -419
this._cache.updateSize();
this._effectiveSize = this._cache.effectiveSize;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moving the cache and effective size update outside the debouncer is the primary change here.

They were previously updated inside the flushing debouncer so any following update resulting from the size update actually caused yet another debouncer to get scheduled (and synchronously flushed). This caused a lot of unnecessary content updates, which affected rendering performance negatively.

Comment on lines -406 to -408
if (this._isExpanded(item) && currentItems.indexOf(item) > -1) {
// Force synchronous data request for expanded item sub-cache
cache.ensureSubCacheForScaledIndex(itemIndex);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This logic is no longer necessary since a similar (but improved) check was added below.

Comment on lines -256 to -259
it('should set itemHasChildren path by default', () => {
expect(grid.itemHasChildrenPath).to.equal('children');
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved this to the main level since I needed to assign a non-default value to to itemHasChildrenPath in beforeEach

@sonarqubecloud

sonarqubecloud Bot commented Mar 9, 2023

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

this._effectiveSize = this._cache.effectiveSize;

// After updating the cache, check if some of the expanded items should have sub-caches loaded
this._getVisibleRows().forEach((row) => {

@vursen vursen Mar 10, 2023 •

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.

Digging into this further, it seems the grid has an _effectiveSize observer that basically does the same. It requests items by calling virtualizer.update() => grid._getItem(...) => cache.ensureSubCacheForScaledIndex(...):

_effectiveSizeChanged(effectiveSize, virtualizer, hasData, columnTree) {

The observer's body is executed when this._hasData is true which is almost always the case except after you have cleared the cache. So, this ensureSubCacheForScaledIndex should be already invoked there.

Although, whether the observer is executed or not also depends on the presence of columnTree. Perhaps that one can be unavailable at the moment the data callback is resolving.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👍 Thx, I'll see if there's room for improvement there also

@tomivirkki
tomivirkki merged commit 3224d4e into main Mar 10, 2023
@tomivirkki
tomivirkki deleted the refactor/render-tree-in-one-iteration branch March 10, 2023 08:00
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.

2 participants