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

crdx: Improve performance of calculateConcurrency #124

Closed
Closed
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
44 changes: 30 additions & 14 deletions packages/crdx/src/graph/concurrency.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { memoize } from '@localfirst/shared'
import { type Hash } from 'util/index.js'
import { getHashes } from './getHashes.js'
import { getLink } from './getLink.js'
import { isPredecessorHash } from './isPredecessor.js'
import { isSuccessorHash } from './isSuccessor.js'
import { getPredecessorHashes } from './getPredecessors.js'
import { getChildrenHashes } from './children.js'
import type { Action, Graph, Link } from './types.js'

/** Returns all links that are concurrent with the given link. */
Expand All @@ -30,24 +29,41 @@ export const getConcurrentHashes = (graph: Graph<any, any>, hash: Hash): Hash[]
* ```
*/
export const calculateConcurrency = memoize(<A extends Action, C>(graph: Graph<A, C>) => {
const toVisit = [graph.root]
const visited: Set<Hash> = new Set()
const concurrencyLookup: Record<Hash, Hash[]> = {}

// for each link, find all links that are concurrent with it
for (const _ in graph.links) {
const hash = _ as Hash
concurrencyLookup[hash] = getHashes(graph)
.filter(b => isConcurrent(graph, hash, b))
.sort()
while (toVisit.length > 0) {
const current = toVisit.shift() as Hash

if (visited.has(current)) {
continue
}

const predecessors = new Set(getPredecessorHashes(graph, current))
const concurrent: Hash[] = []

for (const v of Array.from(visited)) {
if (!predecessors.has(v) && !getPredecessorHashes(graph, v).includes(current)) {
concurrent.push(v)
}
}

concurrencyLookup[current] = concurrent

for (const c of concurrent) {
concurrencyLookup[c].push(current)
}

for (const c of getChildrenHashes(graph, current)) {
toVisit.push(c)
}
visited.add(current)
}

return concurrencyLookup
})

export const isConcurrent = <A extends Action, C>(graph: Graph<A, C>, a: Hash, b: Hash) =>
a !== b && // a link isn't concurrent with itself
!isPredecessorHash(graph, a, b) && // a link isn't concurrent with any of its predecessors
!isSuccessorHash(graph, a, b) // a link isn't concurrent with any of its successors

export const getConcurrentBubbles = <A extends Action, C>(graph: Graph<A, C>): Hash[][] => {
const seen: Record<Hash, boolean> = {}

Expand Down
22 changes: 0 additions & 22 deletions packages/crdx/src/graph/getSuccessors.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/crdx/src/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ export * from './getParents.js'
export * from './getPredecessors.js'
export * from './getRoot.js'
export * from './getSequence.js'
export * from './getSuccessors.js'
export * from './headsAreEqual.js'
export * from './isPredecessor.js'
export * from './isSuccessor.js'
export * from './merge.js'
export * from './redactGraph.js'
export * from './serialize.js'
Expand Down
21 changes: 0 additions & 21 deletions packages/crdx/src/graph/isSuccessor.ts

This file was deleted.

50 changes: 0 additions & 50 deletions packages/crdx/src/graph/test/getSuccessors.test.ts

This file was deleted.

Loading