From b03492ac859ff68e4de77736098fad9cedf901af Mon Sep 17 00:00:00 2001 From: Edwin van der Oetelaar Date: Tue, 15 Jan 2013 11:23:05 +0800 Subject: [PATCH] optimize extpoll fd delete Previous method of shifting back array by one to cover the deleted item could be expensive when the list was large and the deleted item appeared early in it. This scheme swaps the last guy into the vacant space and reduces the list size by one. (AG adapted for style and not to care if n is end guy) Signed-off-by: Edwin van der Oetelaar Signed-off-by: Andy Green --- test-server/test-server-extpoll.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/test-server/test-server-extpoll.c b/test-server/test-server-extpoll.c index 3914907..5025f4d 100644 --- a/test-server/test-server-extpoll.c +++ b/test-server/test-server-extpoll.c @@ -139,13 +139,18 @@ static int callback_http(struct libwebsocket_context * this, break; case LWS_CALLBACK_DEL_POLL_FD: - for (n = 0; n < count_pollfds; n++) - if (pollfds[n].fd == (int)(long)user) - while (n < count_pollfds) { - pollfds[n] = pollfds[n + 1]; - n++; - } - count_pollfds--; + for (n = 0; n < count_pollfds; n++) { + if (pollfds[n].fd != (int)(long)user) + continue; + /* + * swap the end guy into our vacant slot... + * works ok if n is the end guy + */ + pollfds[n] = pollfds[count_pollfds - 1]; + pollfds[count_pollfds - 1].fd = -1; + count_pollfds--; + break; + } break; case LWS_CALLBACK_SET_MODE_POLL_FD: -- 2.7.4