From fa9aeeb3b14e68d2becc8091e219f6c092561240 Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Mon, 14 Dec 2009 09:46:04 +0100 Subject: [PATCH 1/5] RemotePlugin: added branch prediction hints Added some branch prediction hints, especially to ShmFifo locking functions. --- include/RemotePlugin.h | 14 +++++++------- src/core/RemotePlugin.cpp | 15 +++++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/include/RemotePlugin.h b/include/RemotePlugin.h index 12ad00026..b4c112ef1 100644 --- a/include/RemotePlugin.h +++ b/include/RemotePlugin.h @@ -284,7 +284,7 @@ public: // recursive lock inline void lock() { - if( !isInvalid() && ++m_lockDepth == 1 ) + if( likely( !isInvalid() && ++m_lockDepth == 1 ) ) { #ifdef USE_QT_SEMAPHORES m_dataSem.acquire(); @@ -297,9 +297,9 @@ public: // recursive unlock inline void unlock() { - if( m_lockDepth > 0 ) + if( likely( m_lockDepth > 0 ) ) { - if( --m_lockDepth == 0 ) + if( likely( --m_lockDepth == 0 ) ) { #ifdef USE_QT_SEMAPHORES m_dataSem.release(); @@ -313,7 +313,7 @@ public: // wait until message-semaphore is available inline void waitForMessage() { - if( !isInvalid() ) + if( likely( !isInvalid() ) ) { #ifdef USE_QT_SEMAPHORES m_messageSem.acquire(); @@ -372,7 +372,7 @@ public: inline bool messagesLeft() { - if( isInvalid() ) + if( unlikely( isInvalid() ) ) { return false; } @@ -748,7 +748,7 @@ public: inline void lock() { - if( !isInvalid() ) + if( likely( !isInvalid() ) ) { m_commMutex.lock(); } @@ -756,7 +756,7 @@ public: inline void unlock() { - if( !isInvalid() ) + if( likely( !isInvalid() ) ) { m_commMutex.unlock(); } diff --git a/src/core/RemotePlugin.cpp b/src/core/RemotePlugin.cpp index a711d27cd..b60e386c8 100644 --- a/src/core/RemotePlugin.cpp +++ b/src/core/RemotePlugin.cpp @@ -150,7 +150,7 @@ bool RemotePlugin::process( const sampleFrame * _in_buf, { const fpp_t frames = engine::getMixer()->framesPerPeriod(); - if( m_failed || !isRunning() ) + if( unlikely( m_failed || !isRunning() ) ) { if( _out_buf != NULL ) { @@ -160,7 +160,7 @@ bool RemotePlugin::process( const sampleFrame * _in_buf, return false; } - if( m_shm == NULL ) + if( unlikely( m_shm == NULL ) ) { // m_shm being zero means we didn't initialize everything so // far so process one message each time (and hope we get @@ -227,8 +227,7 @@ bool RemotePlugin::process( const sampleFrame * _in_buf, waitForMessage( IdProcessingDone ); unlock(); - const ch_cnt_t outputs = qMin( m_outputCount, - DEFAULT_CHANNELS ); + const ch_cnt_t outputs = qMin( m_outputCount, DEFAULT_CHANNELS ); if( m_splitChannels ) { for( ch_cnt_t ch = 0; ch < outputs; ++ch ) @@ -328,7 +327,7 @@ void RemotePlugin::resizeSharedProcessingMemory() bool RemotePlugin::processMessage( const message & _m ) { lock(); - message reply_message( _m.id ); + message replyMessage( _m.id ); bool reply = false; switch( _m.id ) { @@ -341,12 +340,12 @@ bool RemotePlugin::processMessage( const message & _m ) case IdSampleRateInformation: reply = true; - reply_message.addInt( engine::getMixer()->processingSampleRate() ); + replyMessage.addInt( engine::getMixer()->processingSampleRate() ); break; case IdBufferSizeInformation: reply = true; - reply_message.addInt( engine::getMixer()->framesPerPeriod() ); + replyMessage.addInt( engine::getMixer()->framesPerPeriod() ); break; case IdChangeInputCount: @@ -371,7 +370,7 @@ bool RemotePlugin::processMessage( const message & _m ) } if( reply ) { - sendMessage( reply_message ); + sendMessage( replyMessage ); } unlock(); From ab8f2ab7c7da44e8415563afeb385e15ec54ecb5 Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Mon, 14 Dec 2009 10:50:21 +0100 Subject: [PATCH 2/5] RemotePlugin: smaller buffers for number to string conversions There's no need for 128 bytes when converting a number to a string. Use 64 bytes instead. --- include/RemotePlugin.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/RemotePlugin.h b/include/RemotePlugin.h index b4c112ef1..900c8974e 100644 --- a/include/RemotePlugin.h +++ b/include/RemotePlugin.h @@ -547,7 +547,7 @@ public: message & addInt( int _i ) { - char buf[128]; + char buf[64]; buf[0] = 0; sprintf( buf, "%d", _i ); data.push_back( std::string( buf ) ); @@ -556,7 +556,7 @@ public: message & addFloat( float _f ) { - char buf[128]; + char buf[64]; buf[0] = 0; sprintf( buf, "%f", _f ); data.push_back( std::string( buf ) ); From 7b410bb52d0ca4b561587b4c6ef7c8028c57938a Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Mon, 14 Dec 2009 10:58:14 +0100 Subject: [PATCH 3/5] RemotePlugin: coding style fixes + inline keyword reduces Fixed some minor coding style issues and removed some superfluous "inline" keywords. --- include/RemotePlugin.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/include/RemotePlugin.h b/include/RemotePlugin.h index 900c8974e..3294f54d2 100644 --- a/include/RemotePlugin.h +++ b/include/RemotePlugin.h @@ -617,16 +617,16 @@ public: message waitForMessage( const message & _m, - bool _busy_waiting = false ); + bool _busyWaiting = false ); - inline message fetchAndProcessNextMessage() + message fetchAndProcessNextMessage() { message m = receiveMessage(); processMessage( m ); return m; } - inline void fetchAndProcessAllMessages() + void fetchAndProcessAllMessages() { while( messagesLeft() ) { @@ -708,10 +708,9 @@ public: #endif } - inline void waitForInitDone( bool _busy_waiting = true ) + inline void waitForInitDone( bool _busyWaiting = true ) { - m_failed = waitForMessage( IdInitDone, - _busy_waiting ).id != IdInitDone; + m_failed = waitForMessage( IdInitDone, _busyWaiting ).id != IdInitDone; } virtual bool processMessage( const message & _m ); @@ -1028,7 +1027,7 @@ RemotePluginClient::~RemotePluginClient() bool RemotePluginClient::processMessage( const message & _m ) { - message reply_message( _m.id ); + message replyMessage( _m.id ); bool reply = false; switch( _m.id ) { @@ -1060,7 +1059,7 @@ bool RemotePluginClient::processMessage( const message & _m ) case IdStartProcessing: doProcessing(); - reply_message.id = IdProcessingDone; + replyMessage.id = IdProcessingDone; reply = true; break; @@ -1078,7 +1077,7 @@ bool RemotePluginClient::processMessage( const message & _m ) } if( reply ) { - sendMessage( reply_message ); + sendMessage( replyMessage ); } return true; From 6abba395ca46a605b62ca77f5a6994aec0b25158 Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Mon, 14 Dec 2009 11:05:27 +0100 Subject: [PATCH 4/5] RemotePlugin: better code order in sendMessage() + cleanups We can cache the number of data sets in the message in order to avoid duplicate data::size() calls. Furthermore the accumulation of the size of the written data is an old relict and can be safely removed. --- include/RemotePlugin.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/RemotePlugin.h b/include/RemotePlugin.h index 3294f54d2..1fef44622 100644 --- a/include/RemotePlugin.h +++ b/include/RemotePlugin.h @@ -922,14 +922,13 @@ RemotePluginBase::~RemotePluginBase() void RemotePluginBase::sendMessage( const message & _m ) { + const int n = _m.data.size(); m_out->lock(); m_out->writeInt( _m.id ); - m_out->writeInt( _m.data.size() ); - int j = 0; - for( unsigned int i = 0; i < _m.data.size(); ++i ) + m_out->writeInt( n ); + for( unsigned int i = 0; i < n; ++i ) { m_out->writeString( _m.data[i] ); - j += _m.data[i].size(); } m_out->unlock(); m_out->messageSent(); From 783913e79c0e5a77461a1ac99b0a3fba8498fe0b Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Mon, 14 Dec 2009 11:10:30 +0100 Subject: [PATCH 5/5] RemotePlugin: sleep in waitForMessage() when actively waiting for data RemotePlugin::waitForMessage() can operate in polling mode which is used when loading a remote plugin so the master application does not block until the remote plugin has been loaded. This is achieved by calling QCoreApplication::processEvents(). However if there're no events to process, the polling loop will just eat up CPU time which is especially bad when on a single core system. This has been fixed by inserting short sleeps. --- include/RemotePlugin.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/RemotePlugin.h b/include/RemotePlugin.h index 1fef44622..9b8710d17 100644 --- a/include/RemotePlugin.h +++ b/include/RemotePlugin.h @@ -36,6 +36,7 @@ #include #ifdef LMMS_BUILD_WIN32 +#include #define USE_QT_SEMAPHORES #define USE_QT_SHMEM #endif @@ -966,6 +967,11 @@ RemotePluginBase::message RemotePluginBase::waitForMessage( { QCoreApplication::processEvents( QEventLoop::ExcludeUserInputEvents, 50 ); +#ifdef LMMS_BUILD_WIN32 + Sleep( 5 ); +#else + usleep( 5 * 1000 ); +#endif continue; } #endif