Skip to content

Commit 372491b

Browse files
committed
Changed stream list to arrays and moved its code to a separate file.
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.
1 parent f94f402 commit 372491b

7 files changed

Lines changed: 418 additions & 133 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ endif()
117117

118118
set(SOURCES_C
119119
srtp/srtp.c
120+
srtp/stream_list.c
120121
)
121122

122123
set(CIPHERS_SOURCES_C

Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ cryptobj = $(ciphers) $(hashes) $(math) $(kernel) $(replay)
156156

157157
# libsrtp2.a (implements srtp processing)
158158

159-
srtpobj = srtp/srtp.o
159+
srtpobj = srtp/srtp.o srtp/stream_list.o
160160

161161
libsrtp2.a: $(srtpobj) $(cryptobj) $(gdoi)
162162
$(AR) cr libsrtp2.a $^

include/srtp_priv.h

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,79 @@ typedef struct srtp_stream_ctx_t_ {
149149
int *enc_xtn_hdr;
150150
int enc_xtn_hdr_count;
151151
uint32_t pending_roc;
152-
struct srtp_stream_ctx_t_ *next; /* linked list of streams */
153152
} strp_stream_ctx_t_;
154153

154+
srtp_err_status_t srtp_stream_dealloc(srtp_stream_ctx_t *stream,
155+
const srtp_stream_ctx_t *stream_template);
156+
157+
/*
158+
* An srtp_stream_list_t is a list of streams searchable by SSRC.
159+
*
160+
* Pointers to streams and their respective SSRCs are stored in two arrays,
161+
* where the stream pointer and the SSRC at the same index correspond to each
162+
* other.
163+
*/
164+
typedef struct srtp_stream_list_t {
165+
srtp_stream_ctx_t **streams;
166+
uint32_t *ssrcs;
167+
uint32_t size;
168+
uint32_t capacity;
169+
} srtp_stream_list_t;
170+
171+
/*
172+
* Initializes an empty list of streams
173+
*/
174+
void srtp_stream_list_init(srtp_stream_list_t *streams);
175+
176+
/*
177+
* Returns an index of the stream corresponding to ssrc,
178+
* or >= streams->size if no stream exists for that ssrc.
179+
*/
180+
uint32_t srtp_stream_list_find(const srtp_stream_list_t *streams,
181+
uint32_t ssrc);
182+
183+
/*
184+
* Reserves storage to be able to store at least the specified number
185+
* of elements.
186+
*/
187+
srtp_err_status_t srtp_stream_list_reserve(srtp_stream_list_t *streams,
188+
uint32_t new_capacity);
189+
190+
/*
191+
* Inserts a new stream at the end of the list. The newly added stream
192+
* and its SSRC must not be already present in the list.
193+
*/
194+
srtp_err_status_t srtp_stream_list_push_back(srtp_stream_list_t *streams,
195+
srtp_stream_ctx_t *stream);
196+
197+
/*
198+
* Erases an element at the given position and deallocates the stream.
199+
*/
200+
srtp_err_status_t srtp_stream_list_erase(
201+
srtp_stream_list_t *streams,
202+
uint32_t pos,
203+
const srtp_stream_ctx_t *stream_template);
204+
205+
/*
206+
* Clears the list of streams
207+
*/
208+
srtp_err_status_t srtp_stream_list_clear(
209+
srtp_stream_list_t *streams,
210+
const srtp_stream_ctx_t *stream_template);
211+
212+
/*
213+
* Clears and deallocates memory allocated for the list of streams.
214+
* Does not deallocate the list structure itself.
215+
*/
216+
srtp_err_status_t srtp_stream_list_destroy(
217+
srtp_stream_list_t *streams,
218+
const srtp_stream_ctx_t *stream_template);
219+
155220
/*
156221
* an srtp_ctx_t holds a stream list and a service description
157222
*/
158223
typedef struct srtp_ctx_t_ {
159-
struct srtp_stream_ctx_t_ *stream_list; /* linked list of streams */
224+
srtp_stream_list_t stream_list; /* list of streams */
160225
struct srtp_stream_ctx_t_ *stream_template; /* act as template for other */
161226
/* streams */
162227
void *user_data; /* user custom data */

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ endif
170170

171171
sources = files(
172172
'srtp/srtp.c',
173+
'srtp/stream_list.c',
173174
)
174175

175176
ciphers_sources = files(

0 commit comments

Comments
 (0)