Skip to content

Instantly share code, notes, and snippets.

@dherman
Created November 7, 2012 17:26
Show Gist options
  • Save dherman/4033068 to your computer and use it in GitHub Desktop.
Save dherman/4033068 to your computer and use it in GitHub Desktop.
function ParallelArray(buf) {
this.buffer = buf;
}
ParallelArray.prototype.get = function (i) {
IC1: var v = this.buffer[i];
return v;
}
function MakeOrGetWrapperFromKey(f) {
// compute start and end somehow
return %Clone(f, function (buffer) { // pretend f is the key
for (var i = start; i < end; i++) {
IC2: buffer[i] = f(i);
}
});
}
ParallelArray.prototype.map = function (f) {
var newBuffer = %NewTypeKeyedToScriptPC([]);
var kernelWrapper = MakeOrGetWrapperFromKey(f);
return new ParallelArray(%Fill(newBuffer, kernelWrapper));
}
var pa = new ParallelArray([1,2,3]);
// these create new clones, so there will be 2 copies of IC2, each will be specialized to the buffer as it sees it in this execution
var pa2 = pa.map(function f(x) { return x+1; });
var pa3 = pa2.map(function f(x) { return x-1; });
// these all access the same get function, so there's only one copy of IC1 to record all the types of the buffers seen. it eventually overflows and deopts.
pa.get(0);
pa2.get(0);
pa3.get(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment