Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Bad value context for arguments value
jspm_packages

"Not optimized: Bad value context for arguments value"

Adding a property to one function, but not all functions, causes Not optimized flag on function containing Function.prototype.apply.

Cause

"V8 only recognizes monomorphic .apply call-sites" - @mraleph

Example

See example.js

Fixes

Test in node

node --trace_opt --trace_deopt example.js | grep "failed to optimize dispatch"

Test in browser

jspm install
http-server  # open http://localhost:8080/
System.config({
baseURL: "/",
defaultJSExtensions: true,
transpiler: "none",
paths: {
"github:*": "jspm_packages/github/*",
"npm:*": "jspm_packages/npm/*"
},
map: {
"assert": "github:jspm/nodelibs-assert@0.1.0",
"github:jspm/nodelibs-assert@0.1.0": {
"assert": "npm:assert@1.3.0"
},
"github:jspm/nodelibs-process@0.1.2": {
"process": "npm:process@0.11.2"
},
"github:jspm/nodelibs-util@0.1.0": {
"util": "npm:util@0.10.3"
},
"npm:assert@1.3.0": {
"util": "npm:util@0.10.3"
},
"npm:inherits@2.0.1": {
"util": "github:jspm/nodelibs-util@0.1.0"
},
"npm:process@0.11.2": {
"assert": "github:jspm/nodelibs-assert@0.1.0"
},
"npm:util@0.10.3": {
"inherits": "npm:inherits@2.0.1",
"process": "github:jspm/nodelibs-process@0.1.2"
}
}
});
var assert = require('assert');
// the following line causes "failed to optimize dispatch: Bad value context for arguments value" notification
dispatchOne.$ = 'Causes Bad value context for arguments value';
// the following line, if uncommented, will allow optimization
//dispatchTwo.$ = 'Allows optimization';
var callbacks = [dispatchOne, dispatchTwo];
for(var i=0;i<2000000; i++) {
dispatch(1,2,3,4,5);
}
function dispatch() {
var i = callbacks.length;
while (i--) {
callbacks[i].apply(null, arguments);
}
}
function dispatchOne() {
assert(arguments.length == 5);
}
function dispatchTwo() {
assert(arguments.length > 4);
}
var assert = require('assert');
var callbacks = [dispatchOne, dispatchTwo.bind(0)];
for(var i=0;i<2000000; i++) {
dispatch(1,2,3,4,5);
}
function dispatch() {
var i = callbacks.length;
while (i--) {
callbacks[i].apply(null, arguments);
}
}
function dispatchOne() {
assert(arguments.length == 5);
}
function dispatchTwo() {
assert(arguments.length > 4);
}
var assert = require('assert');
// the following line causes "failed to optimize dispatch: Bad value context for arguments value" notification
dispatchOne.$ = 'Causes Bad value context for arguments value';
// the following line, if uncommented, will allow optimization
//dispatchTwo.$ = 'Allows optimization';
var callbacks = [dispatchOne.bind(null), dispatchTwo];
for(var i=0;i<2000000; i++) {
dispatch(1,2,3,4,5);
}
function dispatch() {
var i = callbacks.length;
while (i--) {
callbacks[i].bind(0) // ensures "monomorphic .apply call-sites"
.apply(0, arguments);
}
}
function dispatchOne() {
assert(arguments.length == 5);
}
function dispatchTwo() {
assert(arguments.length > 4);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>EventsSpeedTests</title>
<style>
button {
padding: .5em 1em;
display: block;
text-align: center;
margin-top: 5px;
width: 200px;
}
</style>
</head>
<body id="home">
<h1>De-opt Test</h1>
<script src="jspm_packages/system.js"></script>
<script src="config.js"></script>
<script>
addButton('example');
addButton('example2');
addButton('fix');
function addButton(name) {
var element = document.createElement("button");
element.innerHTML = name;
element.onclick=function(){
console.log('start',name);
var start = new Date().getTime();
System.import('./'+name+'.js').then(function () {
var time = new Date().getTime() - start;
console.log('end',name, time, 'ms');
});
}
document.body.appendChild(element);
}
</script>
</body>
</html>
{
"jspm": {
"directories": {},
"dependencies": {
"assert": "github:jspm/nodelibs-assert@^0.1.0"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment