Skip to content

Commit

Permalink
perf(formula): dependency maintain
Browse files Browse the repository at this point in the history
  • Loading branch information
DR-Univer committed Mar 19, 2024
1 parent c26eed1 commit b314989
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import {
SetFeatureCalculationMutation,
} from '../commands/mutations/set-feature-calculation.mutation';
import { IFeatureCalculationManagerService } from '../services/feature-calculation-manager.service';
import { IDependencyManagerService } from '../services/dependency-manager.service';

@OnLifecycle(LifecycleStages.Ready, SetFeatureCalculationController)
export class SetFeatureCalculationController extends Disposable {
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IFeatureCalculationManagerService
private readonly _featureCalculationManagerService: IFeatureCalculationManagerService
private readonly _featureCalculationManagerService: IFeatureCalculationManagerService,
@IDependencyManagerService private readonly _dependencyManagerService: IDependencyManagerService
) {
super();

Expand All @@ -56,6 +58,7 @@ export class SetFeatureCalculationController extends Disposable {
return;
}
const { featureId } = params;
this._dependencyManagerService.removeFeatureFormulaDependency(featureId);
this._featureCalculationManagerService.remove(featureId);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import {
type IOtherFormulaManagerSearchParam,
IOtherFormulaManagerService,
} from '../services/other-formula-manager.service';
import { IDependencyManagerService } from '../services/dependency-manager.service';

@OnLifecycle(LifecycleStages.Ready, SetOtherFormulaController)
export class SetOtherFormulaController extends Disposable {
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IOtherFormulaManagerService private readonly _otherFormulaManagerService: IOtherFormulaManagerService
@IOtherFormulaManagerService private readonly _otherFormulaManagerService: IOtherFormulaManagerService,
@IDependencyManagerService private readonly _dependencyManagerService: IDependencyManagerService
) {
super();

Expand All @@ -55,6 +57,7 @@ export class SetOtherFormulaController extends Disposable {
return;
}

this._dependencyManagerService.removeOtherFormulaDependency(params.unitId, params.subUnitId, params.formulaId);
this._otherFormulaManagerService.remove(params);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export class FormulaDependencyTree extends Disposable {
private _state = FDtreeStateType.DEFAULT;

override dispose(): void {
super.dispose();

this.children.forEach((tree) => {
tree.dispose();
});
Expand Down Expand Up @@ -278,6 +280,10 @@ export class FormulaDependencyTreeCache extends Disposable {
return this._cacheItems.size;
}

get length() {
return this._cacheItems.size;
}

add(unitRangeWithToken: IUnitRangeWithToken, tree: FormulaDependencyTree) {
const { token } = unitRangeWithToken;
if (!this._cacheItems.has(token)) {
Expand Down
64 changes: 44 additions & 20 deletions packages/engine-formula/src/engine/dependency/formula-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { Interpreter } from '../interpreter/interpreter';
import type { BaseReferenceObject } from '../reference-object/base-reference-object';
import type { PreCalculateNodeType } from '../utils/node-type';
import { serializeRangeToRefString } from '../utils/reference';
import { IDependencyManagerService } from '../../services/dependency-manager.service';
import type { IUnitRangeWithToken } from './dependency-tree';
import { FormulaDependencyTree, FormulaDependencyTreeCache } from './dependency-tree';

Expand All @@ -59,7 +60,8 @@ export class FormulaDependencyGenerator extends Disposable {
private readonly _featureCalculationManagerService: IFeatureCalculationManagerService,
@Inject(Interpreter) private readonly _interpreter: Interpreter,
@Inject(AstTreeBuilder) private readonly _astTreeBuilder: AstTreeBuilder,
@Inject(Lexer) private readonly _lexer: Lexer
@Inject(Lexer) private readonly _lexer: Lexer,
@IDependencyManagerService private readonly _dependencyManagerService: IDependencyManagerService
) {
super();
}
Expand Down Expand Up @@ -173,6 +175,11 @@ export class FormulaDependencyGenerator extends Disposable {
return true;
}
const { f: formulaString, x, y } = formulaDataItem;

if (this._dependencyManagerService.hasFormulaDependency(unitId, sheetId, row, column)) {
return true;
}

const node = this._generateAstNode(formulaString, x, y);

const FDtree = new FormulaDependencyTree();
Expand All @@ -189,6 +196,8 @@ export class FormulaDependencyGenerator extends Disposable {
FDtree.rowCount = sheetItem.rowCount;
FDtree.columnCount = sheetItem.columnCount;

this._dependencyManagerService.addFormulaDependency(unitId, sheetId, row, column, FDtree);

treeList.push(FDtree);
});
}
Expand Down Expand Up @@ -216,6 +225,10 @@ export class FormulaDependencyGenerator extends Disposable {
const subFormulaDataKeys = Object.keys(subFormulaData);

for (const subFormulaDataId of subFormulaDataKeys) {
if (this._dependencyManagerService.hasOtherFormulaDependency(unitId, subUnitId, subFormulaDataId)) {
continue;
}

const formulaDataItem = subFormulaData[subFormulaDataId];

const { f: formulaString } = formulaDataItem;
Expand All @@ -231,6 +244,8 @@ export class FormulaDependencyGenerator extends Disposable {

FDtree.formulaId = subFormulaDataId;

this._dependencyManagerService.addOtherFormulaDependency(unitId, subUnitId, subFormulaDataId, FDtree);

treeList.push(FDtree);
}
}
Expand All @@ -243,6 +258,11 @@ export class FormulaDependencyGenerator extends Disposable {
*/
this._featureCalculationManagerService.getReferenceExecutorMap().forEach((params, featureId) => {
const { unitId, subUnitId, dependencyRanges, getDirtyData } = params;

if (this._dependencyManagerService.hasFeatureFormulaDependency(unitId, subUnitId, featureId)) {
return true;
}

const FDtree = new FormulaDependencyTree();

FDtree.unitId = unitId;
Expand All @@ -259,6 +279,8 @@ export class FormulaDependencyGenerator extends Disposable {
};
});

this._dependencyManagerService.addFeatureFormulaDependency(featureId, FDtree);

treeList.push(FDtree);
});

Expand Down Expand Up @@ -485,29 +507,31 @@ export class FormulaDependencyGenerator extends Disposable {
const existTree = new Set<FormulaDependencyTree>();
const forceCalculate = this._currentConfigService.isForceCalculate();

let dependencyAlgorithm = true;
let allTree: FormulaDependencyTree[] = [];

if (dependencyTreeCache.size() > treeList.length) {
dependencyAlgorithm = false;
allTree = this._dependencyManagerService.buildDependencyTree(treeList);
} else {
allTree = this._dependencyManagerService.buildDependencyTree(dependencyTreeCache);
}

for (let i = 0, len = treeList.length; i < len; i++) {
const tree = treeList[i];

if (dependencyAlgorithm) {
dependencyTreeCache.dependency(tree);
} else {
for (let m = 0, mLen = treeList.length; m < mLen; m++) {
const treeMatch = treeList[m];
if (tree === treeMatch) {
continue;
}

if (tree.dependency(treeMatch)) {
tree.pushChildren(treeMatch);
}
}
}
for (let i = 0, len = allTree.length; i < len; i++) {
const tree = allTree[i];

// if (dependencyAlgorithm) {
// dependencyTreeCache.dependency(tree);
// } else {
// for (let m = 0, mLen = treeList.length; m < mLen; m++) {
// const treeMatch = treeList[m];
// if (tree === treeMatch) {
// continue;
// }

// if (tree.dependency(treeMatch)) {
// tree.pushChildren(treeMatch);
// }
// }
// }

/**
* forceCalculate: Mandatory calculation, adding all formulas to dependencies
Expand Down
2 changes: 2 additions & 0 deletions packages/engine-formula/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { FunctionService, IFunctionService } from './services/function.service';
import { IOtherFormulaManagerService, OtherFormulaManagerService } from './services/other-formula-manager.service';
import { FormulaRuntimeService, IFormulaRuntimeService } from './services/runtime.service';
import { ISuperTableService, SuperTableService } from './services/super-table.service';
import { DependencyManagerService, IDependencyManagerService } from './services/dependency-manager.service';

const PLUGIN_NAME = 'base-formula-engine';

Expand Down Expand Up @@ -107,6 +108,7 @@ export class UniverFormulaEnginePlugin extends Plugin {
[ISuperTableService, { useClass: SuperTableService }],
[IFormulaCurrentConfigService, { useClass: FormulaCurrentConfigService }],
[IFormulaRuntimeService, { useClass: FormulaRuntimeService }],
[IDependencyManagerService, { useClass: DependencyManagerService }],

//Controller
[CalculateController],
Expand Down

0 comments on commit b314989

Please sign in to comment.