Skip to content

Commit

Permalink
nearing handles as soon as they are no more necessary
Browse files Browse the repository at this point in the history
hopefully a perf/memory improvement :-)
  • Loading branch information
bergus committed Jul 5, 2016
1 parent 1f6b624 commit 4d3e8a1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Handle.js
Expand Up @@ -3,7 +3,7 @@ export class Handle {
this.ref = ref // a ShareHandle, known Promise or unresolved Future
}
_getRef () {
return this.ref._getRef()
return this.ref
}
// the ref will be lost, e.g. when an action is used multiple times
_isReused () {
Expand Down
23 changes: 21 additions & 2 deletions src/Promise.js
Expand Up @@ -41,6 +41,11 @@ class Core {
// assert: isNever(this) || !isPending(this)
return this
}

_getHandle () {
// assert: this is a Fulfilled, Rejected or Never
return this
}
}

// data Promise e a where
Expand Down Expand Up @@ -114,10 +119,19 @@ export class Future extends Core {

// near :: Promise e a -> Promise e a
near () {
if (this.handle === void 0) {
let h = this.handle
if (h === void 0) {
return this
}
return this.handle._getRef()
let ref = h._getRef()
if (ref !== this && ref !== h) {
do {
h = ref
ref = h._getRef()
} while (ref !== h)
this.handle = ref._getHandle()
}
return ref
}

// state :: Promise e a -> Int
Expand All @@ -126,6 +140,10 @@ export class Future extends Core {
return n === this ? PENDING : n.state()
}

_getHandle () {
return this.handle
}

_isResolved () {
return this.near() !== this
}
Expand Down Expand Up @@ -170,6 +188,7 @@ export class Future extends Core {

_become (p) {
/* eslint complexity:[2,8] */
// assert: p is not a resolved future
if (p === this) {
p = cycle()
}
Expand Down

0 comments on commit 4d3e8a1

Please sign in to comment.