mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] replace a mfence instruction by an xchg instruction
@ 2015-08-15  6:51 Jens Gustedt
  2015-08-15 20:17 ` Rich Felker
  2015-08-16 16:28 ` Rich Felker
  0 siblings, 2 replies; 15+ messages in thread
From: Jens Gustedt @ 2015-08-15  6:51 UTC (permalink / raw)
  To: musl

according to the wisdom of the Internet, e.g

https://peeterjoot.wordpress.com/2009/12/04/intel-memory-ordering-fence-instructions-and-atomic-operations/

a mfence instruction is about 3 times slower than an xchg instruction.

Here we not only had mfence but also the mov instruction that was to be
protected by the fence. Replace all that by a native atomic instruction
that gives all the ordering guarantees that we need.

This a_store function is performance critical for the __lock
primitive. In my benchmarks to test my stdatomic implementation I have a
substantial performance increase (more than 10%), just because malloc
does better with it.
---
 arch/x32/atomic.h    | 4 ++--
 arch/x86_64/atomic.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/x32/atomic.h b/arch/x32/atomic.h
index 2ab1f7a..3a2f391 100644
--- a/arch/x32/atomic.h
+++ b/arch/x32/atomic.h
@@ -81,9 +81,9 @@ static inline void a_dec(volatile int *x)
 	__asm__( "lock ; decl %0" : "=m"(*x) : "m"(*x) : "memory" );
 }
 
-static inline void a_store(volatile int *p, int x)
+static inline void a_store(volatile int *x, int v)
 {
-	__asm__( "mov %1, %0 ; mfence" : "=m"(*p) : "r"(x) : "memory" );
+	__asm__( "xchg %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory" );
 }
 
 static inline void a_spin()
diff --git a/arch/x86_64/atomic.h b/arch/x86_64/atomic.h
index 2ab1f7a..3a2f391 100644
--- a/arch/x86_64/atomic.h
+++ b/arch/x86_64/atomic.h
@@ -81,9 +81,9 @@ static inline void a_dec(volatile int *x)
 	__asm__( "lock ; decl %0" : "=m"(*x) : "m"(*x) : "memory" );
 }
 
-static inline void a_store(volatile int *p, int x)
+static inline void a_store(volatile int *x, int v)
 {
-	__asm__( "mov %1, %0 ; mfence" : "=m"(*p) : "r"(x) : "memory" );
+	__asm__( "xchg %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory" );
 }
 
 static inline void a_spin()
-- 
2.1.4



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2015-08-16 18:30 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-15  6:51 [PATCH] replace a mfence instruction by an xchg instruction Jens Gustedt
2015-08-15 20:17 ` Rich Felker
2015-08-15 21:01   ` Jens Gustedt
2015-08-15 23:28     ` Rich Felker
2015-08-16 12:42       ` Jens Gustedt
2015-08-16 15:16         ` Rich Felker
2015-08-16 15:50           ` Jens Gustedt
2015-08-16 15:58             ` Rich Felker
2015-08-16 16:16               ` Jens Gustedt
2015-08-16 18:30                 ` Rich Felker
2015-08-16 16:07         ` Rich Felker
2015-08-16 16:34           ` Jens Gustedt
2015-08-16 16:28 ` Rich Felker
2015-08-16 16:38   ` Jens Gustedt
2015-08-16 17:00     ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).