From f57801eb46c16755b173984b915a4166922df6a6 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 4 Dec 2014 14:58:40 +0800 Subject: [PATCH] fix #249, cache the chunk headers info to +5% or +10% performance. 2.0.51 --- trunk/src/core/srs_core.hpp | 2 +- trunk/src/core/srs_core_performance.hpp | 7 ++++ trunk/src/rtmp/srs_protocol_stack.cpp | 46 ++++++++++++++++++++----- trunk/src/rtmp/srs_protocol_stack.hpp | 6 ++++ 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 0a67aa8f9a..28bc0c07eb 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR 2 #define VERSION_MINOR 0 -#define VERSION_REVISION 50 +#define VERSION_REVISION 51 // server info. #define RTMP_SIG_SRS_KEY "SRS" #define RTMP_SIG_SRS_ROLE "origin/edge server" diff --git a/trunk/src/core/srs_core_performance.hpp b/trunk/src/core/srs_core_performance.hpp index f07e2bd375..c963a61fb0 100644 --- a/trunk/src/core/srs_core_performance.hpp +++ b/trunk/src/core/srs_core_performance.hpp @@ -79,5 +79,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #define SRS_PERF_SEND_MSGS_CACHE 500 +/** +* how many chunk stream to cache, [0, N]. +* to imporove about 10% performance when chunk size small, and 5% for large chunk. +* @see https://github.com/winlinvip/simple-rtmp-server/issues/249 +*/ +#define SRS_PERF_CHUNK_STREAM_CACHE 16 + #endif diff --git a/trunk/src/rtmp/srs_protocol_stack.cpp b/trunk/src/rtmp/srs_protocol_stack.cpp index 23fdfee926..03d9e5e7de 100644 --- a/trunk/src/rtmp/srs_protocol_stack.cpp +++ b/trunk/src/rtmp/srs_protocol_stack.cpp @@ -417,6 +417,16 @@ SrsProtocol::SrsProtocol(ISrsProtocolReaderWriter* io) warned_c0c3_cache_dry = false; auto_response_when_recv = true; + + cs_cache = new SrsChunkStream*[SRS_PERF_CHUNK_STREAM_CACHE]; + for (int cid = 0; cid < SRS_PERF_CHUNK_STREAM_CACHE; cid++) { + SrsChunkStream* cs = new SrsChunkStream(cid); + // set the perfer cid of chunk, + // which will copy to the message received. + cs->header.perfer_cid = cid; + + cs_cache[cid] = cs; + } } SrsProtocol::~SrsProtocol() @@ -448,6 +458,13 @@ SrsProtocol::~SrsProtocol() free(out_iovs); out_iovs = NULL; } + + // free all chunk stream cache. + for (int i = 0; i < SRS_PERF_CHUNK_STREAM_CACHE; i++) { + SrsChunkStream* cs = cs_cache[i]; + srs_freep(cs); + } + srs_freep(cs_cache); } void SrsProtocol::set_auto_response(bool v) @@ -1102,17 +1119,30 @@ int SrsProtocol::recv_interlaced_message(SrsMessage** pmsg) // get the cached chunk stream. SrsChunkStream* chunk = NULL; - if (chunk_streams.find(cid) == chunk_streams.end()) { - chunk = chunk_streams[cid] = new SrsChunkStream(cid); - // set the perfer cid of chunk, - // which will copy to the message received. - chunk->header.perfer_cid = cid; - srs_verbose("cache new chunk stream: fmt=%d, cid=%d", fmt, cid); - } else { - chunk = chunk_streams[cid]; + // use chunk stream cache to get the chunk info. + // @see https://github.com/winlinvip/simple-rtmp-server/issues/249 + if (cid < SRS_PERF_CHUNK_STREAM_CACHE) { + // chunk stream cache hit. + srs_verbose("cs-cache hit, cid=%d", cid); + // already init, use it direclty + chunk = cs_cache[cid]; srs_verbose("cached chunk stream: fmt=%d, cid=%d, size=%d, message(type=%d, size=%d, time=%"PRId64", sid=%d)", chunk->fmt, chunk->cid, (chunk->msg? chunk->msg->size : 0), chunk->header.message_type, chunk->header.payload_length, chunk->header.timestamp, chunk->header.stream_id); + } else { + // chunk stream cache miss, use map. + if (chunk_streams.find(cid) == chunk_streams.end()) { + chunk = chunk_streams[cid] = new SrsChunkStream(cid); + // set the perfer cid of chunk, + // which will copy to the message received. + chunk->header.perfer_cid = cid; + srs_verbose("cache new chunk stream: fmt=%d, cid=%d", fmt, cid); + } else { + chunk = chunk_streams[cid]; + srs_verbose("cached chunk stream: fmt=%d, cid=%d, size=%d, message(type=%d, size=%d, time=%"PRId64", sid=%d)", + chunk->fmt, chunk->cid, (chunk->msg? chunk->msg->size : 0), chunk->header.message_type, chunk->header.payload_length, + chunk->header.timestamp, chunk->header.stream_id); + } } // chunk stream message header diff --git a/trunk/src/rtmp/srs_protocol_stack.hpp b/trunk/src/rtmp/srs_protocol_stack.hpp index 49f332b005..026c755dc6 100644 --- a/trunk/src/rtmp/srs_protocol_stack.hpp +++ b/trunk/src/rtmp/srs_protocol_stack.hpp @@ -205,6 +205,12 @@ class SrsProtocol */ std::map chunk_streams; /** + * cache some frequently used chunk header. + * cs_cache, the chunk stream cache. + * @see https://github.com/winlinvip/simple-rtmp-server/issues/249 + */ + SrsChunkStream** cs_cache; + /** * bytes buffer cache, recv from skt, provide services for stream. */ SrsFastBuffer* in_buffer;