<div dir="ltr">I am playing around with futures currently, and am trying to understand how to make my programs faster. The future visualizer is wonderful in understanding whats going on, but it lead me down a track which made my program slower.<div>
<br></div><div>The issue is that void primitive needs to synchronize with the main thread. So when I was using it to provide the return value from my function it was synchronizing. I thought that not calling a function and just referencing a module-level constant would solve this synchronization issue. And it did, but it made the program slower.</div>
<div><br></div><div style>What is the rule of thumb for understanding how expensive synchronization is, I assumed it was at least the cost of a couple atomic operations? Also can someone explain why void needs to synchronize with the main thread? </div>
<div><br></div><div><br></div><div style>My (reduced) program:</div><div style><div>#lang racket</div><div><br></div><div>(define void-t (void))</div><div>#;</div><div>(define-syntax-rule (my-void arg)</div><div> (begin</div>
<div> arg</div><div> void-t))</div><div>(define my-void void)</div><div><br></div><div><br></div><div>(define (run)</div><div> (define N 1000000)</div><div> (define v (box #f))</div><div> (define (f)</div><div> (future</div>
<div> (thunk</div><div> (for ((i (in-range N)))</div><div> (my-void (box-cas! v (unbox v) i))))))</div><div> (define f1 (f))</div><div> (define f2 (f))</div><div> (touch f1)</div><div> (touch f2)</div>
<div> (values))</div><div><br></div><div><br></div><div><br></div><div>(define (time-it)</div><div> (collect-garbage)</div><div> (time (run))</div><div> (collect-garbage)</div><div> (time (run))</div><div> (collect-garbage)</div>
<div> (time (run))</div><div> (collect-garbage)</div><div> (time (run))</div><div> (collect-garbage)</div><div> (time (run))</div><div> (collect-garbage)</div><div> (time (run))</div><div> (collect-garbage)</div><div>
(time (run))</div><div> (collect-garbage)</div><div> (time (run))</div><div> (collect-garbage)</div><div> (time (run))</div><div> (collect-garbage)</div><div> (time (run))</div><div> (collect-garbage)</div><div> (time (run)))</div>
<div><br></div><div>#|</div><div>(time-it)</div><div>|#</div><div><br></div><div><br></div><div>(require future-visualizer)</div><div>(visualize-futures</div><div> (run)</div><div> #f)</div><div><br></div></div></div>