-
-
Notifications
You must be signed in to change notification settings - Fork 32k
benchmark: improve WHATWG URL benchmarks #10678
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const url = require('url'); | ||
const URL = url.URL; | ||
const assert = require('assert'); | ||
|
||
const inputs = { | ||
long: 'http://nodejs.org:89/docs/latest/api/url.html#test?' + | ||
'payload1=true&payload2=false&test=1&benchmark=3&' + | ||
'foo=38.38.011.293&bar=1234834910480&test=19299&3992&' + | ||
'key=f5c65e1e98fe07e648249ad41e1cfdb0', | ||
short: 'https://nodejs.org/en/blog/', | ||
idn: 'http://你好你好', | ||
auth: 'https://user:pass@example.com/path?search=1', | ||
special: 'file:///foo/bar/test/node.js', | ||
percent: 'https://%E4%BD%A0/foo', | ||
dot: 'https://example.org/./a/../b/./c' | ||
}; | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: Object.keys(inputs), | ||
method: ['legacy', 'whatwg'], | ||
n: [1e5] | ||
}); | ||
|
||
// At the time of writing, when using a passed property name to index | ||
// the object, Crankshaft would generate a LoadKeyedGeneric even when it | ||
// remains a constant in the function, so here we must use the literal | ||
// instead to get a LoadNamedField. | ||
function useLegacy(n, input) { | ||
var obj = url.parse(input); | ||
var noDead = { | ||
protocol: obj.protocol, | ||
auth: obj.auth, | ||
host: obj.host, | ||
hostname: obj.hostname, | ||
port: obj.port, | ||
pathname: obj.pathname, | ||
search: obj.search, | ||
hash: obj.hash | ||
}; | ||
// It's necessary to assign the values to an object | ||
// to avoid loop invariant code motion. | ||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
noDead.protocol = obj.protocol; | ||
noDead.auth = obj.auth; | ||
noDead.host = obj.host; | ||
noDead.hostname = obj.hostname; | ||
noDead.port = obj.port; | ||
noDead.pathname = obj.pathname; | ||
noDead.search = obj.search; | ||
noDead.hash = obj.hash; | ||
} | ||
bench.end(n); | ||
return noDead; | ||
} | ||
|
||
function useWHATWG(n, input) { | ||
var obj = new URL(input); | ||
var noDead = { | ||
protocol: obj.protocol, | ||
auth: obj.username + ':' + obj.password, | ||
host: obj.host, | ||
hostname: obj.hostname, | ||
port: obj.port, | ||
pathname: obj.pathname, | ||
search: obj.search, | ||
hash: obj.hash | ||
}; | ||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
noDead.protocol = obj.protocol; | ||
noDead.auth = obj.username + ':' + obj.password; | ||
noDead.host = obj.host; | ||
noDead.hostname = obj.hostname; | ||
noDead.port = obj.port; | ||
noDead.pathname = obj.pathname; | ||
noDead.search = obj.search; | ||
noDead.hash = obj.hash; | ||
} | ||
bench.end(n); | ||
return noDead; | ||
} | ||
|
||
function main(conf) { | ||
const type = conf.type; | ||
const n = conf.n | 0; | ||
const method = conf.method; | ||
|
||
const input = inputs[type]; | ||
if (!input) { | ||
throw new Error('Unknown input type'); | ||
} | ||
|
||
var noDead; // Avoid dead code elimination. | ||
switch (method) { | ||
case 'legacy': | ||
noDead = useLegacy(n, input); | ||
break; | ||
case 'whatwg': | ||
noDead = useWHATWG(n, input); | ||
break; | ||
default: | ||
throw new Error('Unknown method'); | ||
} | ||
|
||
assert.ok(noDead); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const url = require('url'); | ||
const URL = url.URL; | ||
const assert = require('assert'); | ||
|
||
const inputs = { | ||
long: 'http://nodejs.org:89/docs/latest/api/url.html#test?' + | ||
'payload1=true&payload2=false&test=1&benchmark=3&' + | ||
'foo=38.38.011.293&bar=1234834910480&test=19299&3992&' + | ||
'key=f5c65e1e98fe07e648249ad41e1cfdb0', | ||
short: 'https://nodejs.org/en/blog/', | ||
idn: 'http://你好你好', | ||
auth: 'https://user:pass@example.com/path?search=1', | ||
special: 'file:///foo/bar/test/node.js', | ||
percent: 'https://%E4%BD%A0/foo', | ||
dot: 'https://example.org/./a/../b/./c' | ||
}; | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: Object.keys(inputs), | ||
method: ['legacy', 'whatwg'], | ||
n: [1e5] | ||
}); | ||
|
||
function useLegacy(n, input) { | ||
var noDead = url.parse(input); | ||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
noDead = url.parse(input); | ||
} | ||
bench.end(n); | ||
return noDead; | ||
} | ||
|
||
function useWHATWG(n, input) { | ||
var noDead = url.parse(input); | ||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
noDead = new URL(input); | ||
} | ||
bench.end(n); | ||
return noDead; | ||
} | ||
|
||
function main(conf) { | ||
const type = conf.type; | ||
const n = conf.n | 0; | ||
const method = conf.method; | ||
|
||
const input = inputs[type]; | ||
if (!input) { | ||
throw new Error('Unknown input type'); | ||
} | ||
|
||
var noDead; // Avoid dead code elimination. | ||
switch (method) { | ||
case 'legacy': | ||
noDead = useLegacy(n, input); | ||
break; | ||
case 'whatwg': | ||
noDead = useWHATWG(n, input); | ||
break; | ||
default: | ||
throw new Error('Unknown method'); | ||
} | ||
|
||
assert.ok(noDead); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const url = require('url'); | ||
const URL = url.URL; | ||
const assert = require('assert'); | ||
|
||
const inputs = { | ||
long: 'http://nodejs.org:89/docs/latest/api/url.html#test?' + | ||
'payload1=true&payload2=false&test=1&benchmark=3&' + | ||
'foo=38.38.011.293&bar=1234834910480&test=19299&3992&' + | ||
'key=f5c65e1e98fe07e648249ad41e1cfdb0', | ||
short: 'https://nodejs.org/en/blog/', | ||
idn: 'http://你好你好', | ||
auth: 'https://user:pass@example.com/path?search=1', | ||
special: 'file:///foo/bar/test/node.js', | ||
percent: 'https://%E4%BD%A0/foo', | ||
dot: 'https://example.org/./a/../b/./c' | ||
}; | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: Object.keys(inputs), | ||
method: ['legacy', 'whatwg'], | ||
n: [1e5] | ||
}); | ||
|
||
function useLegacy(n, input, prop) { | ||
var obj = url.parse(input); | ||
var noDead = url.format(obj); | ||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
noDead = url.format(obj); | ||
} | ||
bench.end(n); | ||
return noDead; | ||
} | ||
|
||
function useWHATWG(n, input, prop) { | ||
var obj = new URL(input); | ||
var noDead = obj.toString(); | ||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
noDead = obj.toString(); | ||
} | ||
bench.end(n); | ||
return noDead; | ||
} | ||
|
||
function main(conf) { | ||
const type = conf.type; | ||
const n = conf.n | 0; | ||
const method = conf.method; | ||
|
||
const input = inputs[type]; | ||
if (!input) { | ||
throw new Error('Unknown input type'); | ||
} | ||
|
||
var noDead; // Avoid dead code elimination. | ||
switch (method) { | ||
case 'legacy': | ||
noDead = useLegacy(n, input); | ||
break; | ||
case 'whatwg': | ||
noDead = useWHATWG(n, input); | ||
break; | ||
default: | ||
throw new Error('Unknown method'); | ||
} | ||
|
||
assert.ok(noDead); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and the
return noDead
in each function are unnecessary.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Crankshaft is able to do code motion if we simply do
var protocol = obj.protocol
and leave us timing an empty loop in the legacy version(WHATWG version is not affected because V8's current limitation of closures affects the getters). At the moment looks like it's the fear of changing the hidden class ofnoDead
makes it back off from doing code motion for the legacy implementation(by looking at the output of--trace_gvn
), so keepingnoDead
not dead could be a little bit more future-proof.Aside: another way would be to add some interface to
bench
so it can suspend and resume the timer at anytime, then we can have more flexibility when it comes to avoiding loop invariant code motion, like generating different inputs based on i for each cycle.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's absolutely needed, then there should be some comments above each explaining things a bit, otherwise someone may come along and submit a PR to remove them without knowing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, I will at some comments to the declaration of
noDead
inmain
.