Skip to content

Major Performance Regression with 1.12 generator based stream parsing #303

Closed

Description

@jeremydiviney

When executing an sql statement that returns a non-trivial number of rows (500+), that last releases that uses the babel runtime, are performing about 20 times slower than the previous (1.11.4) and older builds.

I have run the node-inspector and it seems to be due to a huge increase in the cpu utilization while fetching rows, and most of that cpu time is spent in babel runtime calls.

For example I tested fetching about 20,000 rows with about 25 columns of int,nvarchar,bit types, and on the older versions the data is returned in just about 1000ms (which is just about the same as it return in SQL management studio).... In the latest couple versions, is it taking well over 25 seconds(25,000ms).

We are seeing the same performance regression on all types of queries...

This seems like a major issue, that needs to be address very soon

Activity

  1. arthurschreiber commented on Aug 20, 2015

    @arthurschreiber
    Collaborator

    Thanks for the heads up! I'll take a look and see what is going on.

  2. arthurschreiber commented on Aug 21, 2015

    @arthurschreiber
    Collaborator

    So I checked this, and yes, you're totally right, there's been quite some performance impact with the switch to the stream based generator.

    There's a few issues at hand:

    • The code in src/value-parser.js is quite convoluted and when transpiled back to ES5 compatible javascript, the switch statement that is used by regenerator for the valueParse function ends up with more than 128 cases, which makes the whole function non-optimizable.
    • Even when not using the regenerator transpilation, but running on iojs directly, the generator code is one order of magnitude slower than the parser code.

    This regression is not acceptable, and I'll try to get this fixed. I don't want to move away from a streaming parser implementation (as the old parser had some weird quirks as well), but this change in performance is not cool.

    If your application is performance sensitive, I'd suggest you hold off upgrading to 1.12 for now.

  3. arthurschreiber commented on Aug 28, 2015

    @arthurschreiber
    Collaborator

    So, I wrote some becnhmark that focuses on testing only the token parser functionality, and it is around an order of magnitude slower than before:

    $ git checkout v1.11.4
    $ node benchmarks/token-stream-parser.js
    parse tokens x 77.92 ops/sec ±0.74% (69 runs sampled)
    
    $ git checkout master
    $ node benchmarks/token-stream-parser.js
    parse tokens x 8.55 ops/sec ±3.84% (44 runs sampled)
    

    This is really not that great. Even when running on iojs, which has native generator support, the numbers are worse than before:

    $ iojs benchmarks/token-stream-parser.js
    parse tokens x 32.77 ops/sec ±1.31% (79 runs sampled)
    

    I'll need to come up with a plan on how to resolve this with the next minor version.

  4. bretcope commented on Aug 28, 2015

    @bretcope
    Member

    It may be due to generators still not being optimized (I haven't kept up enough on v8 lately to know if that's changed). If so, what often helps is to move everything you can into non-generator functions. Never have big generators with lots of code in them; have small generators and call out to larger synchronous functions. Last I knew (could have changed since), functions which use let, const, arguments, or try blocks weren't optimizable either, so they're best to avoid as much as possible.

    Tedious also has some unfortunate polymorphism properties which defeat inline caching optimizations. I'm not sure there's any good way around that since JavaScript doesn't support true generics, but it's something to keep in mind that less polymorphism is better, performance-wise.

    There are ways to profile which functions are getting optimized, which aren't, and how much time is being spent in different functions. It's been a while since I've done it, but the tools exist if you can't solve your issues with intuitive changes.

  5. arthurschreiber commented on Aug 29, 2015

    @arthurschreiber
    Collaborator

    It may be due to generators still not being optimized (I haven't kept up enough on v8 lately to know if that's changed)

    Yes, and no. As I'm using babel (which internally uses regenerator) to transpile the generator based code back into ES5, this is only partially relevant for tedious.

    When running with iojs and disabling the generator transpilation, I can see the functions get optimized, but they're still a lot slower than non-generator based code. This might change in the future, but it's not great to dump this performance burden on anyone using an older version of node.js.

    Never have big generators with lots of code in them; have small generators and call out to larger synchronous functions. Last I knew (could have changed since), functions which use let, const, arguments, or try blocks weren't optimizable either, so they're best to avoid as much as possible.

    let still has some issues, const works fine. arguments has a few unsafe use cases, but does not prevent optimization in general. try is pure poison for optimization.

    Tedious also has some unfortunate polymorphism properties which defeat inline caching optimizations. I'm not sure there's any good way around that since JavaScript doesn't support true generics, but it's something to keep in mind that less polymorphism is better, performance-wise.

    This is true, but breaking functions down often helps to work around this. I'll focus on this once I've merged the ESnext code changes and fixed the performance issue described in this ticket.


    Overall, I've started rewriting the generator based code into more basic, callback based code, while still retaining the streaming nature of the parsing code. Initial benchmarking shows that this is as fast as the previous, buffering parser code. It makes the code a tiny bit more ugly (due to callback nesting) but that's what we'll have to live with for now.

    @jeremydiviney The performance regression will be fixed with the next tedious version.

  6. kjvalencik commented on Feb 17, 2016

    @kjvalencik

    @arthurschreiber Was this regression resolved?

  7. arthurschreiber commented on Feb 20, 2016

    @arthurschreiber
    Collaborator

    @kjvalencik Partially, yes. The parser was changed to not use any generator functions anymore. There's still a few areas left where performance can be improved, but that work is not finished yet. Overall, performance will largely depend on your use cases.

  8. quazzie commented on Apr 8, 2016

    @quazzie

    I have a problem when fetching a Image type from sql.
    Single row, 2 columns (int, image)
    image is about 5.5mb

    On [email protected] / [email protected] it takes around 5sec.
    On [email protected] / [email protected] it takes about 400ms

    Anything i can do about this ?

  9. SteveyPugs commented on Jul 25, 2016

    @SteveyPugs
    Contributor

    Was this ever resolved? I've noticed sequelize has upgraded to 1.13.x which this still causes a major performance issue. sequelize/sequelize@80e2b77

  10. egandro commented on Jun 24, 2017

    @egandro

    I also have a major performance issue using Image columns.

    Is this fixed?

  11. arthurschreiber commented on Sep 1, 2026

    @arthurschreiber
    Collaborator

    Closing this out with a final round of benchmarking to confirm the regression reported here is fully resolved.

    I replayed the workload from the original report — a raw TDS token stream of 20,000 rows × 25 columns (10 int, 10 nvarchar, 5 bit, ~9.3MB), fed to the token parser in 4KB packet-sized chunks — across the relevant tedious versions on Node.js 22:

    Version Parser implementation Time
    1.11.4 pre-regression (buffering parser) 79 ms
    1.12.3 regressed (babel/regenerator generator-based parser) 1,100 ms
    1.13.2 callback-based streaming rewrite 137 ms
    1.15.0 same 153 ms
    latest async-iterator based parser 95 ms
    master same 79 ms

    Summary:

    • The regression reported here was real and still reproduces on the 1.12.x releases (~14× slower, in line with the ~20× originally reported on 2015-era V8).
    • The parser rewrite that shipped in 1.13 recovered most of the performance, leaving a residual ~1.7–1.9× gap (which is what "partially resolved" referred to at the time).
    • Current versions of tedious have eliminated that residual gap entirely — the current parser matches pre-regression 1.11.4 performance exactly on this workload.

    Regarding the follow-up comments about large image/blob columns being slow (5.5MB image, ~400ms on 1.11.4 vs ~5s on 1.13.2): this does not reproduce in the token parser. Parsing a single-row result with a 5.5MB varbinary(max) value (same chunked PLP path) takes 11–24 ms on every version from 1.12.3 through current master — while 1.11.4 is actually the slow one at 890 ms, due to quadratic buffer concatenation in the old buffering parser. If anyone still sees slow large-value reads on a current release, please open a new issue with a reproduction.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions