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

Temporal kills #169

Closed
Changes from 9 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
56 changes: 38 additions & 18 deletions src/Effect/Aff.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ var Aff = function () {
var FIBER = "Fiber"; // Actual fiber reference
var THUNK = "Thunk"; // Primed effect, ready to invoke

// Error used for early cancelation on Alt branches.
// This is initialized here (rather than in the Fiber constructor) because
// otherwise, in V8, this Error object indefinitely hangs on to memory that
// otherwise would be garbage collected.
var early = new Error("[ParAff] Early exit");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find. Any thoughts as to why this happens? Just curious.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess it has something to do with stack frames and traces or whatever.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<symbol> in Error is what is hanging onto the memory, whatever that is, and I do not know why Error is being referenced from the root. I figure it is some special debugging sauce in V8 being quirky.


function Aff(tag, _1, _2, _3) {
this.tag = tag;
this._1 = _1;
Expand Down Expand Up @@ -320,24 +326,41 @@ var Aff = function () {

case ASYNC:
status = PENDING;
step = runAsync(util.left, step._1, function (result) {
return function () {
if (runTick !== localRunTick) {
return;
}
runTick++;
Scheduler.enqueue(function () {
// It's possible to interrupt the fiber between enqueuing and
// resuming, so we need to check that the runTick is still
// valid.
if (runTick !== localRunTick + 1) {
tmp = step._1;
step = nonCanceler;
Scheduler.enqueue(function () {
if (runTick !== localRunTick) {
return;
}
var issync = true;
var resolved = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think two separate pieces of state are necessary for this check. This pattern is done in the sequential code several times.
https://github.com/slamdata/purescript-aff/blob/202a3eac12a207b6f109c9708f3efb034791bd36/src/Effect/Aff.js#L776-L795

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Just so you know, I am doing this so that run(runTick) is a tail call.

var canceler = runAsync(util.left, tmp, function (result) {
return function () {
if (runTick !== localRunTick) {
return;
}
++runTick;
resolved = true;
status = STEP_RESULT;
step = result;
run(runTick);
});
};
step = result;
// Do not recurse on run if we are synchronous with runAsync.
if (!issync) {
run(runTick);
}
};
});
issync = false;
// Only update the canceler if the asynchronous action has not
// resolved synchronously. If it has, then the next status and
// step have already been set.
if (!resolved) {
step = canceler;
}
// If runAsync already resolved then the next step needs to be
// run.
else {
run(runTick);
}
});
return;

Expand Down Expand Up @@ -643,9 +666,6 @@ var Aff = function () {
var killId = 0;
var kills = {};

// Error used for early cancelation on Alt branches.
var early = new Error("[ParAff] Early exit");

// Error used to kill the entire tree.
var interrupt = null;

Expand Down