GithubHelp home page GithubHelp logo

Comments (6)

cakoose avatar cakoose commented on May 20, 2024

Small demo of the problem: https://gist.github.com/cakoose/5271c469142205d37c600000cc22bb3e

from mammoth.

martijndeh avatar martijndeh commented on May 20, 2024

Mammoth automatically executes your query when you await it. This is done by adding a .then() so the whole query looks like a promise and thus can be await'ed.

There isn't a function which we can change to async to make sure native async / await is triggered. How do you think we can fix this?

  1. Create an async exec() { .. } on every function — assuming this will trigger native async / await (is this true?). (a) This would be in addition to .then() (b) remove .then() so you must always explicitly .exec() your queries.
  2. Create some sort of trace capture mechanism similar to something like this one https://knexjs.org/#Installation-asyncStackTraces
  3. Anything else?

from mammoth.

cakoose avatar cakoose commented on May 20, 2024

Re 1: Using an explicit .exec() appears to yield good stack traces: snippet. I would love to get it working for the plain .then as well, but I don't know if it's possible. (I tried asking on the v8-users mailing list but never got a response.)

Re 2: That style of stack trace preservation isn't as efficient, and thus is not recommended in production. Having something only work in dev is still definitely useful, but I'm hoping we can get V8 native async stack traces working, which will work in both.

Tangent: Is an explicit .exec() function a good idea?

Zapatos has an explicit .run(conn). Knex seems to do what Mammoth does. I don't know which is better but here are some thoughts:

When I first started using Mammoth, I preferred the idea of Zapatos' explicit .run(conn), since it separated the concerns of building the query and executing the query. But after having used Mammoth for a while, I've gotten used to the convenience of just doing await 😅 .

There's also a small practical advantage to Mammoth's approach: I can use the ESLint no-floating-promises rule to detect when I forget to await. In theory, we should be able to write an ESLint rule to detect cases where someone builds a query but forgets to run it, but nobody has written that yet.

from mammoth.

martijndeh avatar martijndeh commented on May 20, 2024

Re 2: That style of stack trace preservation isn't as efficient, and thus is not recommended in production.

I do not see any performance differences in the happy path when enabling asyncStackTraces in Knex (there is a simple benchmark in the repo now). Are you referring to the extra memory to keep track of the stack?

from mammoth.

cakoose avatar cakoose commented on May 20, 2024

I do not see any performance differences in the happy path when enabling asyncStackTraces in Knex (there is a simple benchmark in the repo now). Are you referring to the extra memory to keep track of the stack?

I was just going off of the warning in Knex's docs ("This has small performance overhead, so it is advised to use only for development.") and the general warnings around "async stack trace" libraries in general, e.g. longjohn.

I assume the cost is mainly in generating the stack trace, but it probably adds some GC pressure too But maybe that overhead is not actually that much in this context because a remote DB query is already relatively expensive?

Where's the benchmark? I wonder if it's measuring a case where generating the stack is cheap for whatever reason, e.g. shallow stack.

from mammoth.

martijndeh avatar martijndeh commented on May 20, 2024

The benchmark is here https://github.com/Ff00ff/mammoth/tree/master/workspaces/sql-benchmarks. You are right, the stack isn't too deep so maybe that's why there is no significant overhead.

from mammoth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.