Skip to content

Commit

Permalink
fixed recursion into visited atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
canonic-epicure committed Jul 21, 2020
1 parent 61bc29e commit 813e283
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 65 deletions.
68 changes: 44 additions & 24 deletions src/chrono2/atom/Atom.ts
Expand Up @@ -216,7 +216,7 @@ export class Atom extends Owner implements Identifiable, Uniqable {

if (!this.equality(newValue, oldValue)) this.propagateDeepStaleOutsideOfGraph()

this.immutable = quark
this.immutable = quark
}


Expand All @@ -239,24 +239,24 @@ export class Atom extends Owner implements Identifiable, Uniqable {
if (atom.graph && !atom.lazy) {
atom.graph.addPossiblyStaleStrictAtomToTransaction(atom)
}
}

quark.forEachOutgoing((outgoing, atom) => {
if (atom.state === AtomState.UpToDate) toVisit.push(atom.immutable)
})
quark.forEachOutgoing((outgoing, atom) => {
if (atom.state === AtomState.UpToDate) toVisit.push(atom.immutable)
})
}
}
}


propagateDeepStaleOutsideOfGraph () {
const toVisit : Quark[] = []
const toVisit1 : Quark[] = []

const graph = this.graph
const graph = this.graph

this.immutable.forEachOutgoing((outgoing, atom) => {
if (atom.graph !== graph) {
// only go deeper if state was UpToDate
if (atom.state === AtomState.UpToDate) toVisit.push(atom.immutable)
if (atom.state === AtomState.UpToDate) toVisit1.push(atom.immutable)

// but reset to stale anyway
atom.state = AtomState.Stale
Expand All @@ -268,8 +268,18 @@ export class Atom extends Owner implements Identifiable, Uniqable {
// so this condition is always false
// if (!this.immutable.frozen) this.immutable.clearOutgoing()

while (toVisit.length) {
const quark = toVisit.pop()
const toVisit2 : Quark[] = []

for (let i = 0; i < toVisit1.length; i++) {
const quark = toVisit1[ i ]

quark.forEachOutgoing((outgoing, atom) => {
if (atom.state === AtomState.UpToDate) toVisit2.push(atom.immutable)
})
}

while (toVisit2.length) {
const quark = toVisit2.pop()

const atom = quark.owner

Expand All @@ -279,25 +289,25 @@ export class Atom extends Owner implements Identifiable, Uniqable {
if (atom.graph && !atom.lazy) {
atom.graph.addPossiblyStaleStrictAtomToTransaction(atom)
}
}

quark.forEachOutgoing((outgoing, atom) => {
if (atom.graph !== graph) {
if (atom.state === AtomState.UpToDate) toVisit.push(atom.immutable)
}
})
quark.forEachOutgoing((outgoing, atom) => {
if (atom.graph !== graph) {
if (atom.state === AtomState.UpToDate) toVisit2.push(atom.immutable)
}
})
}
}
}


// immediate outgoings should become stale, further outgoings - possibly stale
propagateStaleDeep () {
const toVisit : Quark[] = []
const toVisit1 : Quark[] = []

this.immutable.forEachOutgoing((outgoing, atom) => {
// only go deeper if state was UpToDate
if (atom.state === AtomState.UpToDate) {
toVisit.push(atom.immutable)
toVisit1.push(atom.immutable)

if (atom.graph && !atom.lazy) {
atom.graph.addPossiblyStaleStrictAtomToTransaction(atom)
Expand All @@ -310,8 +320,18 @@ export class Atom extends Owner implements Identifiable, Uniqable {

if (!this.immutable.frozen) this.immutable.clearOutgoing()

while (toVisit.length) {
const quark = toVisit.pop()
const toVisit2 : Quark[] = []

for (let i = 0; i < toVisit1.length; i++) {
const quark = toVisit1[ i ]

quark.forEachOutgoing((outgoing, atom) => {
if (atom.state === AtomState.UpToDate) toVisit2.push(atom.immutable)
})
}

while (toVisit2.length) {
const quark = toVisit2.pop()

const atom = quark.owner

Expand All @@ -321,11 +341,11 @@ export class Atom extends Owner implements Identifiable, Uniqable {
if (atom.graph && !atom.lazy) {
atom.graph.addPossiblyStaleStrictAtomToTransaction(atom)
}
}

quark.forEachOutgoing((outgoing, atom) => {
if (atom.state === AtomState.UpToDate) toVisit.push(atom.immutable)
})
quark.forEachOutgoing((outgoing, atom) => {
if (atom.state === AtomState.UpToDate) toVisit2.push(atom.immutable)
})
}
}
}

Expand Down
46 changes: 10 additions & 36 deletions src/chrono2/atom/Quark.ts
Expand Up @@ -104,12 +104,10 @@ export class Quark extends Node implements Immutable {
// IMPORTANT LIMITATION : can not nest calls to `forEachOutgoing` (call `forEachOutgoing` in the `func` argument)
// this messes up internal "uniqables" state
forEachOutgoing (func : (quark : Quark, resolvedAtom : Atom) => any) {
let quark : this = this

const uniqable = getUniqable()
const uniqable2 = getUniqable()
const uniqable = getUniqable()
const graph = this.owner.graph

const graph = this.owner.graph
let quark : this = this

do {
const outgoing = quark.$outgoing
Expand All @@ -118,35 +116,16 @@ export class Quark extends Node implements Immutable {
if (outgoing) {

for (let i = outgoing.length - 1; i >= 0; i--) {
const outgoingRevision = outgoingRev[ i ]
const outgoingQuark = outgoing[ i ] as Quark
const outgoingHistory = outgoingQuark.owner

const identity = outgoingHistory.identity
const outgoingQuark = outgoing[ i ]
const outgoingOwner = outgoingQuark.owner
const identity = outgoingOwner.identity

const delta = uniqable2 - identity.uniqable

if (delta > 1) {
const outgoingOwner = !outgoingHistory.graph || outgoingHistory.graph === graph ? outgoingHistory : graph.checkout(outgoingHistory)

if (outgoingOwner.immutable.revision === outgoingRevision) {
identity.uniqable = uniqable2
identity.uniqableBox2 = outgoingOwner
} else
identity.uniqable = uniqable
}
}

for (let i = 0; i < outgoing.length; i++) {
const outgoingQuark = outgoing[ i ] as Quark
const outgoingHistory = outgoingQuark.owner

const identity = outgoingHistory.identity
if (identity.uniqable !== uniqable) {
identity.uniqable = uniqable

if (identity.uniqable === uniqable2) {
identity.uniqable = uniqable
const outgoingAtom = !outgoingOwner.graph || outgoingOwner.graph === graph ? outgoingOwner : graph.checkout(outgoingOwner)

func(outgoingQuark, identity.uniqableBox2)
if (outgoingAtom.immutable.revision === outgoingRev[ i ]) func(outgoingQuark, outgoingAtom)
}
}
}
Expand Down Expand Up @@ -263,11 +242,6 @@ export class Quark extends Node implements Immutable {

this.previous = zero

// need to keep the order of edges addition, otherwise
// performance degrades
collapsedOutgoing.reverse()
collapsedOutgoingRev.reverse()

this.$outgoing = collapsedOutgoing
this.$outgoingRev = collapsedOutgoingRev
}
Expand Down
6 changes: 1 addition & 5 deletions src/chrono2/data/CalculableBox.ts
Expand Up @@ -241,11 +241,7 @@ export class CalculableBox<V> extends Box<V> {

this.proposedValue = value

if (this.state === AtomState.UpToDate) {
this.state = AtomState.PossiblyStale

this.propagatePossiblyStale()
}
this.propagatePossiblyStale()
}


Expand Down

0 comments on commit 813e283

Please sign in to comment.