Skip to content

Commit c1d4597

Browse files
kseamonjelbourn
authored andcommitted
perf(table): Reduce calls to updateStickyColumnStyles (#19739)
* perf(table): Reduce the number of times sticky column positioning is computed. * Applied suggested readability improvements. (cherry picked from commit f484e96)
1 parent 62e217c commit c1d4597

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/cdk/table/table.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
465465
}
466466

467467
// Render updates if the list of columns have been changed for the header, row, or footer defs.
468-
this._renderUpdatedColumns();
468+
const columnsChanged = this._renderUpdatedColumns();
469+
const stickyColumnStyleUpdateNeeded =
470+
columnsChanged || this._headerRowDefChanged || this._footerRowDefChanged;
469471

470472
// If the header row definition has been changed, trigger a render to the header row.
471473
if (this._headerRowDefChanged) {
@@ -483,6 +485,10 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
483485
// connection has already been made.
484486
if (this.dataSource && this._rowDefs.length > 0 && !this._renderChangeSubscription) {
485487
this._observeRenderChanges();
488+
} else if (stickyColumnStyleUpdateNeeded) {
489+
// In the above case, _observeRenderChanges will result in updateStickyColumnStyles being
490+
// called when it row data arrives. Otherwise, we need to call it proactively.
491+
this.updateStickyColumnStyles();
486492
}
487493

488494
this._checkStickyStates();
@@ -790,22 +796,27 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
790796
* whether the sticky states have changed for the header or footer. If there is a diff, then
791797
* re-render that section.
792798
*/
793-
private _renderUpdatedColumns() {
799+
private _renderUpdatedColumns(): boolean {
794800
const columnsDiffReducer = (acc: boolean, def: BaseRowDef) => acc || !!def.getColumnsDiff();
795801

796802
// Force re-render data rows if the list of column definitions have changed.
797-
if (this._rowDefs.reduce(columnsDiffReducer, false)) {
803+
const dataColumnsChanged = this._rowDefs.reduce(columnsDiffReducer, false);
804+
if (dataColumnsChanged) {
798805
this._forceRenderDataRows();
799806
}
800807

801-
// Force re-render header/footer rows if the list of column definitions have changed..
802-
if (this._headerRowDefs.reduce(columnsDiffReducer, false)) {
808+
// Force re-render header/footer rows if the list of column definitions have changed.
809+
const headerColumnsChanged = this._headerRowDefs.reduce(columnsDiffReducer, false);
810+
if (headerColumnsChanged) {
803811
this._forceRenderHeaderRows();
804812
}
805813

806-
if (this._footerRowDefs.reduce(columnsDiffReducer, false)) {
814+
const footerColumnsChanged = this._footerRowDefs.reduce(columnsDiffReducer, false);
815+
if (footerColumnsChanged) {
807816
this._forceRenderFooterRows();
808817
}
818+
819+
return dataColumnsChanged || headerColumnsChanged || footerColumnsChanged;
809820
}
810821

811822
/**
@@ -875,7 +886,6 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
875886

876887
this._headerRowDefs.forEach((def, i) => this._renderRow(this._headerRowOutlet, def, i));
877888
this.updateStickyHeaderRowStyles();
878-
this.updateStickyColumnStyles();
879889
}
880890
/**
881891
* Clears any existing content in the footer row outlet and creates a new embedded view
@@ -889,7 +899,6 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
889899

890900
this._footerRowDefs.forEach((def, i) => this._renderRow(this._footerRowOutlet, def, i));
891901
this.updateStickyFooterRowStyles();
892-
this.updateStickyColumnStyles();
893902
}
894903

895904
/** Adds the sticky column styles for the rows according to the columns' stick states. */
@@ -1049,7 +1058,6 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
10491058
this._dataDiffer.diff([]);
10501059
this._rowOutlet.viewContainer.clear();
10511060
this.renderRows();
1052-
this.updateStickyColumnStyles();
10531061
}
10541062

10551063
/**

0 commit comments

Comments
 (0)