Skip to content

Commit

Permalink
fix #249, cache the chunk headers info to +5% or +10% performance. 2.…
Browse files Browse the repository at this point in the history
…0.51
  • Loading branch information
winlinvip committed Dec 4, 2014
1 parent b84e878 commit f57801e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions trunk/src/core/srs_core_performance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

46 changes: 38 additions & 8 deletions trunk/src/rtmp/srs_protocol_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions trunk/src/rtmp/srs_protocol_stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ class SrsProtocol
*/
std::map<int, SrsChunkStream*> 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;
Expand Down

0 comments on commit f57801e

Please sign in to comment.