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

feat(exec): add `FutureMaker` and fix memory leaks in flatMap chains #57

Merged
merged 6 commits into from Oct 11, 2017

Conversation

Projects
None yet
1 participant
@alexandru
Member

alexandru commented Oct 2, 2017

This standard ECMAScript Promise-enabled loop crashes the Node.js process due to memory leaks:

function signal(i) {
  return new Promise(cb => setImmediate(() => cb(i)))
}

function loop(n) {
  return signal(n).then(i => {
    if (i % 1000 == 0) console.log(i)
    return loop(n + 1)
  })
}

loop(0).catch(console.error)

Note that this describes a tail-recursive loop, equivalent with:

async function loop(n) {
  const i = await signal(n)
  if (i % 1000 == 0) console.log(i)

  // Recursive call
  return await loop(n + 1)
}

Due to the changes in this PR, inspired by Scala's Future implementation which manages to avoid this problem, by using Funfix's Future this loop uses constant memory and is entirely equivalent (down to usage of setImmediate):

import { Future } from "funfix"

function loop(n) {
  return Future.of(() => n).flatMap(i => {
    if (i % 1000 == 0) console.log(i)
    return loop(n + 1)
  })
}

loop(0).recover(console.error)

The PR also introduces FutureMaker, which is a Future builder, inspired by Scala's Promise and by the now deprecated JavaScript Deferred. The safer Future.create builder is still available.

import { Future, FutureMaker, Success } from "funfix"

// The write interface
const m = FutureMaker.empty<number>()

// The read interface
const f: Future<number> = m.future()

// Completing our future
m.complete(Success(1))

@alexandru alexandru changed the title from WIP: refactoring Future, get rid of leaks, introduce `FutureBuilder` to WIP: get rid of Future leaks, introduce `FutureBuilder` Oct 2, 2017

@codecov

This comment has been minimized.

Show comment
Hide comment
@codecov

codecov bot Oct 2, 2017

Codecov Report

Merging #57 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master    #57    +/-   ##
======================================
  Coverage     100%   100%            
======================================
  Files          24     24            
  Lines        2470   2626   +156     
  Branches      393    432    +39     
======================================
+ Hits         2470   2626   +156
Impacted Files Coverage Δ
packages/funfix-exec/src/future.ts 100% <100%> (ø) ⬆️
packages/funfix-exec/src/cancelable.ts 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 940d25f...4e90902. Read the comment docs.

codecov bot commented Oct 2, 2017

Codecov Report

Merging #57 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master    #57    +/-   ##
======================================
  Coverage     100%   100%            
======================================
  Files          24     24            
  Lines        2470   2626   +156     
  Branches      393    432    +39     
======================================
+ Hits         2470   2626   +156
Impacted Files Coverage Δ
packages/funfix-exec/src/future.ts 100% <100%> (ø) ⬆️
packages/funfix-exec/src/cancelable.ts 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 940d25f...4e90902. Read the comment docs.

@alexandru alexandru changed the title from WIP: get rid of Future leaks, introduce `FutureBuilder` to WIP: get rid of Future leaks, introduce Deferred Oct 10, 2017

@alexandru alexandru changed the title from WIP: get rid of Future leaks, introduce Deferred to feat(exec): add `FutureMaker` and fix memory leaks in flatMap chains Oct 11, 2017

@alexandru alexandru merged commit d469def into master Oct 11, 2017

4 checks passed

codecov/patch 100% of diff hit (target 100%)
Details
codecov/project 100% (+0%) compared to 940d25f
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment