From 45d1a9ae772acdf1291c0880217beb16d3bc60c7 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Mon, 5 Jul 2021 17:05:10 +0200 Subject: [PATCH] detect: faster linked list copy In DetectAppLayerInspectEngineCopyListToDetectCtx Avoid quadratic complexity by remembering last element of the linked list we are inserting into --- src/detect-engine-mpm.c | 11 +++++------ src/detect-engine.c | 9 +++------ 2 files changed, 8 insertions(+), 12 deletions(-) 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; }