1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

fix #249, cache the chunk headers info to +5% or +10% performance. 2.0.51

This commit is contained in:
winlin 2014-12-04 14:58:40 +08:00
parent b84e87845e
commit f57801eb46
4 changed files with 52 additions and 9 deletions

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -205,6 +205,12 @@ private:
*/
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;