Skip to content

Commit

Permalink
Changed stream list to arrays and moved its code to a separate file.
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 faster on large number of SSRCs because it is more cache-friendly.
This opens up the further possibility to optimize the lookup by using
vector instructions, which will follow in a separate commit.

Expected performance of stream lookup compared to the previous list-based
implementation:

SSRCs    speedup (scalar)

1        0.39x
3        0.57x
5        0.69x
10       0.77x
20       0.86x
30       0.87x
50       1.13x
100      1.25x
200      1.30x

Performance tested on an Intel Core i7 2600K CPU.

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 Jan 21, 2022
1 parent f94f402 commit 372491b
Show file tree
Hide file tree
Showing 7 changed files with 418 additions and 133 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ endif()

set(SOURCES_C
srtp/srtp.c
srtp/stream_list.c
)

set(CIPHERS_SOURCES_C
Expand Down
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ cryptobj = $(ciphers) $(hashes) $(math) $(kernel) $(replay)

# libsrtp2.a (implements srtp processing)

srtpobj = srtp/srtp.o
srtpobj = srtp/srtp.o srtp/stream_list.o

libsrtp2.a: $(srtpobj) $(cryptobj) $(gdoi)
$(AR) cr libsrtp2.a $^
Expand Down
69 changes: 67 additions & 2 deletions include/srtp_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,79 @@ 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_;

srtp_err_status_t srtp_stream_dealloc(srtp_stream_ctx_t *stream,
const srtp_stream_ctx_t *stream_template);

/*
* 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;

/*
* Initializes an empty list of streams
*/
void srtp_stream_list_init(srtp_stream_list_t *streams);

/*
* Returns an index of the stream corresponding to ssrc,
* or >= streams->size if no stream exists for that ssrc.
*/
uint32_t srtp_stream_list_find(const srtp_stream_list_t *streams,
uint32_t ssrc);

/*
* Reserves storage to be able to store at least the specified number
* of elements.
*/
srtp_err_status_t srtp_stream_list_reserve(srtp_stream_list_t *streams,
uint32_t new_capacity);

/*
* Inserts a new stream at the end of the list. The newly added stream
* and its SSRC must not be already present in the list.
*/
srtp_err_status_t srtp_stream_list_push_back(srtp_stream_list_t *streams,
srtp_stream_ctx_t *stream);

/*
* Erases an element at the given position and deallocates the stream.
*/
srtp_err_status_t srtp_stream_list_erase(
srtp_stream_list_t *streams,
uint32_t pos,
const srtp_stream_ctx_t *stream_template);

/*
* Clears the list of streams
*/
srtp_err_status_t srtp_stream_list_clear(
srtp_stream_list_t *streams,
const srtp_stream_ctx_t *stream_template);

/*
* Clears and deallocates memory allocated for the list of streams.
* Does not deallocate the list structure itself.
*/
srtp_err_status_t srtp_stream_list_destroy(
srtp_stream_list_t *streams,
const srtp_stream_ctx_t *stream_template);

/*
* 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
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ endif

sources = files(
'srtp/srtp.c',
'srtp/stream_list.c',
)

ciphers_sources = files(
Expand Down
Loading

0 comments on commit 372491b

Please sign in to comment.