zlib: make constants keep readonly#1361
Conversation
In zlib module, a dozen constants were exported to user land, If user change the constant, maybe lead unexcepted error. Make them readonly and freezon.
|
LGTM. Anyone else from @iojs/collaborators want to weigh in? |
|
I like it. Don't think we need a full jenkins run for this, right? LGTM. |
|
How about to add |
|
@shigeki the |
|
@JacksonTian That's fine. LGTM. |
In zlib module, a dozen constants were exported to user land, If user change the constant, maybe lead unexcepted error. Make them readonly and freezon. PR-URL: #1361 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp>
|
Thanks, I made tests locally and all is fine. Landed in 372bf83. |
|
Wouldn't it slow down the lookup? I remember that I did similar thing for node 0.4, but we reverted it because of slowdown :) |
|
Well, I will make a benchmark. |
|
For quick benchmark as blow, this commit gets about 4.6% slower in the lookup performance. ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 292205.78329
ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 311704.56550
ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 263579.49684
ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 273116.33671
ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 317763.51096
ave. 291673.93866/sec
after reverting 372bf83818fd87bae5ad7cedadfa121ddc5d4345
ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 329755.18151
ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 252276.96915
ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 326891.71396
ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 290428.49166
ohtsu@ubuntu:~/github/io.js$ ./iojs benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 329554.20748
ave. 305781.312752/sec
291673.93866/305781.312752=0.95386449889617157777803953405404var common = require('../common.js');
var zlib = require('zlib');
var assert = require('assert');
var bench = common.createBenchmark(main, {n: [2e6]});
var zlib_codes = {
'0': 'Z_OK',
'1': 'Z_STREAM_END',
'2': 'Z_NEED_DICT',
Z_OK: 0,
Z_STREAM_END: 1,
Z_NEED_DICT: 2,
Z_ERRNO: -1,
Z_STREAM_ERROR: -2,
Z_DATA_ERROR: -3,
Z_MEM_ERROR: -4,
Z_BUF_ERROR: -5,
Z_VERSION_ERROR: -6,
'-1': 'Z_ERRNO',
'-2': 'Z_STREAM_ERROR',
'-3': 'Z_DATA_ERROR',
'-4': 'Z_MEM_ERROR',
'-5': 'Z_BUF_ERROR',
'-6': 'Z_VERSION_ERROR'
};
function main(conf) {
var n = conf.n | 0;
bench.start();
for (var i = 0; i < n; i += 1) {
for(var key in zlib.codes) {
assert.equal(zlib.codes[key], zlib_codes[key]);
}
}
bench.end(n);
} |
|
Thanks @shigeki! I don't think that it would take much time then. LGTM |
|
@indutny Okay, thanks for pointing out to care about performance. I missed it. |
|
@shigeki sorry for the silence; blaming timezones. |
|
how about use Object.keys instead of for/in in benchmark? For/in is slow case. 发自我的 iPhone
|
|
@JacksonTian It makes both performance get better owing to non-enumeration of prototype chain. A new benchmark shows that difference is about 7.5% slower in lookups. It seems that this number varies between 13% and 5%. --- a/benchmark/zlib/zlib_lookup.js
+++ b/benchmark/zlib/zlib_lookup.js
@@ -29,9 +29,9 @@ function main(conf) {
bench.start();
for (var i = 0; i < n; i += 1) {
- for(var key in zlib.codes) {
+ Object.keys(zlib.codes).forEach(function(key) {
assert.equal(zlib.codes[key], zlib_codes[key]);
- }
+ });
}
bench.end(n);
}ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 343327.09061
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 363036.73678
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 355959.65101
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 349914.96226
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 353198.02292
ave. 353087.292716
Reverted
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 381779.95576
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 388148.84485
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 386636.61563
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 379509.47249
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 370719.73390
ave. 381358.924526
353087.292716/381358.924526=0.92586608050371581616651897385889@jbergstroem Never mind. I thought you had been in online at that time. |
|
@shigeki The |
|
@trevnorris Wow, I did not realize that the forEach has so much performance degradation. Thanks. A new benchmark shows about 11% performance loss in lookup with this commit. I can't decide if it is serious. ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 625939.69392
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 669428.59785
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 661328.00027
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 656103.20345
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 632819.48158
ave. 649123.795414
Reverted
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 702057.08514
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 755169.90155
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 742403.59253
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 745905.08977
ohtsu@ubuntu:~/github/io.js$ ./iojs ./benchmark/zlib/zlib_lookup.js
zlib/zlib_lookup.js n=2000000: 693043.52458
ave. 727715.838714
649123.795414/727715.838714=0.89200174145050111799867436958126--- a/benchmark/zlib/zlib_lookup.js
+++ b/benchmark/zlib/zlib_lookup.js
@@ -29,9 +29,10 @@ function main(conf) {
bench.start();
for (var i = 0; i < n; i += 1) {
- Object.keys(zlib.codes).forEach(function(key) {
- assert.equal(zlib.codes[key], zlib_codes[key]);
- });
+ var keys = Object.keys(zlib.codes);
+ for(var j = 0; j < keys.length; ++j) {
+ assert.equal(zlib.codes[keys[j]], zlib_codes[keys[j]]);
+ };
}
bench.end(n);
} |
|
The case shows for/in, forEach ’s performance effect more than lookup a lot.
|
In zlib module, a dozen constants were exported to user land,
If user change the constant, maybe lead unexcepted error.
Make them readonly and freezon.