Skip to content

Commit

Permalink
perf: 优化 getDimensionValues 在大量 flatten 情况下的性能 (#2640)
Browse files Browse the repository at this point in the history
* perf: 优化 getDimensionValues 在大量 flatten 情况下的性能

* test: 单测修复
  • Loading branch information
wjgogogo authored Mar 29, 2024
1 parent b6e6015 commit 37aecc8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/s2-core/__tests__/unit/facet/pivot-facet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ jest.mock('@/data-set/pivot-data-set', () => {
getFieldsAndPivotMetaByField:
actualPivotDataSet.prototype.getFieldsAndPivotMetaByField,
displayFormattedValueMap: new Map(),
dimensionValuesCache: new Map(),
};
}),
};
Expand Down
17 changes: 16 additions & 1 deletion packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export class PivotDataSet extends BaseDataSet {
// sorted dimension values
public sortedDimensionValues: SortedDimensionValues;

private dimensionValuesCache: Map<string, string[]>;

getExistValuesByDataItem(data: DataType, values: string[]) {
return getExistValues(data, values);
}
Expand All @@ -95,6 +97,7 @@ export class PivotDataSet extends BaseDataSet {
this.sortedDimensionValues = {};
this.rowPivotMeta = new Map();
this.colPivotMeta = new Map();
this.dimensionValuesCache = new Map();
this.transformIndexesData(this.originData.concat(this.totalData), rows);
this.handleDimensionValuesSort();
}
Expand Down Expand Up @@ -337,6 +340,13 @@ export class PivotDataSet extends BaseDataSet {
return [];
}

const isGetAllDimensionValues = isEmpty(query);

// 暂时先对获取某一个维度所有的 labels 这样的场景做缓存处理,因为内部 flatten 逻辑比较耗时
if (this.dimensionValuesCache.has(field) && isGetAllDimensionValues) {
return this.dimensionValuesCache.get(field);
}

const dimensionValues = transformDimensionsValues(
query,
dimensions,
Expand All @@ -352,7 +362,12 @@ export class PivotDataSet extends BaseDataSet {
sortedDimensionValues: this.sortedDimensionValues,
});

return uniq(values.map((v) => v.value));
const result = uniq(values.map((v) => v.value));

if (isGetAllDimensionValues) {
this.dimensionValuesCache.set(field, result);
}
return result;
}

getTotalValue(query: Query, totalStatus?: TotalStatus) {
Expand Down
22 changes: 13 additions & 9 deletions packages/s2-core/src/utils/dataset/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import {
flatMap,
forEach,
get,
indexOf,
intersection,
isArray,
isEmpty,
isNull,
last,
set,
sortBy,
} from 'lodash';
import type { PickEssential } from '../../common/interface/utils';
import {
Expand Down Expand Up @@ -477,19 +475,25 @@ export function getSatisfiedPivotMetaValues(params: {

function flattenMetaValue(list: PivotMetaValue[], field: string) {
const allValues = flatMap(list, (metaValue) => {
const values = [...metaValue.children.values()];
return values.filter(
(v) =>
const values: PivotMetaValue[] = [];
for (const v of metaValue.children.values()) {
if (
v.value !== EMPTY_EXTRA_FIELD_PLACEHOLDER &&
(queryType === QueryDataType.All ? true : v.value !== TOTAL_VALUE),
);
(queryType === QueryDataType.All ? true : v.value !== TOTAL_VALUE)
) {
values.push(v);
}
}
return values;
});
if (list.length > 1) {
// 从不同父维度中获取的子维度需要再排一次,比如province => city 按照字母倒序,那么在获取了所有 province 的 city 后需要再排一次
const sortedDimensionValue = sortedDimensionValues[field] ?? [];
return sortBy(allValues, (item) =>
indexOf(sortedDimensionValue, item.id),
const indexMap = new Map(
sortedDimensionValue.map((id, index) => [id, index]),
);
allValues.sort((a, b) => indexMap.get(a.id) - indexMap.get(b.id));
return allValues;
}
return allValues;
}
Expand Down

0 comments on commit 37aecc8

Please sign in to comment.