You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Major Performance Regression with 1.12 generator based stream parsing #303
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
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.
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.
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.
@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.
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:
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.
Activity
arthurschreiber commentedon Aug 20, 2015
Thanks for the heads up! I'll take a look and see what is going on.
arthurschreiber commentedon Aug 21, 2015
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:
src/value-parser.jsis quite convoluted and when transpiled back to ES5 compatible javascript, the switch statement that is used byregeneratorfor thevalueParsefunction ends up with more than 128 cases, which makes the whole function non-optimizable.regeneratortranspilation, 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.12for now.arthurschreiber commentedon Aug 28, 2015
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:
This is really not that great. Even when running on iojs, which has native generator support, the numbers are worse than before:
I'll need to come up with a plan on how to resolve this with the next minor version.
bretcope commentedon Aug 28, 2015
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, ortryblocks 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.
arthurschreiber commentedon Aug 29, 2015
Yes, and no. As I'm using
babel(which internally usesregenerator) to transpile the generator based code back into ES5, this is only partially relevant for tedious.When running with
iojsand 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.letstill has some issues,constworks fine.argumentshas a few unsafe use cases, but does not prevent optimization in general.tryis pure poison for optimization.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.
kjvalencik commentedon Feb 17, 2016
@arthurschreiber Was this regression resolved?
arthurschreiber commentedon Feb 20, 2016
@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.
quazzie commentedon Apr 8, 2016
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 ?
SteveyPugs commentedon Jul 25, 2016
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
egandro commentedon Jun 24, 2017
I also have a major performance issue using Image columns.
Is this fixed?
arthurschreiber commentedon Sep 1, 2026
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, 10nvarchar, 5bit, ~9.3MB), fed to the token parser in 4KB packet-sized chunks — across the relevant tedious versions on Node.js 22:Summary:
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.5MBvarbinary(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