From: Philippe Antoine Date: Mon, 5 Jul 2021 15:05:10 +0000 (+0200) Subject: detect: faster linked list copy X-Git-Tag: suricata-7.0.0-beta1~747 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=45d1a9ae772acdf1291c0880217beb16d3bc60c7;p=thirdparty%2Fsuricata.git detect: faster linked list copy In DetectAppLayerInspectEngineCopyListToDetectCtx Avoid quadratic complexity by remembering last element of the linked list we are inserting into --- diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index 2d55e709ed..9d126c7ed6 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -215,6 +215,7 @@ void DetectAppLayerMpmRegisterByParentId(DetectEngineCtx *de_ctx, void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx) { const DetectBufferMpmRegistery *list = g_mpm_list[DETECT_BUFFER_MPM_TYPE_APP]; + DetectBufferMpmRegistery *toadd = de_ctx->app_mpms_list; while (list != NULL) { DetectBufferMpmRegistery *n = SCCalloc(1, sizeof(*n)); BUG_ON(n == NULL); @@ -222,14 +223,12 @@ void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx) *n = *list; n->next = NULL; - if (de_ctx->app_mpms_list == NULL) { + if (toadd == NULL) { + toadd = n; de_ctx->app_mpms_list = n; } else { - DetectBufferMpmRegistery *t = de_ctx->app_mpms_list; - while (t->next != NULL) { - t = t->next; - } - t->next = n; + toadd->next = n; + toadd = toadd->next; } /* default to whatever the global setting is */ diff --git a/src/detect-engine.c b/src/detect-engine.c index 000fed2112..9d7fd2a49a 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -322,6 +322,7 @@ static void DetectAppLayerInspectEngineCopy( static void DetectAppLayerInspectEngineCopyListToDetectCtx(DetectEngineCtx *de_ctx) { const DetectEngineAppInspectionEngine *t = g_app_inspect_engines; + DetectEngineAppInspectionEngine *list = de_ctx->app_inspect_engines; while (t) { DetectEngineAppInspectionEngine *new_engine = SCCalloc(1, sizeof(DetectEngineAppInspectionEngine)); if (unlikely(new_engine == NULL)) { @@ -334,16 +335,12 @@ static void DetectAppLayerInspectEngineCopyListToDetectCtx(DetectEngineCtx *de_c new_engine->progress = t->progress; new_engine->v2 = t->v2; - if (de_ctx->app_inspect_engines == NULL) { + if (list == NULL) { de_ctx->app_inspect_engines = new_engine; } else { - DetectEngineAppInspectionEngine *list = de_ctx->app_inspect_engines; - while (list->next != NULL) { - list = list->next; - } - list->next = new_engine; } + list = new_engine; t = t->next; }