Skip to content

Commit

Permalink
Changed stream list to arrays and optimized stream lookup with SSE2.
Browse files Browse the repository at this point in the history
The stream list in the SRTP context is now implemented with two arrays:
an array of SSRCs and an array of pointers to the streams corresponding
to the SSRCs. The streams no longer form a single linked list.

Stream lookup by SSRC is now performed over the array of SSRCs, which
is considerably faster because it is more cache-friendly. Additionally,
the lookup is optimized for SSE2, which provides an additional massive
speedup with many streams in the list. Although the lookup still has
linear complexity, its absolute times are reduced and with tens to
hundreds elements are lower or comparable with a typical rb-tree
equivalent.

Expected speedup of SSE2 version over the previous implementation:

SSRCs    speedup (scalar)   speedup (SSE2)

1        0.39x              0.22x
3        0.57x              0.23x
5        0.69x              0.62x
10       0.77x              1.43x
20       0.86x              2.38x
30       0.87x              3.44x
50       1.13x              6.21x
100      1.25x              8.51x
200      1.30x              9.83x

At small numbers of SSRCs the new algorithm is somewhat slower, but
given that the absolute and relative times of the lookup are very small,
that slowdown is not very significant.
  • Loading branch information
Lastique committed Sep 25, 2020
1 parent 7d351de commit a4c8d6c
Show file tree
Hide file tree
Showing 3 changed files with 356 additions and 130 deletions.
16 changes: 14 additions & 2 deletions include/srtp_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,26 @@ typedef struct srtp_stream_ctx_t_ {
int *enc_xtn_hdr;
int enc_xtn_hdr_count;
uint32_t pending_roc;
struct srtp_stream_ctx_t_ *next; /* linked list of streams */
} strp_stream_ctx_t_;

/*
* An srtp_stream_list_t is a list of streams searchable by SSRC.
*
* Pointers to streams and their respective SSRCs are stored in two arrays, where
* the stream pointer and the SSRC at the same index correspond to each other.
*/
typedef struct srtp_stream_list_t {
srtp_stream_ctx_t **streams;
uint32_t *ssrcs;
uint32_t size;
uint32_t capacity;
} srtp_stream_list_t;

/*
* an srtp_ctx_t holds a stream list and a service description
*/
typedef struct srtp_ctx_t_ {
struct srtp_stream_ctx_t_ *stream_list; /* linked list of streams */
srtp_stream_list_t stream_list; /* list of streams */
struct srtp_stream_ctx_t_ *stream_template; /* act as template for other */
/* streams */
void *user_data; /* user custom data */
Expand Down
Loading

0 comments on commit a4c8d6c

Please sign in to comment.