Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DependencyGraph.getArrayVerticesRelatedToRanges method to be more memory-effective #1333

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Fixed an issue where operation on ranges with incompatible sizes resulted in a runtime exception. [#1267](https://github.com/handsontable/hyperformula/issues/1267)
- Fixed an issue where `simpleCellAddressFromString` method was crashing when called with a non-ASCII character in an unquoted sheet name. [#1312](https://github.com/handsontable/hyperformula/issues/1312)
- Fixed a bug where adding a row to the very large spreadsheet resulted in `Maximum call stack size exceeded` error. [#1332](https://github.com/handsontable/hyperformula/issues/1332)
- Fixed a typo in the JSDoc comment of the `HyperFormula` class. [#1323](https://github.com/handsontable/hyperformula/issues/1323)
- Fixed a bug where function SUBSTITUTE did not work correctly with regexp special characters. [#1289](https://github.com/handsontable/hyperformula/issues/1289)

Expand Down
175 changes: 80 additions & 95 deletions src/DependencyGraph/DependencyGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,6 @@ export class DependencyGraph {
return this.getAndClearContentChanges()
}

public ensureThatVertexIsNonArrayCellVertex(vertex: Maybe<CellVertex>) {
if (vertex instanceof ArrayVertex) {
throw new Error('Illegal operation')
}
}

public clearDirtyVertices() {
this.graph.clearDirtyNodes()
}
Expand Down Expand Up @@ -238,22 +232,6 @@ export class DependencyGraph {
this.exchangeGraphNode(vertexFrom, vertexTo)
}

public correctInfiniteRangesDependency(address: SimpleCellAddress) {
const relevantInfiniteRanges = (this.graph.getInfiniteRanges())
.filter(({ node }) => (node as RangeVertex).range.addressInRange(address))

if (relevantInfiniteRanges.length <= 0) {
return
}

const { vertex, id: maybeVertexId } = this.fetchCellOrCreateEmpty(address)
const vertexId = maybeVertexId ?? this.graph.getNodeId(vertex)!

relevantInfiniteRanges.forEach(({ id }) => {
this.graph.addEdge(vertexId, id)
})
}

public fetchCellOrCreateEmpty(address: SimpleCellAddress): { vertex: CellVertex, id: Maybe<number> } {
const existingVertex = this.addressMapping.getCell(address)

Expand Down Expand Up @@ -434,12 +412,6 @@ export class DependencyGraph {
return {affectedArrays, contentChanges: this.getAndClearContentChanges()}
}

public ensureNoArrayInRange(range: AbsoluteCellRange) {
if (this.arrayMapping.isFormulaArrayInRange(range)) {
throw Error('It is not possible to move / replace cells with array')
}
}

public isThereSpaceForArray(arrayVertex: ArrayVertex): boolean {
const range = arrayVertex.getRangeOrUndef()
if (range === undefined) {
Expand Down Expand Up @@ -559,10 +531,6 @@ export class DependencyGraph {
yield* this.addressMapping.entriesFromColumnsSpan(columnsSpan)
}

public existsVertex(address: SimpleCellAddress): boolean {
return this.addressMapping.has(address)
}

public fetchCell(address: SimpleCellAddress): CellVertex {
return this.addressMapping.fetchCell(address)
}
Expand Down Expand Up @@ -607,10 +575,6 @@ export class DependencyGraph {
return this.arrayMapping.getArray(range)
}

public setArray(range: AbsoluteCellRange, vertex: ArrayVertex): void {
this.arrayMapping.setArray(range, vertex)
}

public getRange(start: SimpleCellAddress, end: SimpleCellAddress): Maybe<RangeVertex> {
return this.rangeMapping.getRange(start, end)
}
Expand All @@ -635,17 +599,6 @@ export class DependencyGraph {
}
}

public getArrayVerticesRelatedToRanges(ranges: RangeVertex[]): Set<ArrayVertex> {
const arrayVertices = ranges.map(range => {
if (this.graph.hasNode(range)) {
return Array.from(this.graph.adjacentNodes(range)).filter(node => node instanceof ArrayVertex)
} else {
return []
}
}) as ArrayVertex[][]
return new Set(...arrayVertices)
}

public* rawValuesFromRange(range: AbsoluteCellRange): IterableIterator<[RawScalarValue, SimpleCellAddress]> {
for (const address of range.addresses(this)) {
const value = this.getScalarValue(address)
Expand All @@ -655,31 +608,6 @@ export class DependencyGraph {
}
}

public* entriesFromRange(range: AbsoluteCellRange): IterableIterator<[SimpleCellAddress, Maybe<CellVertex>]> {
for (const address of range.addresses(this)) {
yield [address, this.getCell(address)]
}
}

public exchangeGraphNode(oldNode: Vertex, newNode: Vertex) {
this.graph.addNodeAndReturnId(newNode)
const adjNodesStored = this.graph.adjacentNodes(oldNode)
this.removeVertex(oldNode)
adjNodesStored.forEach((adjacentNode) => {
if (this.graph.hasNode(adjacentNode)) {
this.graph.addEdge(newNode, adjacentNode)
}
})
}

public exchangeOrAddGraphNode(oldNode: Maybe<Vertex>, newNode: Vertex) {
if (oldNode) {
this.exchangeGraphNode(oldNode, newNode)
} else {
this.graph.addNodeAndReturnId(newNode)
}
}

public dependencyQueryAddresses: (vertex: Vertex) => (SimpleCellAddress | SimpleCellRange)[] = (vertex: Vertex) => {
if (vertex instanceof RangeVertex) {
return this.rangeDependencyQuery(vertex).map(([address, _]) => address)
Expand All @@ -702,29 +630,6 @@ export class DependencyGraph {
}
}

public dependencyQueryVertices: DependencyQuery<Vertex> = (vertex: Vertex) => {
if (vertex instanceof RangeVertex) {
return this.rangeDependencyQuery(vertex)
} else {
const dependenciesResult = this.formulaDependencyQuery(vertex)
if (dependenciesResult !== undefined) {
const [address, dependencies] = dependenciesResult
return dependencies.map((dependency: CellDependency) => {
if (dependency instanceof AbsoluteCellRange) {
return [dependency.start, this.rangeMapping.fetchRange(dependency.start, dependency.end)]
} else if (dependency instanceof NamedExpressionDependency) {
const namedExpression = this.namedExpressions.namedExpressionOrPlaceholder(dependency.name, address.sheet)
return [namedExpression.address, this.addressMapping.fetchCell(namedExpression.address)]
} else {
return [dependency, this.addressMapping.fetchCell(dependency)]
}
})
} else {
return []
}
}
}

public computeListOfValuesInRange(range: AbsoluteCellRange): InternalScalarValue[] {
const values: InternalScalarValue[] = []
for (const cellFromRange of range.addresses(this)) {
Expand Down Expand Up @@ -783,6 +688,86 @@ export class DependencyGraph {
return ret
}

private exchangeGraphNode(oldNode: Vertex, newNode: Vertex) {
this.graph.addNodeAndReturnId(newNode)
const adjNodesStored = this.graph.adjacentNodes(oldNode)
this.removeVertex(oldNode)
adjNodesStored.forEach((adjacentNode) => {
if (this.graph.hasNode(adjacentNode)) {
this.graph.addEdge(newNode, adjacentNode)
}
})
}

private setArray(range: AbsoluteCellRange, vertex: ArrayVertex): void {
this.arrayMapping.setArray(range, vertex)
}

private correctInfiniteRangesDependency(address: SimpleCellAddress) {
const relevantInfiniteRanges = (this.graph.getInfiniteRanges())
.filter(({ node }) => (node as RangeVertex).range.addressInRange(address))

if (relevantInfiniteRanges.length <= 0) {
return
}

const { vertex, id: maybeVertexId } = this.fetchCellOrCreateEmpty(address)
const vertexId = maybeVertexId ?? this.graph.getNodeId(vertex)!

relevantInfiniteRanges.forEach(({ id }) => {
this.graph.addEdge(vertexId, id)
})
}

private exchangeOrAddGraphNode(oldNode: Maybe<Vertex>, newNode: Vertex) {
if (oldNode) {
this.exchangeGraphNode(oldNode, newNode)
} else {
this.graph.addNodeAndReturnId(newNode)
}
}

private dependencyQueryVertices: DependencyQuery<Vertex> = (vertex: Vertex) => {
if (vertex instanceof RangeVertex) {
return this.rangeDependencyQuery(vertex)
} else {
const dependenciesResult = this.formulaDependencyQuery(vertex)
if (dependenciesResult !== undefined) {
const [address, dependencies] = dependenciesResult
return dependencies.map((dependency: CellDependency) => {
if (dependency instanceof AbsoluteCellRange) {
return [dependency.start, this.rangeMapping.fetchRange(dependency.start, dependency.end)]
} else if (dependency instanceof NamedExpressionDependency) {
const namedExpression = this.namedExpressions.namedExpressionOrPlaceholder(dependency.name, address.sheet)
return [namedExpression.address, this.addressMapping.fetchCell(namedExpression.address)]
} else {
return [dependency, this.addressMapping.fetchCell(dependency)]
}
})
} else {
return []
}
}
}

private getArrayVerticesRelatedToRanges(ranges: RangeVertex[]): Set<ArrayVertex> {
const arrayVertices = new Set<ArrayVertex>()

ranges.forEach(range => {
if (!this.graph.hasNode(range)) {
return
}

this.graph.adjacentNodes(range).forEach(adjacentVertex => {
if (adjacentVertex instanceof ArrayVertex) {
arrayVertices.add(adjacentVertex)
}
})
})

return arrayVertices
}

private correctInfiniteRangesDependenciesByRangeVertex(vertex: RangeVertex) {
this.graph.getInfiniteRanges()
.forEach(({ id: infiniteRangeVertexId, node: infiniteRangeVertex }) => {
Expand Down
Loading