Truly independent web browser
LibJS: Share local snapshots across pending expressions
Shannon Booth5f1013ac0588
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rsindex bd47d983ae3..d88ebb4f80f 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs@@ -739,6 +739,8 @@ pub struct Stream {has_left_entry: bool,/// The operands of a call or an array literal being put together.operands: Vec<Operand>,+ /// The copies that copy_locals_on_stack() made, kept so that it does not allocate each time.+ spare_local_copies: Vec<(Operand, Operand)>,handler: Option<Handler>,/// Where the handler changes, in order, as offsets of the main segment.handler_changes: Vec<(u32, Option<Handler>)>,@@ -1488,18 +1490,20 @@ impl Generator<'_> {}fn copy_local_on_stack(&mut self, local: Operand) {+ let mut copy = None;for index in 0..self.context.stream.values.len() {let value = self.context.stream.values[index];if value.kind == ValKind::LocalTarget {continue;}- if value.a == local {- let copy = self.copy_to_kept_temporary(local);- self.context.stream.values[index].a = copy;- }- if value.b == local {- let copy = self.copy_to_kept_temporary(local);- self.context.stream.values[index].b = copy;+ if value.a == local || value.b == local {+ let copy = *copy.get_or_insert_with(|| self.copy_to_kept_temporary(local));+ if value.a == local {+ self.context.stream.values[index].a = copy;+ }+ if value.b == local {+ self.context.stream.values[index].b = copy;+ }}}}@@ -1517,20 +1521,32 @@ impl Generator<'_> {/// Whatever is about to happen happens in only some of the ways through the code, so copies have to come first.pub(super) fn copy_locals_on_stack(&mut self) {+ let mut copies = core::mem::take(&mut self.context.stream.spare_local_copies);+ copies.clear();for index in 0..self.context.stream.values.len() {let value = self.context.stream.values[index];if value.kind == ValKind::LocalTarget {continue;}- if value.a.is_local() || value.a.is_argument() {- let copy = self.copy_to_kept_temporary(value.a);- self.context.stream.values[index].a = copy;- }- if value.b.is_local() || value.b.is_argument() {- let copy = self.copy_to_kept_temporary(value.b);- self.context.stream.values[index].b = copy;- }+ let a = self.copy_local_before_branch(value.a, &mut copies);+ let b = self.copy_local_before_branch(value.b, &mut copies);+ let value = &mut self.context.stream.values[index];+ value.a = a;+ value.b = b;}+ self.context.stream.spare_local_copies = copies;+ }++ fn copy_local_before_branch(&mut self, operand: Operand, copies: &mut Vec<(Operand, Operand)>) -> Operand {+ if !operand.is_local() && !operand.is_argument() {+ return operand;+ }+ if let Some(&(_, copy)) = copies.iter().find(|&&(local, _)| local == operand) {+ return copy;+ }+ let copy = self.copy_to_kept_temporary(operand);+ copies.push((operand, copy));+ copy}/// Makes `dst` hold `value`, which is either in a temporary that the last instruction wrote or takes a move.diff --git a/Tests/LibJS/Bytecode/expected/shared-local-snapshots.txt b/Tests/LibJS/Bytecode/expected/shared-local-snapshots.txtnew file mode 100644index 00000000000..174c0973275--- /dev/null+++ b/Tests/LibJS/Bytecode/expected/shared-local-snapshots.txt@@ -0,0 +1,71 @@+$3e68fc01 shared-local-snapshots.js:13:1+ Registers: 7+ Blocks: 1+ Constants:+ [0] = Undefined+ [1] = Int32(1)+ [2] = Int32(2)+ [3] = Bool(true)++block0:+ [ 0] Enter+ [ 1] Mov dst:reg5, src:Undefined+ [ 4] GetGlobal value_feedback:0, dst:reg6, `beforeWrite`+ [ c] Call call_feedback:0, dst:reg5, callee:reg6, this_value:Undefined, arguments:[Int32(1)]+ [ 14] GetGlobal value_feedback:1, dst:reg6, `beforeBranch`+ [ 1c] Call call_feedback:1, dst:reg5, callee:reg6, this_value:Undefined, arguments:[Int32(1), Int32(2), Bool(true)]+ [ 26] End value:reg5+++beforeWrite$048e9e9c shared-local-snapshots.js:6:5+ Registers: 8+ Blocks: 1+ Constants:+ [0] = Undefined++block0:+ [ 0] Enter+ [ 1] GetGlobal value_feedback:0, dst:reg5, `sink`+ [ 9] Mov dst:reg6, src:arg0+ [ c] PostfixIncrement arith_feedback:0, dst:reg7, src:arg0+ [ 11] Call call_feedback:0, dst:reg7, callee:reg5, this_value:Undefined, arguments:[reg6, reg6, reg6, reg7]+ [ 1c] Return value:reg7+++sink$9478f1ad shared-local-snapshots.js:1:15+ Registers: 5+ Blocks: 1++block0:+ [ 0] Enter+ [ 1] CreateRestParams dst:arg0, rest_index:0+ [ 7] Return value:arg0+++beforeBranch$861fa17b shared-local-snapshots.js:10:5+ Registers: 9+ Blocks: 4+ Constants:+ [0] = Undefined+ [1] = Int32(2)+ [2] = Int32(3)++block0:+ [ 0] Enter+ [ 1] GetGlobal value_feedback:0, dst:reg5, `sink`+ [ 9] Mov dst:reg6, src:arg0+ [ c] Mov dst:reg7, src:arg1+ [ f] JumpFalse condition:arg2, target:block2++block1:+ [ 15] Mov dst:arg0, src:Int32(2)+ [ 18] Mov dst:reg8, src:arg0+ [ 1b] Jump target:block3++block2:+ [ 20] Mov dst:arg1, src:Int32(3)+ [ 23] Mov dst:reg8, src:arg1++block3:+ [ 26] Call call_feedback:0, dst:reg8, callee:reg5, this_value:Undefined, arguments:[reg6, reg7, reg6, reg7, reg8]+ [ 32] Return value:reg8diff --git a/Tests/LibJS/Bytecode/input/shared-local-snapshots.js b/Tests/LibJS/Bytecode/input/shared-local-snapshots.jsnew file mode 100644index 00000000000..f3236f17706--- /dev/null+++ b/Tests/LibJS/Bytecode/input/shared-local-snapshots.js@@ -0,0 +1,14 @@+function sink(...values) {+ return values;+}++function beforeWrite(x) {+ return sink(x, x, x, x++);+}++function beforeBranch(x, y, flag) {+ return sink(x, y, x, y, flag ? (x = 2) : (y = 3));+}++beforeWrite(1);+beforeBranch(1, 2, true);diff --git a/Tests/LibJS/Runtime/regress/shared-local-snapshots.js b/Tests/LibJS/Runtime/regress/shared-local-snapshots.jsnew file mode 100644index 00000000000..e760f5fc3d6--- /dev/null+++ b/Tests/LibJS/Runtime/regress/shared-local-snapshots.js@@ -0,0 +1,76 @@+function take(...values) {+ return values;+}++test("each write takes a fresh shared snapshot", () => {+ function repeatedWrites(x) {+ return take(x, x, (x = 2), x, x, (x = 3), x);+ }+ expect(repeatedWrites(1)).toEqual([1, 1, 2, 2, 2, 3, 3]);++ function rawObject() {+ let value = { valueOf: () => 4 };+ const original = value;+ const result = take(value, value, value++);+ return [result[0] === original, result[1] === original, result[2], value];+ }+ expect(rawObject()).toEqual([true, true, 4, 5]);+});++test("shared snapshots preserve callees, receivers, and assignment targets", () => {+ function callee() {+ let fn = function old(a, b, c) {+ return [a === old, b === old, c.name];+ };+ return fn(fn, fn, (fn = function newer() {}));+ }+ expect(callee()).toEqual([true, true, "newer"]);++ function receiver(flag) {+ let base = {+ name: "old",+ method(a, b) {+ return [this.name, a.name, b.name];+ },+ };+ return base.method(base, flag ? (base = { name: "new" }) : base);+ }+ expect(receiver(true)).toEqual(["old", "old", "new"]);+ expect(receiver(false)).toEqual(["old", "old", "old"]);++ function indexedTarget() {+ const object = {};+ let key = "x";+ const result = take(key, (object[key] = key = "y"), key);+ return [result, object.x, Object.hasOwn(object, "y")];+ }+ expect(indexedTarget()).toEqual([["x", "y", "y"], "y", false]);+});++test("shared snapshots survive held registers and distinguish locals from arguments", () => {+ function classKey(flag, x) {+ const result = take(+ x,+ x,+ class {+ [flag ? (x = 2) : (x = 3)]() {}+ },+ x+ );+ return [result[0], result[1], Object.hasOwn(result[2].prototype, flag ? "2" : "3"), result[3]];+ }+ expect(classKey(true, 1)).toEqual([1, 1, true, 2]);+ expect(classKey(false, 1)).toEqual([1, 1, true, 3]);++ function pattern(x) {+ return take(x, x, ([x] = [7]), x);+ }+ expect(pattern(1)).toEqual([1, 1, [7], 7]);++ function distinct(argument, flag) {+ let local = 2;+ return take(argument, local, argument, local, flag ? ((argument = 3), (local = 4)) : null, argument, local);+ }+ expect(distinct(1, true)).toEqual([1, 2, 1, 2, 4, 3, 4]);+ expect(distinct(1, false)).toEqual([1, 2, 1, 2, null, 1, 2]);+});