dedupe.js (1378B)
1 function str(c) { 2 let s = c; 3 for (let i = 0; i < 30; i++) 4 s += c; 5 ensureLinearString(s); 6 return s; 7 } 8 9 function f() { 10 // Create some slots to write into. 11 const o = {owner: 'short1', s: 'short2'}; 12 13 // Make a tenured rope. 14 const r1 = str("a") + str("b"); 15 gc(); 16 17 // Write the first instance of our duplicate string into one of the slots 18 // (`owner`). This will be scanned first, and entered into deDupSet when 19 // tenured. 20 o.owner = ensureLinearString(str("a") + str("b") + str("c")); 21 22 // Make another rope with identical contents, with a tenured subtree. 23 const r2 = r1 + str("c"); 24 25 // Linearize the new rope, creating a new extensible string and a bunch of 26 // dependent strings replacing the rest of the rope nodes. 27 ensureLinearString(r2); 28 29 // Write the new rope into a slot, so that it will be scanned next during the 30 // minor GC during traceSlots(). 31 o.s = r2; 32 33 // Do a nursery collection. o.owner will be tenured and inserted into 34 // deDupSet. Then o.s aka r2 will be tenured. If things work correctly, r2 35 // will be marked non-deduplicatable because it is the base of a tenured 36 // string r1. If not, it will be deduplicated to o.owner. 37 minorgc(); 38 39 // Extract out that r1 child node. If its base was deduplicated, this will 40 // assert because its chars have been freed. 41 const s1 = r1.substr(0, 31); 42 } 43 44 f();