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

Why is the main API async simpleSmoothingSpline(data, opts) asynchronous ? #5

Open
loicraux opened this issue Nov 29, 2024 · 3 comments

Comments

@loicraux
Copy link

Hi !

Nice work, thanks for this library !

I was wondering why the main API async simpleSmoothingSpline(data, opts) is asynchronous ?

There is no reason as far as I can imagine for these calculus to be asynchronous. If, for some reason, users of this library do not want to block the UI with costly calculus, it's up to the user to defer this to the next tick.

I'm gonna have a deeper look at the library contents, maybe there is something I am missing ?

@loicraux
Copy link
Author

I think it boils down to the 2 calls await eigen.ready;

This should be maybe done only once at the beginning of simpleSmoothingSpline implementation, so that all the rest of the code would not be async... ?

@jxjj
Copy link
Contributor

jxjj commented Dec 1, 2024

What problem are you looking to solve?

@loicraux
Copy link
Author

loicraux commented Dec 4, 2024

I am just pointing out that many APIs / methods of the code are async while they don't have to.

You can simplify the whole codebase by simply calling Matrix.ready at the beginning of the implementation of smoothingSpline :

export default async function smoothingSpline(
  data: Point[],
  { lambda = 1000, type = "smoothing" }: smoothingSplineOptions = {}
): Promise<SmoothingSpline> {

  await Matrix.ready; // <=== The only call to an async function.

  // The rest is not async anymore :

  let splineFn: SplineFunction;
  if (type === "cubic") {
    splineFn = generateCubicSplineFunction(data);
  } else {
    splineFn = generateSmoothingSplineFunction(data, { lambda });
  }

  const splinePoints = generateSplinePoints(splineFn, data);
  return {
    fn: splineFn,
    points: splinePoints,
  };
}

The implementation of Matrix.ready being the following :

export default class Matrix implements MatrixLike<Matrix> {
  static ready = eigen.ready;
  // ...
}

Note that by making these modifications, the workaround described in the Matrix class is not necessary anymore :

  // EigenMatrix ops are async so we cannot create _eigenMatrix
  // in the constructor. If we're given an array, we'll store it
  // in _data and create the matrix when we do our first operation.

I can submit a PR for this, all the tests are passing fine...

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