[racket] Understanding future syncronization

From: Eric Dobson (eric.n.dobson at gmail.com)
Date: Mon Dec 31 22:41:35 EST 2012

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.

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.

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?


My (reduced) program:
#lang racket

(define void-t (void))
#;
(define-syntax-rule (my-void arg)
  (begin
    arg
    void-t))
(define my-void void)


(define (run)
  (define N 1000000)
  (define v (box #f))
  (define (f)
    (future
      (thunk
        (for ((i (in-range N)))
          (my-void (box-cas! v (unbox v) i))))))
  (define f1 (f))
  (define f2 (f))
  (touch f1)
  (touch f2)
  (values))



(define (time-it)
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run)))

#|
(time-it)
|#


(require future-visualizer)
(visualize-futures
  (run)
  #f)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121231/ecc9e09b/attachment.html>

Posted on the users mailing list.