Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
perf(formula): optimize incremental formula updates (#7265)
Univerf294c7fcd12a
diff --git a/packages/engine-formula/src/models/__tests__/formula-data.model.spec.ts b/packages/engine-formula/src/models/__tests__/formula-data.model.spec.tsindex 5f0e65b6ca..9f72f57f44 100644--- a/packages/engine-formula/src/models/__tests__/formula-data.model.spec.ts+++ b/packages/engine-formula/src/models/__tests__/formula-data.model.spec.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { IBaseSnapshot, ICellData, Injector, IWorkbookData, Nullable, Univer } from '@univerjs/core';+import type { IBaseSnapshot, ICellData, Injector, IWorkbookData, Nullable, Univer, Workbook } from '@univerjs/core';import type { IFormulaData } from '../../basics/common';import {BaseDataModel,@@ -27,7 +27,7 @@ import {RANGE_TYPE,UniverInstanceType,} from '@univerjs/core';-import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { FormulaDataModel, initSheetFormulaData } from '../formula-data.model';import { createCommandTestBed } from './create-command-test-bed';@@ -199,9 +199,155 @@ describe('Test formula data model', () => {});describe('updateFormulaData', () => {+ it('skips formula reconstruction for value updates', () => {+ const worksheet = get(IUniverInstanceService)+ .getUnit<Workbook>('test')+ ?.getSheetBySheetId('sheet1');+ const worksheetCellMatrix = worksheet!.getCellMatrix();+ worksheetCellMatrix.setValue(10, 10, { v: 1 });+ const getFormulaData = vi.spyOn(formulaDataModel, 'getFormulaData');+ const getSheetFormulaData = vi.spyOn(formulaDataModel, 'getSheetFormulaData');+ const scanWorksheet = vi.spyOn(worksheetCellMatrix, 'forValue');++ expect(formulaDataModel.updateFormulaData('test', 'sheet1', {+ 10: {+ 10: { v: 2 },+ },+ })).toEqual({});+ expect(getFormulaData).not.toHaveBeenCalled();+ expect(getSheetFormulaData).not.toHaveBeenCalled();+ expect(scanWorksheet).not.toHaveBeenCalled();+ });++ it('updates formulas without ids incrementally', () => {+ const worksheet = get(IUniverInstanceService)+ .getUnit<Workbook>('test')+ ?.getSheetBySheetId('sheet1');+ const worksheetCellMatrix = worksheet!.getCellMatrix();+ worksheetCellMatrix.setValue(10, 11, { f: '=A1' });+ const getFormulaData = vi.spyOn(formulaDataModel, 'getFormulaData');+ const getSheetFormulaData = vi.spyOn(formulaDataModel, 'getSheetFormulaData');+ const scanWorksheet = vi.spyOn(worksheetCellMatrix, 'forValue');++ expect(formulaDataModel.updateFormulaData('test', 'sheet1', {+ 10: {+ 10: { f: '=A1' },+ 11: { f: '=B1' },+ },+ })).toEqual({+ 10: {+ 10: { f: '=A1' },+ 11: { f: '=B1' },+ },+ });+ expect(getFormulaData).not.toHaveBeenCalled();+ expect(getSheetFormulaData).not.toHaveBeenCalled();+ expect(scanWorksheet).not.toHaveBeenCalled();+ });++ it('deletes formulas without ids incrementally', () => {+ const worksheet = get(IUniverInstanceService)+ .getUnit<Workbook>('test')+ ?.getSheetBySheetId('sheet1');+ worksheet?.getCellMatrix().setValue(10, 10, { f: '=A1' });+ const getFormulaData = vi.spyOn(formulaDataModel, 'getFormulaData');+ const getSheetFormulaData = vi.spyOn(formulaDataModel, 'getSheetFormulaData');++ expect(formulaDataModel.updateFormulaData('test', 'sheet1', {+ 10: {+ 10: { v: 1, f: null },+ },+ })).toEqual({+ 10: {+ 10: null,+ },+ });+ expect(getFormulaData).not.toHaveBeenCalled();+ expect(getSheetFormulaData).not.toHaveBeenCalled();+ });++ it('processes mixed updates containing shared formulas incrementally', () => {+ const getSheetFormulaData = vi.spyOn(formulaDataModel, 'getSheetFormulaData');++ const result = formulaDataModel.updateFormulaData('test', 'sheet1', {+ 0: {+ 0: { f: '=B1' },+ },+ 1: {+ 3: { v: null, f: null, si: null },+ },+ });++ expect(result[0]?.[0]).toEqual({ f: '=B1' });+ expect(result[1]?.[3]).toBeNull();+ expect(result[2]?.[3]).toEqual({ f: '=SUM(A3)', si: 'OSPtzm' });+ expect(getSheetFormulaData).not.toHaveBeenCalled();+ });++ it('updates both old and new shared formula groups', () => {+ expect(formulaDataModel.updateFormulaData('test', 'sheet1', {+ 1: {+ 3: { f: '=SUM(B2)', si: 'new-group' },+ },+ })).toEqual({+ 1: {+ 3: { f: '=SUM(B2)', si: 'new-group' },+ },+ 2: {+ 3: { f: '=SUM(A3)', si: 'OSPtzm' },+ },+ 3: {+ 3: { f: '=SUM(A3)', si: 'OSPtzm', x: 0, y: 1 },+ },+ });+ });++ it('detaches a shared formula anchor into a normal formula', () => {+ expect(formulaDataModel.updateFormulaData('test', 'sheet1', {+ 1: {+ 3: { f: '=SUM(B2)', si: null },+ },+ })).toEqual({+ 1: {+ 3: { f: '=SUM(B2)' },+ },+ 2: {+ 3: { f: '=SUM(A3)', si: 'OSPtzm' },+ },+ 3: {+ 3: { f: '=SUM(A3)', si: 'OSPtzm', x: 0, y: 1 },+ },+ });+ });++ it('creates a shared formula group when a follower precedes its anchor', () => {+ expect(formulaDataModel.updateFormulaData('test', 'sheet1', {+ 10: {+ 3: { si: 'new-group' },+ },+ 11: {+ 3: { f: '=SUM(A12)', si: 'new-group' },+ },+ })).toEqual({+ 10: {+ 3: { f: '=SUM(A12)', si: 'new-group', x: 0, y: -1 },+ },+ 11: {+ 3: { f: '=SUM(A12)', si: 'new-group' },+ },+ });+ });+it('delete formula with id', () => {const unitId = 'test';const sheetId = 'sheet1';+ const getFormulaData = vi.spyOn(formulaDataModel, 'getFormulaData');+ const getSheetFormulaData = vi.spyOn(formulaDataModel, 'getSheetFormulaData');+ const worksheetCellMatrix = get(IUniverInstanceService)+ .getUnit<Workbook>(unitId)+ ?.getSheetBySheetId(sheetId)+ ?.getCellMatrix();+ const scanWorksheet = vi.spyOn(worksheetCellMatrix!, 'forValue');const cellValue = {1: {3: {@@ -235,6 +381,9 @@ describe('Test formula data model', () => {const newFormulaData = formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);expect(newFormulaData).toStrictEqual(result);+ expect(getFormulaData).not.toHaveBeenCalled();+ expect(getSheetFormulaData).not.toHaveBeenCalled();+ expect(scanWorksheet).toHaveBeenCalledOnce();});it('delete formulas with ids and formulas with only ids', () => {diff --git a/packages/engine-formula/src/models/formula-data.model.ts b/packages/engine-formula/src/models/formula-data.model.tsindex eb84d5fbba..353274df96 100644--- a/packages/engine-formula/src/models/formula-data.model.ts+++ b/packages/engine-formula/src/models/formula-data.model.ts@@ -457,31 +457,81 @@ export class FormulaDataModel extends Disposable {updateFormulaData(unitId: string, sheetId: string, cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>) {const cellMatrix = new ObjectMatrix(cellValue);+ const newSheetFormulaDataMatrix = new ObjectMatrix<IFormulaDataItem | null>();+ const worksheetCellMatrix = this._univerInstanceService+ .getUnit<Workbook>(unitId)+ ?.getSheetBySheetId(sheetId)+ ?.getCellMatrix();+ const affectedFormulaIds = new Set<string>();- const formulaIdMap = this._getSheetFormulaIdMap(unitId, sheetId); // Connect the formula and ID+ cellMatrix.forValue((r, c, cell) => {+ const currentCell = worksheetCellMatrix?.getValue(r, c);+ const currentFormulaId = currentCell?.si;+ const formulaId = cell?.si;+ const hasCurrentFormulaId = isFormulaId(currentFormulaId);+ const hasFormulaId = isFormulaId(formulaId);+ const formulaString = cell?.f;- const deleteFormulaIdMap = new Map<string, string | IFormulaIdMap>();+ if (hasCurrentFormulaId) {+ affectedFormulaIds.add(String(currentFormulaId));+ }+ if (hasFormulaId) {+ affectedFormulaIds.add(String(formulaId));+ }- const formulaData = this.getFormulaData();+ if (!hasCurrentFormulaId && !hasFormulaId) {+ if (typeof formulaString === 'string' && isFormulaString(formulaString)) {+ newSheetFormulaDataMatrix.setValue(r, c, { f: formulaString });+ } else if (isFormulaString(currentCell?.f)) {+ newSheetFormulaDataMatrix.setValue(r, c, null);+ }+ }+ });- if (formulaData[unitId] == null) {- formulaData[unitId] = {};+ if (affectedFormulaIds.size === 0) {+ return newSheetFormulaDataMatrix.getMatrix();}- const workbookFormulaData = formulaData[unitId]!;+ const formulaIdMap: { [formulaId: string]: IFormulaIdMap } = {};+ const sharedFormulaCellMatrix = new ObjectMatrix<Nullable<ICellData>>();+ worksheetCellMatrix?.forValue((r, c, cell) => {+ const formulaId = cell?.si;+ if (!isFormulaId(formulaId) || !affectedFormulaIds.has(String(formulaId))) {+ return;+ }- if (workbookFormulaData[sheetId] == null) {- workbookFormulaData[sheetId] = {};- }+ sharedFormulaCellMatrix.setValue(r, c, cell);- const sheetFormulaDataMatrix = new ObjectMatrix<Nullable<IFormulaDataItem>>(workbookFormulaData[sheetId] || {});- const newSheetFormulaDataMatrix = new ObjectMatrix<IFormulaDataItem | null>();+ if (typeof cell?.f === 'string' && isFormulaString(cell.f)) {+ formulaIdMap[String(formulaId)] = { f: cell.f, r, c };+ }+ });++ const formulaData: IFormulaData = {};+ initSheetFormulaData(formulaData, unitId, sheetId, sharedFormulaCellMatrix);+ const deleteFormulaIdMap = new Map<string, string | IFormulaIdMap>();+ const sheetFormulaDataMatrix = new ObjectMatrix<Nullable<IFormulaDataItem>>(formulaData[unitId]?.[sheetId] ?? {});cellMatrix.forValue((r, c, cell) => {+ const currentFormulaId = worksheetCellMatrix?.getValue(r, c)?.si;+ if (!isFormulaId(currentFormulaId) && !isFormulaId(cell?.si)) {+ return;+ }+updateFormulaDataByCellValue(sheetFormulaDataMatrix, newSheetFormulaDataMatrix, formulaIdMap, deleteFormulaIdMap, r, c, cell);});- // Convert the formula ID to formula string+ this._rebindSharedFormulaData(sheetFormulaDataMatrix, newSheetFormulaDataMatrix, formulaIdMap, deleteFormulaIdMap);++ return newSheetFormulaDataMatrix.getMatrix();+ }++ private _rebindSharedFormulaData(+ sheetFormulaDataMatrix: ObjectMatrix<Nullable<IFormulaDataItem>>,+ newSheetFormulaDataMatrix: ObjectMatrix<IFormulaDataItem | null>,+ formulaIdMap: { [formulaId: string]: IFormulaIdMap },+ deleteFormulaIdMap: Map<string, string | IFormulaIdMap>+ ) {sheetFormulaDataMatrix.forValue((r, c, cell) => {const formulaString = cell?.f || '';const formulaId = cell?.si || '';@@ -515,8 +565,6 @@ export class FormulaDataModel extends Disposable {}}});-- return newSheetFormulaDataMatrix.getMatrix();}updateArrayFormulaRange(@@ -729,35 +777,6 @@ export class FormulaDataModel extends Disposable {return dirtyRanges;}- private _getSheetFormulaIdMap(unitId: string, sheetId: string) {- const formulaIdMap: Nullable<{ [formulaId: string]: IFormulaIdMap }> = {}; // Connect the formula and ID-- const workbook = this._univerInstanceService.getUnit<Workbook>(unitId);- if (workbook == null) {- return formulaIdMap;- }-- const worksheet = workbook.getSheetBySheetId(sheetId);- if (worksheet == null) {- return formulaIdMap;- }-- const cellMatrix = worksheet.getCellMatrix();- cellMatrix.forValue((r, c, cell) => {- if (cell == null) {- return true;- }-- const { f, si } = cell;-- if (isFormulaString(f) && isFormulaId(si)) {- formulaIdMap[si as string] = { f: f as string, r, c };- }- });-- return formulaIdMap;- }-private _initSheetArrayFormulaData(unitId: string, sheetId: string, cellMatrix: ObjectMatrix<Nullable<ICellData>>) {let arrayFormulaRangeMatrix: Nullable<ObjectMatrix<IRange>>;let arrayFormulaCellDataMatrix: Nullable<ObjectMatrix<Nullable<ICellData>>>;diff --git a/packages/engine-formula/src/services/__tests__/formula-calculation-trigger.service.spec.ts b/packages/engine-formula/src/services/__tests__/formula-calculation-trigger.service.spec.tsindex 153721cb33..087ebb7bcb 100644--- a/packages/engine-formula/src/services/__tests__/formula-calculation-trigger.service.spec.ts+++ b/packages/engine-formula/src/services/__tests__/formula-calculation-trigger.service.spec.ts@@ -20,6 +20,7 @@ import {SetFormulaCalculationNotificationMutation,SetFormulaCalculationStartMutation,SetFormulaCalculationStopMutation,+ SetTriggerFormulaCalculationStartMutation,} from '../../commands/mutations/set-formula-calculation.mutation';import { FormulaCalculationTriggerService } from '../formula-calculation-trigger.service';import { FormulaExecutedStateType } from '../runtime.service';@@ -131,4 +132,27 @@ describe('FormulaCalculationTriggerService', () => {expect(testBed.executed).toEqual([]);testBed.service.dispose();});++ it('only runs empty dirty data for an explicit trigger', async () => {+ const testBed = createTestBed();+ testBed.conversions.set('empty-dirty', {+ commandId: 'empty-dirty',+ getDirtyData: () => ({}),+ });+ testBed.conversions.set(SetTriggerFormulaCalculationStartMutation.id, {+ commandId: SetTriggerFormulaCalculationStartMutation.id,+ getDirtyData: () => ({}),+ });++ testBed.emit({ id: 'empty-dirty' } as ICommandInfo);+ await vi.advanceTimersByTimeAsync(100);+ expect(testBed.executed).toEqual([]);++ testBed.emit({ id: SetTriggerFormulaCalculationStartMutation.id } as ICommandInfo);+ await vi.advanceTimersByTimeAsync(100);++ expect(testBed.executed).toHaveLength(1);+ expect(testBed.executed[0]).toMatchObject({ id: SetFormulaCalculationStartMutation.id });+ testBed.service.dispose();+ });});diff --git a/packages/engine-formula/src/services/formula-calculation-trigger.service.ts b/packages/engine-formula/src/services/formula-calculation-trigger.service.tsindex 2b4ecc0989..744588d6da 100644--- a/packages/engine-formula/src/services/formula-calculation-trigger.service.ts+++ b/packages/engine-formula/src/services/formula-calculation-trigger.service.ts@@ -23,6 +23,7 @@ import {SetFormulaCalculationNotificationMutation,SetFormulaCalculationStartMutation,SetFormulaCalculationStopMutation,+ SetTriggerFormulaCalculationStartMutation,} from '../commands/mutations/set-formula-calculation.mutation';import { IActiveDirtyManagerService } from './active-dirty-manager.service';import { FormulaExecutedStateType } from './runtime.service';@@ -86,10 +87,11 @@ export class FormulaCalculationTriggerService extends Disposable {}private _flush(): void {- const dirtyData = this._generateDirty(this._waitingCommandQueue);+ const commands = this._waitingCommandQueue;+ const dirtyData = this._generateDirty(commands);this._waitingCommandQueue = [];this._timer = undefined;- if (!hasDirtyData(dirtyData)) {+ if (!hasDirtyData(dirtyData) && !commands.some(({ id }) => id === SetTriggerFormulaCalculationStartMutation.id)) {return;}