<div dir="ltr">Lock instructions on modern x86 processors aren't really that expensive.  What is expensive is lock contention.  When I've measured something code that does this in a bunch of concurrent threads:<div>  1. acquire_lock()</div><div>  2. do_something_really_small_on_thread_local_data()</div><div>  3. release_lock()</div><div><br></div><div>It costs about 1ns to do step 2 with no locks.</div><div>It costs about 5ns to acquire the lock if the lock is thread-local, and thus not actually contended.</div><div>It costs about 100ns-200ns if the lock is actually contended.</div><div><br></div><div>I've found that these measurements have changed the way I write lock-based code.  For example, I like per-core data structures that need a lock, because the per-core lock is almost always uncontended.  (The difference between per-core and per-thread shows up only when a thread is preempted.)</div><div><br></div><div>-Badley<br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 10, 2015 at 12:16 AM, Daniel Micay <span dir="ltr"><<a href="mailto:danielmicay@gmail.com" target="_blank">danielmicay@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br><br></div></div><span class="">
> FWIW, I also tried to remove all the bin mutexes, and make them all use<br>
> the arena mutex, and, counter-intuitively, it made things faster. Not by<br>
> a very significant margin, though, but it's interesting to note that the<br>
> synchronization overheads of n locks can make things slower than 1<br>
> lock with more contention.<br>
<br>
</span>It's not really surprising. A LOCK prefix on x86 is extremely expensive<br>
so locks without contention still have an enormous cost, as do many of<br>
the atomic operations. An atomic_load() with acquire semantics or an<br>
atomic_store() with release semantics are fast since no LOCK is needed.<br>
<br>
<br>_______________________________________________<br>
jemalloc-discuss mailing list<br>
<a href="mailto:jemalloc-discuss@canonware.com">jemalloc-discuss@canonware.com</a><br>
<a href="http://www.canonware.com/mailman/listinfo/jemalloc-discuss" target="_blank">http://www.canonware.com/mailman/listinfo/jemalloc-discuss</a><br>
<br></blockquote></div><br></div></div></div>