Skip to content

Commit f57801e

Browse files
committed
fix #249, cache the chunk headers info to +5% or +10% performance. 2.0.51
1 parent b84e878 commit f57801e

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

trunk/src/core/srs_core.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131
// current release version
3232
#define VERSION_MAJOR 2
3333
#define VERSION_MINOR 0
34-
#define VERSION_REVISION 50
34+
#define VERSION_REVISION 51
3535
// server info.
3636
#define RTMP_SIG_SRS_KEY "SRS"
3737
#define RTMP_SIG_SRS_ROLE "origin/edge server"

trunk/src/core/srs_core_performance.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7979
*/
8080
#define SRS_PERF_SEND_MSGS_CACHE 500
8181

82+
/**
83+
* how many chunk stream to cache, [0, N].
84+
* to imporove about 10% performance when chunk size small, and 5% for large chunk.
85+
* @see https://github.com/winlinvip/simple-rtmp-server/issues/249
86+
*/
87+
#define SRS_PERF_CHUNK_STREAM_CACHE 16
88+
8289
#endif
8390

trunk/src/rtmp/srs_protocol_stack.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,16 @@ SrsProtocol::SrsProtocol(ISrsProtocolReaderWriter* io)
417417

418418
warned_c0c3_cache_dry = false;
419419
auto_response_when_recv = true;
420+
421+
cs_cache = new SrsChunkStream*[SRS_PERF_CHUNK_STREAM_CACHE];
422+
for (int cid = 0; cid < SRS_PERF_CHUNK_STREAM_CACHE; cid++) {
423+
SrsChunkStream* cs = new SrsChunkStream(cid);
424+
// set the perfer cid of chunk,
425+
// which will copy to the message received.
426+
cs->header.perfer_cid = cid;
427+
428+
cs_cache[cid] = cs;
429+
}
420430
}
421431

422432
SrsProtocol::~SrsProtocol()
@@ -448,6 +458,13 @@ SrsProtocol::~SrsProtocol()
448458
free(out_iovs);
449459
out_iovs = NULL;
450460
}
461+
462+
// free all chunk stream cache.
463+
for (int i = 0; i < SRS_PERF_CHUNK_STREAM_CACHE; i++) {
464+
SrsChunkStream* cs = cs_cache[i];
465+
srs_freep(cs);
466+
}
467+
srs_freep(cs_cache);
451468
}
452469

453470
void SrsProtocol::set_auto_response(bool v)
@@ -1102,17 +1119,30 @@ int SrsProtocol::recv_interlaced_message(SrsMessage** pmsg)
11021119
// get the cached chunk stream.
11031120
SrsChunkStream* chunk = NULL;
11041121

1105-
if (chunk_streams.find(cid) == chunk_streams.end()) {
1106-
chunk = chunk_streams[cid] = new SrsChunkStream(cid);
1107-
// set the perfer cid of chunk,
1108-
// which will copy to the message received.
1109-
chunk->header.perfer_cid = cid;
1110-
srs_verbose("cache new chunk stream: fmt=%d, cid=%d", fmt, cid);
1111-
} else {
1112-
chunk = chunk_streams[cid];
1122+
// use chunk stream cache to get the chunk info.
1123+
// @see https://github.com/winlinvip/simple-rtmp-server/issues/249
1124+
if (cid < SRS_PERF_CHUNK_STREAM_CACHE) {
1125+
// chunk stream cache hit.
1126+
srs_verbose("cs-cache hit, cid=%d", cid);
1127+
// already init, use it direclty
1128+
chunk = cs_cache[cid];
11131129
srs_verbose("cached chunk stream: fmt=%d, cid=%d, size=%d, message(type=%d, size=%d, time=%"PRId64", sid=%d)",
11141130
chunk->fmt, chunk->cid, (chunk->msg? chunk->msg->size : 0), chunk->header.message_type, chunk->header.payload_length,
11151131
chunk->header.timestamp, chunk->header.stream_id);
1132+
} else {
1133+
// chunk stream cache miss, use map.
1134+
if (chunk_streams.find(cid) == chunk_streams.end()) {
1135+
chunk = chunk_streams[cid] = new SrsChunkStream(cid);
1136+
// set the perfer cid of chunk,
1137+
// which will copy to the message received.
1138+
chunk->header.perfer_cid = cid;
1139+
srs_verbose("cache new chunk stream: fmt=%d, cid=%d", fmt, cid);
1140+
} else {
1141+
chunk = chunk_streams[cid];
1142+
srs_verbose("cached chunk stream: fmt=%d, cid=%d, size=%d, message(type=%d, size=%d, time=%"PRId64", sid=%d)",
1143+
chunk->fmt, chunk->cid, (chunk->msg? chunk->msg->size : 0), chunk->header.message_type, chunk->header.payload_length,
1144+
chunk->header.timestamp, chunk->header.stream_id);
1145+
}
11161146
}
11171147

11181148
// chunk stream message header

trunk/src/rtmp/srs_protocol_stack.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ class SrsProtocol
205205
*/
206206
std::map<int, SrsChunkStream*> chunk_streams;
207207
/**
208+
* cache some frequently used chunk header.
209+
* cs_cache, the chunk stream cache.
210+
* @see https://github.com/winlinvip/simple-rtmp-server/issues/249
211+
*/
212+
SrsChunkStream** cs_cache;
213+
/**
208214
* bytes buffer cache, recv from skt, provide services for stream.
209215
*/
210216
SrsFastBuffer* in_buffer;

0 commit comments

Comments
 (0)