<p>When I measured it, gettimeofday/clock_gettime took less than a microsecond. Make sure the overhead of having a dedicated thread for time caching doesn&#39;t slow things down. My test was on x86 Linux on some 2009 Intel Xeon.<br>
</p>
<p>Sent from my Android, please excuse the brevity. </p>
<div class="gmail_quote">Am 08.08.2011 14:25 schrieb &quot;Gregory Collins&quot; &lt;<a href="mailto:greg@gregorycollins.net">greg@gregorycollins.net</a>&gt;:<br type="attribution">&gt; Not really. tryTakeMVar and tryPutMVar are pretty heavyweight and involve<br>
&gt; taking a mutex lock:<br>&gt; <br>&gt;     <a href="http://hackage.haskell.org/trac/ghc/browser/rts/PrimOps.cmm#L1440">http://hackage.haskell.org/trac/ghc/browser/rts/PrimOps.cmm#L1440</a><br>&gt; <br>&gt; IORef is much lighter-weight, see e.g.<br>
&gt; <br>&gt;     atomicModifyMutVar#:<br>&gt; <a href="http://hackage.haskell.org/trac/ghc/browser/rts/PrimOps.cmm#L253">http://hackage.haskell.org/trac/ghc/browser/rts/PrimOps.cmm#L253</a><br>&gt; <br>&gt; and<br>&gt; <br>
&gt;     readMutVar# / writeMutVar#:<br>&gt; <a href="http://hackage.haskell.org/trac/ghc/browser/compiler/codeGen/CgPrimOp.hs#L128">http://hackage.haskell.org/trac/ghc/browser/compiler/codeGen/CgPrimOp.hs#L128</a><br>&gt; <br>
&gt; The readMutVar primop just does a read (which turns into a small number of<br>&gt; assembly instructions), and the writeMutVar primop just does a store and<br>&gt; then marks the location dirty in the garbage collector. Doing an<br>
&gt; atomicModifyMutVar# involves a CAS which is also typically cheaper than<br>&gt; mutex locking (you spin-loop instead of doing blocking/wakeup semantics).<br>&gt; <br>&gt; For the Snap date-caching code, we only mess with mutex locks if the date<br>
&gt; thread has been idle for a couple of seconds, in which case we don&#39;t mind<br>&gt; paying the overhead of doing the locking. It&#39;s the &quot;X hundred/thousand QPS&quot;<br>&gt; case we&#39;re trying to optimize.<br>
&gt; <br>&gt; G<br>&gt; <br>&gt; On Sun, Aug 7, 2011 at 12:20 AM, Greg Weber &lt;<a href="mailto:greg@gregweber.info">greg@gregweber.info</a>&gt; wrote:<br>&gt; <br>&gt;&gt; couldn&#39;t tryTakeMVar and tryPutMVar handle this case of multiple writers<br>
&gt;&gt; well? I am asking because I really don&#39;t know :)<br>&gt;&gt;<br>&gt;&gt; On Sat, Aug 6, 2011 at 2:05 PM, Gregory Collins &lt;<a href="mailto:greg@gregorycollins.net">greg@gregorycollins.net</a>&gt;wrote:<br>&gt;&gt;<br>
&gt;&gt;&gt; On Sat, Aug 6, 2011 at 8:11 PM, Michael Snoyman &lt;<a href="mailto:michael@snoyman.com">michael@snoyman.com</a>&gt;wrote:<br>&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt; Without having delved into the issue in any great depth, I had a<br>
&gt;&gt;&gt;&gt; possible idea on this point. How about storing an IORef containing a<br>&gt;&gt;&gt;&gt; time and the text representations you need. Whenever you need to get<br>&gt;&gt;&gt;&gt; the time, compare the time in the IORef with the current time, and if<br>
&gt;&gt;&gt;&gt; they&#39;re different, update appropriately. Bonus points: store in an<br>&gt;&gt;&gt;&gt; unpacked datatype and use a Builder instead of String. Would this (in<br>&gt;&gt;&gt;&gt; theory) work?<br>&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>&gt;&gt;&gt; We do something similar to this, except we get the thread to recompute the<br>&gt;&gt;&gt; date -- otherwise every second you have a race condition possibility where<br>&gt;&gt;&gt; multiple threads compete to write the new thread into the IORef. If there&#39;s<br>
&gt;&gt;&gt; no activity, the date thread goes to sleep, the code to get the current date<br>&gt;&gt;&gt; notices that the date is stale, and then it wakes the thread and recomputes<br>&gt;&gt;&gt; the new date itself. Again, I&#39;m not sure if it&#39;s worth it in the general<br>
&gt;&gt;&gt; case, but during periods of high load I think it helps to get as much stuff<br>&gt;&gt;&gt; out of the processing threads as possible.<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; G<br>&gt;&gt;&gt; --<br>&gt;&gt;&gt; Gregory Collins &lt;<a href="mailto:greg@gregorycollins.net">greg@gregorycollins.net</a>&gt;<br>
&gt;&gt;&gt;<br>&gt;&gt;&gt; _______________________________________________<br>&gt;&gt;&gt; web-devel mailing list<br>&gt;&gt;&gt; <a href="mailto:web-devel@haskell.org">web-devel@haskell.org</a><br>&gt;&gt;&gt; <a href="http://www.haskell.org/mailman/listinfo/web-devel">http://www.haskell.org/mailman/listinfo/web-devel</a><br>
&gt;&gt;&gt;<br>&gt;&gt;&gt;<br>&gt;&gt;<br>&gt; <br>&gt; <br>&gt; -- <br>&gt; Gregory Collins &lt;<a href="mailto:greg@gregorycollins.net">greg@gregorycollins.net</a>&gt;<br></div>