<div dir="ltr"><div>Blush. I did miss the - 1, sorry for the noise.</div><div><br></div><div>With regards to yield*, I am sure the engines will continue to improve the performance - it is relatively new, so I assume there might be some low hanging fruit.</div><div>We'll see. I am trying to concentrate on correctness first, I also have a lot of room for optimizations</div><div><br></div><div>Florin<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 7, 2021 at 8:46 PM Vanessa Freudenberg <<a href="mailto:vanessa@codefrau.net">vanessa@codefrau.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"><div><div dir="ltr" class="gmail_attr">On Wed, Apr 7, 2021 at 3:07 PM Florin Mateoc <<a href="mailto:florin.mateoc@gmail.com" target="_blank">florin.mateoc@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"><div>Small update: I can now see the UI, and even interact with it a little (before it breaks with various bugs :)) - woohoo!</div><div></div></div></blockquote></div><div><br></div><div>Awesome!</div><div><br></div><div>And thanks for the bug report! I think you might have missed that SqueakJS does in fact subtract 1 from the frexp() result:</div><div><br></div><div>case 53: return this.popNandPushIntIfOK(argCount+1, this.frexp_exponent(this.stackFloat(0)) - 1);<br></div><div><br></div><div>I'm almost certain it is correct. I wrote a whole blockpost about emulating frexp in JavaScript 7 years ago:</div><div><a href="https://croquetweak.blogspot.com/2014/08/deconstructing-floats-frexp-and-ldexp.html" target="_blank">https://croquetweak.blogspot.com/2014/08/deconstructing-floats-frexp-and-ldexp.html</a><br></div><div><br></div><div>Btw, I determined that yield* is incredibly slow, because (as you were saying) every send now needs to return a generator function. So I had to scrap my initial plan for the new SqueakJS JIT. I'm going with regular function calls now, and reifying the stack via exception unwinding when needed, rather than trying to keep multiple call stacks alive. See this twitter thread: </div><div><a href="https://twitter.com/codefrau/status/1378455847055941635" target="_blank">https://twitter.com/codefrau/status/1378455847055941635</a><br></div><div><br></div><div>Vanessa</div><div><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 7, 2021 at 3:07 PM Florin Mateoc <<a href="mailto:florin.mateoc@gmail.com" target="_blank">florin.mateoc@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"><div>Small update: I can now see the UI, and even interact with it a little (before it breaks with various bugs :)) - woohoo!</div><div><br></div><div>Anyway, while debugging my various bugs, I found a small bug in the SqueakJS primitive 53 - I do consult them regularly :)</div><div>Squeak uses the IEEE 754 specification for exponent (significand between 1 and 2), frexp does not (significand between 0.5 and 1), so the result is off by one.</div><div>It should not matter much, as the significand is computed based on the exponent, but e.g. the code executed when the primitive fails gives a different result from the primitive itself<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 17, 2021 at 5:01 AM Vanessa Freudenberg <<a href="mailto:vanessa@codefrau.net" target="_blank">vanessa@codefrau.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"><div dir="ltr">On Tue, Mar 16, 2021 at 1:38 AM Vanessa Freudenberg <<a href="mailto:vanessa@codefrau.net" target="_blank">vanessa@codefrau.net</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">Meanwhile, I made a little scheduler just to learn how to use yield*. Works nicely:<br></div><div class="gmail_quote"><div><a href="https://codepen.io/codefrau/pen/jOVRmxm" target="_blank">https://codepen.io/codefrau/pen/jOVRmxm</a></div></div></div></blockquote><div><br></div><div>And here is a JIT mockup using that scheduler:</div><div><a href="https://codepen.io/codefrau/pen/LYbopBV" target="_blank">https://codepen.io/codefrau/pen/LYbopBV</a><br></div><div><br></div><div>It's executing "5 benchFib", "4 benchFib", and "3 benchFib" in three separate processes, switching every 3 sends just for testing, and printing all contexts.</div><div><br></div><div>No benchmarks yet.</div><div><br></div><div>Vanessa</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div></div><div><div>Vanessa</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Mon, Mar 15, 2021 at 10:28 PM Vanessa Freudenberg <<a href="mailto:vanessa@codefrau.net" target="_blank">vanessa@codefrau.net</a>> wrote:</blockquote></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"><div>Thanks for that Florin, very helpful!</div><div><br></div><div>I'm curious why you need to make every send into a generator call, and not just rely on the one in your GlobalCheckForInterrupts()?</div><div><br></div><div>My implementation is intended to allow context switching that way:</div><div><br></div><div><pre style="font-family:Menlo,Consolas,monospace;font-size:13.92px;line-height:19.488px;width:915px;overflow-x:auto;border-top:1px solid rgb(204,204,204);border-bottom:1px solid rgb(204,204,204);padding:5px 0px 5px 20px;margin-top:0px;margin-bottom:0px;background-color:rgb(252,252,252);break-inside:avoid"><code style="font-family:Menlo,Consolas,monospace;font-size:12.1104px;line-height:16.9546px;break-inside:avoid"><span><span style="color:rgb(0,34,238)">if</span> (--vm.interruptCheckCounter <= <span style="color:rgb(170,0,204)">0</span> && vm.handleDepthAndInterrupts(depth, thisProxy) === <span style="color:rgb(170,13,145)">true</span>) <span style="color:rgb(0,34,238)">return</span> <span style="color:rgb(170,13,145)">false</span>;</span>
</code></pre><br></div><div>This is the same technique as in other VMs, where the actual check for context switch is not done for every send, but only when the interruptCheckCounter goes below zero. In the first mockup on codepen, vm.handleDepthAndInterrupts prints the contents of all the contexts, proving that the information is accessible in case we need to context switch or reify the stack. My hope was that inside of handleDepthAndInterrupts() I could use *yield to do the actual context switching. </div><div><br></div><div>My second, exception based mockup on codepen uses the same approach, except that when a context switch is needed it would throw, unwinding the stack fully, and creating actual context objects along the way. I have not mocked up that part yet (because it would need a mockup interpreter too, to continue after unwind), but you can see the unwind working correctly by uncommenting the line with</div><div>// throw Error("unwind")<br></div><div><br></div><div>I tried your benchFib vs benchFiby on Chrome, which seems to optimize generators a lot better than Firefox. On Chrome the overhead is just 50% or so, vs 300% on Firefox. Safari appears to be between the two.</div><div><br></div><div>I will need to make more complete mockups before deciding on a design. Especially how closures would be handled. Does anyone have a tiny one-method benchmark that would highlight closure performance?</div><div><br></div><div>Vanessa</div><div><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 15, 2021 at 6:20 PM Florin Mateoc <<a href="mailto:florin.mateoc@gmail.com" target="_blank">florin.mateoc@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"><div>I don't think the numbers could be meaningfully compared. The whole purpose of the yield* invocations is to enable process switching, which I don't think your jitted methods allow for. But then are you merely comparing yield* invocations against direct invocations? For sure, the yield* ones will be  much slower.</div><div>Let alone my implementation, which also uses yield* for #< , #- and #+, so it would be much slower than below as operators are surely well optimized by JS, we can just measure direct vs yield* for the main recursive invocation:</div><div><br></div><div>Number.prototype.benchFib = function benchFib() {<br>  return this < 2 ? 1 : (this - 1).benchFib() + (this - 2).benchFib() + 1<br>}<br><br>var t = performance.now();<br>(30).benchFib();<br>performance.now() - t</div><div><br></div><div>gives on my laptop in Firefox 920, 911, 919, 898</div><div><br></div><div>Versus</div><div><br></div><div>
<span><span><span><span>Number</span>.<span>prototype</span>.<span>benchFiby</span> <span>=</span> <span>function</span><span>*</span> <span>benchFiby</span>() {</span></span></span></div><div><span><span><span>   <span>return</span> <span>this</span> <span><</span> <span>2</span> <span>?</span> <span>1</span> : (<span>yield</span><span>*</span> (<span>this</span> <span>-</span> <span>1</span>).<span>benchFiby</span>()) <span>+</span> (<span>yield</span><span>*</span> (<span>this</span> <span>-</span> <span>2</span>).<span>benchFiby</span>()) <span>+</span> <span>1</span> <br></span></span></span></div><div><span><span><span>}</span></span></span></div><div><span><span><span><br></span></span></span></div><div><span><span><span>var t = performance.now();<br>(30).benchFiby().next();<br>performance.now() - t</span></span></span></div><div><span><span><span><br></span></span></span></div><div><span><span><span>gives 2998, 3125, 3116, 3140<br></span></span></span>

</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 15, 2021 at 3:41 PM Vanessa Freudenberg <<a href="mailto:vanessa@codefrau.net" target="_blank">vanessa@codefrau.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi Florin,</div><div><br></div><div>wow, that looks exciting! </div><div><br></div><div>This is indeed a much more thorough Squeak-to-JS mapping, where mine is more of a "traditional" VM. I love it!</div><div><br></div><div>Since my original post I implemented a mockup of "my" new JIT scheme:</div><div><br></div><div><a href="https://squeak.js.org/docs/jit.md.html#sketch:contextproxieswithintrospectionandinlinecaching/newjitsketch/performanceestimate" target="_blank">https://squeak.js.org/docs/jit.md.html#sketch:contextproxieswithintrospectionandinlinecaching/newjitsketch/performanceestimate</a><br></div><div><img src="cid:ii_kmazl2oj0" alt="image.png" style="margin-right: 0px;" width="562" height="182"><br></div><div><div>You can play with it here: <a href="https://codepen.io/codefrau/pen/JjbmVGw" target="_blank">https://codepen.io/codefrau/pen/JjbmVGw</a></div><div></div></div><div><br></div><div>Could you share the performance numbers you are seeing for your benchFib, in comparison to SqueakJS or my mockup? I am curious if <font face="monospace">yield*</font> is the way to go. </div><div><br></div><div>Thanks for sharing! And congrats on your new job. My progress is slow too, I only work on it some weekends. But then, I'm not in a hurry :)</div><div><br></div><div>Vanessa<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Mar 14, 2021 at 6:18 PM Florin Mateoc <<a href="mailto:florin.mateoc@gmail.com" target="_blank">florin.mateoc@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"><div>Hi Vanessa,</div><div><br></div><div>Sorry for the delay in responding - as somebody who has been inspired by your SqueakJS project, I think I should mention that I am working on a related project, for now tentatively called JsSqueak.</div><div>In addition to the inspiration provided by SqueakJS, it also scratches my longstanding itch about compiling (transpiling) Squeak.</div><div><br></div><div>I hesitated to talk about it, as it is still a work in progress - after small bits and pieces that I worked on over a long period, I had the opportunity to spend a significant and uninterrupted chunk of time on it last summer, when I was unemployed for 3 months, and I was able to make good progress. I was optimistically thinking of releasing a first version before the end of last year, but after I started working on my new job, progress on JsSqueak has slowed down significantly. I must confess that I (and especially my wife) hesitate in recreating that productive unemployed situation :)</div><div><br></div><div>I started with Squeak 4.5 - I already had code transforming Smalltalk code to a form more suitable for translation - and I also started with VMMakerJS-dtl.18 for the plugin generation part. Of course, I had to heavily modify it, since I have to get rid of the stack usage for arguments/receiver and returns.<br></div><div>Both of these big parts are working. I also implemented most numbered primitives by hand - they are inlined at generation time in the methods calling them.<br></div><div>I am also taking advantage of the latest and greatest additions to JavaScript. I am, of course, using classes, and the parallel class-side hierarchy is implemented using statics. To implement green threads/process switching, all translated methods are implemented as generator functions, and all calls are through yield* expressions. The preemption/interrupt check points are inlined. With this, a process switch is achieved by simply yield-ing (in the process/semaphore primitives).<br></div><div>With this, the Integer>>#benchFib method is translated (as a method in Number.prototype, there is one more, simpler, implementation in BigInt) as:</div><div><br></div><div><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">*<span style="color:rgb(122,122,67)">_benchFib</span>() {<br>   <span style="color:rgb(0,51,179)">if </span>(<span style="color:rgb(131,0,145)">Number</span>.<span style="color:rgb(122,122,67)">isSafeInteger</span>(<span style="color:rgb(0,51,179)">this</span>.<span style="color:rgb(122,122,67)">valueOf</span>())) { <span style="color:rgb(140,140,140);font-style:italic">// Effective (inherited or local) source for #benchFib in SmallInteger<br></span><span style="color:rgb(140,140,140);font-style:italic">      /*Handy send-heavy benchmark*/<br></span><span style="color:rgb(140,140,140);font-style:italic">   /*(result // seconds to run) = approx calls per second*/<br></span><span style="color:rgb(140,140,140);font-style:italic">   /* | r t |<br></span><span style="color:rgb(140,140,140);font-style:italic">     t := Time millisecondsToRun: [r := 26 benchFib].<br></span><span style="color:rgb(140,140,140);font-style:italic">     (r // 1000) // t*/<br></span><span style="color:rgb(140,140,140);font-style:italic">   /*138000 on a Mac 8100//100*/<br></span><span style="color:rgb(140,140,140);font-style:italic">   </span><span style="color:rgb(0,51,179)">if </span>(<span style="color:rgb(135,16,148)">GlobalActivationCounter</span>-- < <span style="color:rgb(23,80,235)">0</span>) <span style="color:rgb(0,51,179)">yield</span>* <span style="color:rgb(122,122,67)">GlobalCheckForInterrupts</span>();<br><br>   <span style="color:rgb(0,51,179)">return </span>(<span style="color:rgb(0,51,179)">yield</span>* <span style="color:rgb(0,51,179)">this</span>.<span style="color:rgb(122,122,67)">_lt</span>( <span style="color:rgb(23,80,235)">2</span>)).<span style="color:rgb(135,16,148)">booleanValueOf</span>(<span style="color:rgb(6,125,23)">"questionMark:colon:"</span>) ? (<span style="color:rgb(23,80,235)">1</span>) : (<span style="color:rgb(0,51,179)">yield</span>* (<span style="color:rgb(0,51,179)">yield</span>* (<span style="color:rgb(0,51,179)">yield</span>* (<span style="color:rgb(0,51,179)">yield</span>* <span style="color:rgb(0,51,179)">this</span>.<span style="color:rgb(122,122,67)">_sub</span>( <span style="color:rgb(23,80,235)">1</span>)).<span style="color:rgb(122,122,67)">_benchFib</span>()).<span style="color:rgb(122,122,67)">_add</span>( <span style="color:rgb(0,51,179)">yield</span>* (<span style="color:rgb(0,51,179)">yield</span>* <span style="color:rgb(0,51,179)">this</span>.<span style="color:rgb(122,122,67)">_sub</span>( <span style="color:rgb(23,80,235)">2</span>)).<span style="color:rgb(122,122,67)">_benchFib</span>())).<span style="color:rgb(122,122,67)">_add</span>( <span style="color:rgb(23,80,235)">1</span>));<br>} <span style="color:rgb(0,51,179)">else </span><span style="color:rgb(140,140,140);font-style:italic">// No implementation for #benchFib in Float hierarchy, trigger a DNU<br></span><span style="color:rgb(140,140,140);font-style:italic">      </span><span style="color:rgb(0,51,179)">return yield</span>* <span style="color:rgb(0,51,179)">super</span>.<span style="color:rgb(122,122,67)">_benchFib</span>()<br>}<br><br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">The top-level check for smallIntegers is because both SmallInteger and Float are mapped to Number.<br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">The booleanValueOf call is for implementing the mustBeBoolean machinery (it actually translates directly to DNU, like it is done nowadays in Squeak).<br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">Of course, in Boolean, booleanValueOf is just an alias for valueOf<br><br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">As you can see, though, this is not terribly efficient, but there is room for improvement/optimizations. With more work, in this case, the _lt call could be replaced by the < operator, and even the _sub and _add calls could be optimized, <br>although not completely, since their result can morph into LargeInteger (mapped to BigInt).<br><br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">As hinted above, SmallInteger is mapped to Number (in the safeInteger range), Float is mapped to Number as well, and LargeInteger is mapped to BigInt.<br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">BlockClosure is mapped to Function, Boolean is mapped to Boolean, Character is mapped to String, weak references are implemented via WeakRef. <br>I have briefly considered also doing slightly higher-level mappings, for IdentitySet to Set and IdentityDictionary to Map, but this is not a priority.<br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">The image is serialized sort of like a JavaScript storeString. No processes or contexts though, or rather they are not brought back in on the JavaScript side. Blocks are both stored and loaded.<br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">Non-local returns, unwind blocks, resumable and restartable exceptions are implemented via JavaScript exception handling plus explicit handler block chains associated with the processes.<br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">The "image" starts with the global state loaded, but all processes are started from scratch instead of resumed. A non-UI image is thus trivially started.<br><br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">One major todo left is hooking up the UI/browser. I did take vm.display.browser.js from SqueakJS and adapted the code in order to implement its numbered primitives, but I still have to work through squeak.js from the same to initialize <br>and hook up the display.<br><br></pre><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9pt">Florin<br></pre></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Mar 7, 2021 at 11:17 PM Vanessa Freudenberg <<a href="mailto:vanessa@codefrau.net" target="_blank">vanessa@codefrau.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr">Hi all,<div><br></div><div>ideas for a faster SqueakJS JIT have been swirling around in my head for many years. This weekend I took the time to write some of them down:</div><div><br></div><div><a href="https://squeak.js.org/docs/jit.md.html" target="_blank">https://squeak.js.org/docs/jit.md.html</a><br></div><div><br></div><div>Feedback welcome!</div><div><br></div><div>Vanessa</div></div>
</blockquote></div>
</blockquote></div></div></div></div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div></div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div>