{"id":207,"date":"2022-06-04T23:25:27","date_gmt":"2022-06-04T23:25:27","guid":{"rendered":"https:\/\/alexrock.com\/blog\/?p=207"},"modified":"2022-06-04T23:30:44","modified_gmt":"2022-06-04T23:30:44","slug":"mandlebrot-generator","status":"publish","type":"post","link":"https:\/\/alexrock.com\/blog\/archives\/207-mandlebrot-generator.html","title":{"rendered":"Mandlebrot Generator"},"content":{"rendered":"\n<p>I was watching James Sharman&#8217;s video about <a href=\"https:\/\/www.youtube.com\/watch?v=bIj3xBmALxE\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=bIj3xBmALxE\">benchmarking his hombrew CPU<\/a> recently.  In that video he generates a mandlebrot set as a benchmark.  I realized I&#8217;d never written an implementation of mandlebrot, so thought it might be fun.  I probably should have started from scratch, but instead I just copied the <a href=\"https:\/\/github.com\/SlithyMatt\/multi-mandlebrot\/blob\/main\/apple\/asb-mandelbrot.bas\">apple basic version<\/a> from the multi-mandlebrot repo, which was the base of the benchmark (the apple basic one seemed the easiest to understand).<\/p>\n\n\n\n<p>This was my first semi-working attempt.  The code actually takes long enough to run that I was getting script timeouts, so this version does each render loop on a timeout. It made rendering a lot slower, but does prevent the browser from locking up.<\/p>\n\n\n\n<script async=\"\" src=\"\/\/jsfiddle.net\/nynexman4464\/m687d1cj\/1\/embed\/result\/dark\/\"><\/script>\n\n\n\n<p>You&#8217;ll note that this doesn&#8217;t look quite right.  I thought it might have been the low resolution, or the way I did the mapping to canvas.  I messed around with various params, but it took an embarrassingly long time to realized I&#8217;d simply added two values instead of subtracting them.<\/p>\n\n\n\n<script async=\"\" src=\"\/\/jsfiddle.net\/nynexman4464\/sndjha78\/embed\/result\/dark\/\"><\/script>\n\n\n\n<p>This version is still quite slow, however.  It&#8217;s because we are doing a timeout after every loop, which introduces a lot of delay. To speed things up we can instead try do do as much work as possible in one loop, then stop and call ourselves again with a timeout (this allows the browser to run the event loop, giving time to respond to events and avoid the slow script issues).<\/p>\n\n\n\n<script async=\"\" src=\"\/\/jsfiddle.net\/nynexman4464\/m687d1cj\/40\/embed\/result\/dark\/\"><\/script>\n\n\n\n<p>This is reasonably fast, even at higher resolutions &amp; iterations.  However I was curious if we used a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Web_Workers_API\">web worker<\/a> if I could make things even faster.  Since web workers run on their own thread, we don&#8217;t have to worry about locking up the browser and can just run in a single loop.  One downside to the worker is we have to communicate all of our results via <code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Worker\/postMessage\">postMessage<\/a><\/code> which means a copy of each message has to be made.  If we do this for each point it actually ends up being <em>slower<\/em> than our original implementation (presumably due to the overhead of copying).  We could just send all the points when the loop is done, but then we can&#8217;t see any progress.  After a little experimentation sending 50 points at at time seemed a reasonable compromise. (note I had to move off jsfiddle to allow use of the worker)<\/p>\n\n\n\n<iframe loading=\"lazy\" width=\"100%\" height=\"340px\" src=\"\/mandlebrot\">Your browser can&#8217;t do iframes?<\/iframe>\n\n\n\n<p>Perceptually the worker seems slower, however if the &#8220;ramp&#8221; setting is enabled (which re-draws the image with increasingly more iterations) it&#8217;s fairly clear the worker is faster.  It&#8217;s possible we could get even faster using <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Transferable_objects\">transferable objects<\/a> or <code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/OffscreenCanvas\">OffscreenCanvas<\/a><\/code> (not to mention potentially optimizing the algorithm itself) but this &#8220;final&#8221; version is good enough for me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was watching James Sharman&#8217;s video about benchmarking his hombrew CPU recently. In that video he generates a mandlebrot set as a benchmark. I realized I&#8217;d never written an implementation of mandlebrot, so thought it might be fun&#8230;. <a class=\"read-more\" href=\"https:\/\/alexrock.com\/blog\/archives\/207-mandlebrot-generator.html\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":7,"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/posts\/207\/revisions\/216"}],"wp:attachment":[{"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alexrock.com\/blog\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}