Truly independent web browser
// The feedback the profiling tier records for functions that jit.prepare() moved to it, as jit.feedback() describes
// it: one line per instruction with feedback slots, with what each slot recorded. Loads and calls keep the last value
// they produced, which jit.feedback() folds into the kinds of values its slot saw.
// Without the JIT nothing is profiled, and the harness's repeated runs of a test would add to what the first run
// recorded, so the body runs once.
function profilingTest(name, body) {
let ran = false;
test(name, () => {
if (!jit.enabled || ran) return;
ran = true;
body();
});
}
// What the slots of the first instruction named `name` in the feedback of `f` recorded.
function feedbackOf(f, name) {
const marker = `] ${name} `;
const line = jit
.feedback(f)
.split("\n")
.find(line => line.includes(marker));
return line?.substring(line.indexOf(marker) + marker.length);
}
profilingTest("arithmetic records the kinds of its operands", () => {
function add(a, b) {
return a + b;
}
jit.prepare(add);
add(1, 2);
expect(feedbackOf(add, "Add")).toBe("arith#0: Int32");
add(1, 0.5);
add(0x7fffffff, 1);
expect(feedbackOf(add, "Add")).toBe("arith#0: Int32|Double|Int32Overflow");
add("a", 1);
add(1n, 2n);
expect(feedbackOf(add, "Add")).toBe("arith#0: Int32|Double|Int32Overflow|String|BigInt");
function negate(value) {
return -value;
}
jit.prepare(negate);
negate({});
expect(feedbackOf(negate, "UnaryMinus")).toBe("arith#0: Other");
});
profilingTest("comparisons record the kinds of their operands", () => {
function less(a, b) {
return a < b;
}
function same(a, b) {
return a === b;
}
jit.prepare(less);
jit.prepare(same);
less(1, 2);
less(1.5, 2);
same("a", "a");
expect(feedbackOf(less, "LessThan")).toBe("arith#0: Int32|Double");
expect(feedbackOf(same, "StrictlyEquals")).toBe("arith#0: String");
});
profilingTest("loads and calls record the kinds of the values they produce", () => {
function read(object) {
return object.value;
}
jit.prepare(read);
read({ value: 1 });
expect(feedbackOf(read, "GetById")).toBe("value of cache#0: Int32");
read({ value: "string" });
expect(feedbackOf(read, "GetById")).toBe("value of cache#0: Int32|String");
read({ value: undefined });
expect(feedbackOf(read, "GetById")).toBe("value of cache#0: Int32|String|Undefined");
// Loads record into the slots of their property lookup caches, which stores and the other loads have too.
function readAfterWrites(object) {
object.first = 1;
object.second = 2;
const length = object.length;
return [length, object.value];
}
jit.prepare(readAfterWrites);
readAfterWrites({ value: true, length: 1.5 });
expect(feedbackOf(readAfterWrites, "GetLength")).toBe("value of cache#2: Double keyed#0: keys=none elements=Other");
expect(feedbackOf(readAfterWrites, "GetById")).toBe("value of cache#3: Boolean");
function call(f) {
return f();
}
jit.prepare(call);
// NB: The feedback only holds its targets weakly, so the functions stay alive until the end of the test.
const returnsNull = () => null;
const returnsDouble = () => 1.5;
call(returnsNull);
expect(feedbackOf(call, "Call")).toBe("call#0: target=function returnsNull flags=none returned: Null");
call(returnsDouble);
expect(feedbackOf(call, "Call")).toBe(
"call#0: target=function returnsNull flags=Polymorphic|OtherFunctions returned: Double|Null"
);
expect(returnsDouble()).toBe(1.5);
expect(returnsNull()).toBeNull();
});
profilingTest("calls record their first callee and how the others differ", () => {
function first() {}
function second() {}
function call(f) {
return f();
}
jit.prepare(call);
call(first);
call(first);
expect(feedbackOf(call, "Call")).toBe("call#0: target=function first flags=none returned: Undefined");
call(second);
expect(feedbackOf(call, "Call")).toBe(
"call#0: target=function first flags=Polymorphic|OtherFunctions returned: Undefined"
);
call(Math.random);
expect(feedbackOf(call, "Call")).toBe(
"call#0: target=function first flags=Polymorphic|SawNative|OtherFunctions returned: Double|Undefined"
);
function callClosure(f) {
return f();
}
const makeClosure = value => () => value;
jit.prepare(callClosure);
const closures = [makeClosure(1), makeClosure(2)];
callClosure(closures[0]);
callClosure(closures[1]);
expect(feedbackOf(callClosure, "Call")).toBe(
"call#0: target=function <anonymous> flags=Polymorphic returned: Int32"
);
expect(closures.length).toBe(2);
function construct(C) {
return new C();
}
jit.prepare(construct);
construct(Object);
expect(feedbackOf(construct, "CallConstruct")).toBe(
"call#0: target=native function Object flags=SawNative|SawConstruct returned: Object"
);
});
profilingTest("calls record where their callee forwarded them to", () => {
function target(a, b) {
return a + b;
}
function viaCall(f) {
return f.call(null, 1, 2);
}
function viaApply(f) {
return f.apply(null, [1, 2, 3]);
}
function viaBound(f) {
return f(2);
}
let sum = 0;
function add(value) {
sum += value;
}
function viaCallback(values) {
sum = 0;
values.forEach(add);
return sum;
}
for (const f of [viaCall, viaApply, viaBound, viaCallback]) jit.prepare(f);
viaCall(target);
viaApply(target);
viaBound(target.bind(null, 1));
viaCallback([1, 2]);
expect(feedbackOf(viaCall, "Call")).toBe(
"call#0: target=native function call flags=SawNative forwarded by call with 2 arguments to function target returned: Int32"
);
expect(feedbackOf(viaApply, "Call")).toBe(
"call#0: target=native function apply flags=SawNative forwarded by apply with 3 arguments to function target returned: Int32"
);
expect(feedbackOf(viaBound, "Call")).toBe(
"call#0: target=function target flags=SawNative forwarded by bound with 2 arguments to function target returned: Int32"
);
expect(feedbackOf(viaCallback, "Call")).toBe(
"call#0: target=native function forEach flags=SawNative forwarded by callback with 0 arguments to function add returned: none"
);
function other(a, b) {
return a * b;
}
viaCall(other);
expect(feedbackOf(viaCall, "Call")).toBe(
"call#0: target=native function call flags=SawNative|ForwardedPolymorphic forwarded by call with 2 arguments to function target returned: Int32"
);
// NB: The feedback only holds its targets weakly, so `add` stays alive until here.
expect(add.name).toBe("add");
});
profilingTest("keyed accesses record their keys and elements", () => {
function get(object, key) {
return object[key];
}
jit.prepare(get);
get([1, 2, 3], 1);
expect(feedbackOf(get, "GetByValue")).toBe("value of cache#0: Int32 keyed#0: keys=Int32Index elements=Packed");
get([1, , 3], 1);
expect(feedbackOf(get, "GetByValue")).toBe(
"value of cache#0: Int32|Undefined keyed#0: keys=Int32Index elements=Packed|Holey out-of-bounds"
);
get(new Float64Array(2), 0);
get({ x: 1 }, "x");
expect(feedbackOf(get, "GetByValue")).toBe(
'value of cache#0: Int32|Undefined keyed#0: keys=Int32Index|String elements=Packed|Holey|Other|Float64Array out-of-bounds last_key="x"'
);
get({ y: 1 }, "y");
expect(feedbackOf(get, "GetByValue")).toBe(
'value of cache#0: Int32|Undefined keyed#0: keys=Int32Index|String|MultipleKeys elements=Packed|Holey|Other|Float64Array out-of-bounds last_key="y"'
);
function fill(count) {
const values = new Array(count);
for (let i = 0; i < count; ++i) values[i] = i;
return values;
}
jit.prepare(fill);
fill(4);
expect(feedbackOf(fill, "PutByValue")).toBe("keyed#0: keys=Int32Index elements=Holey out-of-bounds");
});
profilingTest("feedback forgets callees and keys that died", () => {
function call(f) {
return f();
}
function get(object, key) {
return object[key];
}
jit.prepare(call);
jit.prepare(get);
// NB: The feedback only holds its targets weakly, so the holder keeps them alive until it is emptied.
const holder = [];
(() => {
const temporary = function temporary() {};
const symbol = Symbol("temporary");
holder.push(temporary, symbol);
call(temporary);
get({}, symbol);
})();
expect(feedbackOf(call, "Call")).toBe("call#0: target=function temporary flags=none returned: Undefined");
expect(feedbackOf(get, "GetByValue")).toBe(
"value of cache#0: Undefined keyed#0: keys=Symbol elements=Other last_key=Symbol(temporary)"
);
holder.length = 0;
gc();
expect(feedbackOf(call, "Call")).toBe("call#0: target=none flags=none returned: Undefined");
expect(feedbackOf(get, "GetByValue")).toBe("value of cache#0: Undefined keyed#0: keys=Symbol elements=Other");
});
profilingTest("functions that never ran have no feedback", () => {
function neverCalled() {
return 1;
}
expect(jit.feedback(neverCalled)).toBeUndefined();
expect(() => jit.feedback(Math.max)).toThrowWithMessage(TypeError, "Not an ECMAScript function");
});