Skip to content

Small optimization for parseUnsignedNumber #700

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

Closed
wants to merge 1 commit into from
Closed

Small optimization for parseUnsignedNumber #700

wants to merge 1 commit into from

Conversation

dougwilson
Copy link
Member

This implements a small optimization for the parseUnsignedNumber method in the parser. This method is called multiple times for parsing almost all packets, making a gain here seem like it would be pretty nice. Checking out a simple test on jsperf it seems like it would make this method about 3x faster than what it is now. I cannot say if this actually has any real-world performance impacts yet, though.

@sidorares
Copy link
Member

a lot of failed tests

@dougwilson
Copy link
Member Author

Yea, looks like I made a logic error in the offset calculation.

@dougwilson
Copy link
Member Author

OK, code is corrected and tests are passing.

@sidorares
Copy link
Member

lgtm, but I'll wait for benchmark (it's 2 hours wrk load test)

@dougwilson
Copy link
Member Author

No problem. Obviously I'm only interested in the patch if it actually makes any real difference, since it is an optimization :)

@felixge
Copy link
Collaborator

felixge commented Jan 7, 2014

Just ran this through my faster than c benchmark (heavy row data parsing): https://docs.google.com/spreadsheet/ccc?key=0ApjTSpxgQIZtdFZRcXpiNUFMck5uZFlBX1lObkJnTWc&usp=sharing

Seems like your patch makes things a little faster. That being said, I only ran the benchmarks once, and my computer had a few other things running : ). Looking to hear from @sidorares .

@dougwilson
Copy link
Member Author

Thanks, @felixge :)

Right now I'm actually going to say to hold off on merging this for the time being. There is another spot that can have a simple change that would also have an additional improvement like this, but more importantly, I need to be sure this change works the same as the old all the way up to the max 4 byte value.

@dougwilson dougwilson closed this Jan 7, 2014
@sidorares
Copy link
Member

My benchmarks:

npm/2.0rc2

~/wrk$ ./wrk -t6 -c100 -d100m http://******/test
Running 100m test @ http://*****/test
  6 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     9.15s     1.95s   10.80s    95.70%
    Req/Sec     2.19      3.31    27.00     95.40%
  89585 requests in 100.01m, 2.00GB read
  Socket errors: connect 0, read 0, write 0, timeout 211197
Requests/sec:     14.93
Transfer/sec:    348.82KB

dougwilson:parse-unsigned-optimization

ubuntu@ip-172-31-27-190:~/wrk$ ./wrk -t6 -c100 -d100m http://****/test
Running 100m test @ http://****/test
  6 threads and 100 connections

  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    11.24s     3.50s   14.66s    94.73%
    Req/Sec     2.38      3.69    30.00     95.68%
  94334 requests in 100.01m, 2.10GB read
  Socket errors: connect 0, read 0, write 0, timeout 209034
Requests/sec:     15.72
Transfer/sec:    367.32KB
ubuntu@ip-172-31-27-190:~/wrk$

test setup - 3 Amazon VMs in the same subnet, 2 t1.micro (server + load test client) + db.t1.micro MySQL RDS

mysql> show create table users;
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                         |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=latin1 |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)

mysql> select * from users limit 100,5;
+-----+------+
| id  | name |
+-----+------+
| 101 | 101  |
| 102 | 102  |
| 103 | 103  |
| 104 | 104  |
| 105 | 105  |
+-----+------+
5 rows in set (0.00 sec)

10000 rows total.

Server under benchmark - t1.micro

//var mysql = require('mysql2');
//var mysql = require('mysql-dougwilson');
var mysql = require('mysql');

var connection = mysql.createPool({
  host     : 'db.id.ap-southeast-2.rds.amazonaws.com',
  user     : '****',
  password : '****',
  port     : '3306',
  database : '****',
  connectionLimit: 20,
  queueLimit: 1024000,
});

require('http').createServer(function(req, res) {
  if (req.url != '/test') {
    return res.end('');
  }
  connection.query('select * from users limit 1000', function(err, rows) {
    res.end(JSON.stringify(rows));
  });
}).listen(80);

Benchmark results with node-mysql2

ubuntu@ip-172-31-27-190:~/wrk$ ./wrk -t6 -c400 -d2h http://*****/test
Running 120m test @ http://*****/test
  6 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    21.36s     9.60s   38.01s    70.09%
    Req/Sec     4.63      6.39    43.00     91.03%
  218841 requests in 120.01m, 4.89GB read
  Socket errors: connect 0, read 85, write 0, timeout 1210546
Requests/sec:     30.39
Transfer/sec:    711.63KB

@dougwilson
Copy link
Member Author

Thanks, @sidorares :) As an update, I have moved this to #702 which implements the parseUnsignedNumber correctly and also updates parseLengthCodedNumber.

dveeden pushed a commit to dveeden/mysql that referenced this pull request Jan 31, 2023
* Fixed broken import for appengine/cloudsql

appengine.go
import path of appengine/cloudsql has changed to google.golang.org/appengine/cloudsql - Fixed.

* Added my name to the AUTHORS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants