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

Performance of sequenceT & sequenceS #1255

Closed
pbadenski opened this issue Jun 29, 2020 · 2 comments
Closed

Performance of sequenceT & sequenceS #1255

pbadenski opened this issue Jun 29, 2020 · 2 comments
Assignees
Milestone

Comments

@pbadenski
Copy link
Contributor

🐛 Bug report

In general we're very happy with fp-ts performance, but recently we've noticed small problems with sequenceT and specifically the curried function. It's not a huge problem - but a noticeable one and it would be nice if it could be fixed.

Reproducible example

fp-ts generic curried of 1 and 2 arguments + a handrolled curried of 2 arguments for reference.

https://stackblitz.com/edit/fp-ts-curried-performance-jc38ka?file=index.js


curried(1) x 4,532,421 ops/sec ±1.45% (65 runs sampled)

curried(2) x 2,342,792 ops/sec ±0.88% (65 runs sampled)

curried2 handrolled x 34,394,850 ops/sec ±1.55% (62 runs sampled)

Suggested solution(s)

  1. (approx 1 order of magnitude, 5-10 x times speed up) Replace acc.concat([x]) with a faster implementation of concat:
const fastConcat = <T>(a: T[], b: T) => {
  const result = Array(a.length + 1);

  for (let i = 0; i < a.length; i++) {
    result[i] = a[i];
  }
  result[a.length] = b;
  return result;
};
  1. (solid 1 order of magnitude, 10-50 x times speed up) Handroll the first N (5?) curried implementations, ie.
function curried2<T>(f: Function) {
  return function (x1: T) {
    return function (x2: T) {
      return f(x1, x2);
    };
  }
}

*) alternatively first N implementations could be autogenerated, although this might be a controversial suggestion:
const f = Function("f", `return ${range(1, N).map(n => `x${n}`).join(" => ")} => f(${range(1, N).map(n => `x${n}`).join(" + ")})`);

Your environment

Software Version(s)
fp-ts 2.5.3
TypeScript 3.9.3
@gcanti gcanti added this to the 2.7 milestone Jun 29, 2020
@gcanti gcanti self-assigned this Jun 29, 2020
@pbadenski
Copy link
Contributor Author

Amazing stuff, thank you!

@gcanti gcanti closed this as completed in 0e9dc73 Jun 30, 2020
@gcanti
Copy link
Owner

gcanti commented Jun 30, 2020

@pbadenski thanks to you for the report / suggestion 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants