Skip to content

Commit

Permalink
Reduced the amount of garbage created during JIT process.
Browse files Browse the repository at this point in the history
General tidy up.
  • Loading branch information
paulcuth committed Oct 29, 2014
1 parent 262abc0 commit fc9ffee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
61 changes: 25 additions & 36 deletions vm/src/jit.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
var SET_REG_PATTERN = /^setR\(R,(\d+),([^;]*?)\);$/,
gc = shine.gc,
Function_apply = shine.Function.prototype.apply,
compileQueue = [],
compileQueue = gc.createArray(),
frameCounter = 0,
waitingToCompile = false,
getNow = Date['now']? Date['now'] : function () { return new Date().getTime(); },
Expand Down Expand Up @@ -145,15 +145,15 @@

function createRunner (instance, data, compiled) {
return function (context, args) {
var closure = shine.gc.createObject(),
var closure = gc.createObject(),
retvals;

closure._vm = instance._vm;
closure._globals = instance._globals;
closure._upvalues = instance._upvalues;
closure._constants = data.constants;
closure._functions = data.functions;
closure._localsUsedAsUpvalues = shine.gc.createArray();
closure._localsUsedAsUpvalues = gc.createArray();

return compiled.apply(closure, args);
};
Expand Down Expand Up @@ -220,7 +220,11 @@
waitingToCompile = false;
shine.jit.onCompile();

while (compileQueue.length) compile.apply(null, compileQueue.shift());
while (compileQueue.length) {
var item = compileQueue.shift()
compile(item[0], item[1]);
gc.collect(item);
}
}


Expand All @@ -239,21 +243,8 @@
******************************************************************/


/**
* Create a comma delimited string of consecutive numbers.
* @param {number} last The number on which to end the sequence.
* @param {string} [prefix=''] Optional prefix to each number in the string.
* @param {number} [first=0] Optional start number.
* @returns {string} Comma delimited string.
*/
function createNumberString (last, prefix, first) {
prefix = '' + (prefix || '');
var x = first || 0;
if (last < x) return '';
return Array(last - x + 1).join().replace(new RegExp('','g'), function () { return prefix + x++; });
}


var NEWLINE_PATTERN = /\n/g,
APOS_PATTERN = /'/g;


/**
Expand All @@ -263,8 +254,8 @@
*/
function formatValue (value) {
if (typeof value == 'string') {
value = value.replace(/\n/g, '\\n');
value = value.replace(/'/g, '\\\'');
value = value.replace(NEWLINE_PATTERN, '\\n');
value = value.replace(APOS_PATTERN, '\\\'');
return "'" + value + "'";
}

Expand Down Expand Up @@ -295,24 +286,20 @@


function translate_move (a, b) {
// return 'R' + a + '=R' + b + ';';
// return 'setupval(' + a + ',register[' + b + ']);';
return 'setR(R,' + a + ',R[' + b + ']);';
}




function translate_loadk (a, bx) {
// return 'R' + a + '=' + formatValue(this.getConstant(bx)) + ';';
return 'setR(R,' + a + ',' + formatValue(this.getConstant(bx)) + ');';
}




function translate_loadbool (a, b, c) {
// var result = 'decr(register[' + a + ']);register[' + a + ']=' + !!b + ';',
var result = 'setR(R,' + a + ',' + !!b + ');',
pc;

Expand Down Expand Up @@ -351,8 +338,6 @@

function translate_getglobal (a, b) {
var key = this.getConstant(b);

// return 'R' + a + '=cl._globals' + ((key == '_G')? '' : '[' + formatValue(key) + ']') + ';';
return 'setR(R,' + a + ',shine_g' + ((key == '_G')? '' : '[' + formatValue(key) + ']') + ');';
}

Expand Down Expand Up @@ -813,13 +798,12 @@
* @param {shine.Function} func The input Moonshine function definition.
* @returns {function} A JavaScript representation of the function.
*/
// shine.jit.compile = function (func) {
// var js = shine.jit.toJS(func);
// return shine.operations.evaluateInScope(js);
// };
shine.jit.compile = function (func, callback) {
if (shine.jit.MIN_FPS_TO_COMPILE) {
compileQueue.push(arguments);
var args = gc.createArray();
args.push(func, callback);

compileQueue.push(args);
enableCompileTimer();

} else {
Expand Down Expand Up @@ -862,17 +846,18 @@
pc: pc,
code: gc.createArray(),
vars: gc.createArray(),
jumpDestinations: [1],
jumpDestinations: gc.createArray(),

_constants: func._data.constants,
_instructions: func._data.instructions,

getConstant: function (index) {
if (this._constants[index] === null) return;
return this._constants[index];
var val = this._constants[index];
return this._constants[index] === null? void 0 : val;
}
};

state.jumpDestinations.push(1);

// Get code representation of instructions
l = instructions.length / 4;
Expand Down Expand Up @@ -927,9 +912,13 @@
// return 'function(){' + code.join('\n') + '}';
result = 'function(' + paramNames.join() + '){' + code.join('\n') + '}';


gc.collect(code);
gc.collect(paramNames);

gc.collect(state.code);
gc.collect(state.vars);
gc.collect(state.jumpDestinations);

gc.collect(state);

return result;
Expand Down
19 changes: 10 additions & 9 deletions vm/src/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,16 +1152,17 @@


function callR (register, index, c, argStart, argEnd) {
var args, result;
var result, i, limit,
args = createArray();

if (!argStart) {
args = createArray();
} else if (!argEnd) {
args = register.slice(argStart);
} else {
args = register.slice(argStart, argEnd);
if (argStart) {
limit = argEnd? argEnd : register.length;

for (i = argStart; i < limit; i++) {
args.push(register[i]);
}
}

result = call_internal(register[index],args);

register.length = index;
Expand All @@ -1171,7 +1172,7 @@

if (!(result instanceof Array)) {
setR(register, index, result);

} else {
result.unshift(index, 0);
Array.prototype.splice.apply(register, result);
Expand Down

0 comments on commit fc9ffee

Please sign in to comment.