]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Reduce reallocation in AC Tile MPM creation.
authorKen Steele <ken@tilera.com>
Thu, 3 Jul 2014 16:42:12 +0000 (12:42 -0400)
committerVictor Julien <victor@inliniac.net>
Wed, 30 Jul 2014 12:41:20 +0000 (14:41 +0200)
Exponentially increase the memory allocated for new states when adding new
states, then at the end resize down to the actually final size so that no space is wasted.

src/util-mpm-ac-tile.c
src/util-mpm-ac-tile.h

index 685114381955a30cc085eadf3577ad0ef9ffd6f5..f74f04d2150fc16307d1e0ac1023d6fda72e3a08 100644 (file)
@@ -523,24 +523,13 @@ error:
     return -1;
 }
 
-/**
- * \internal
- * \brief Initialize a new state in the goto and output tables.
- *
- * \param mpm_ctx Pointer to the mpm context.
- *
- * \retval The state id, of the newly created state.
- */
-static inline int SCACTileInitNewState(MpmCtx *mpm_ctx)
+static void SCACTileReallocState(SCACTileCtx *ctx, int new_state_count)
 {
     void *ptmp;
-    SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
-    SCACTileCtx *ctx = search_ctx->init_ctx;
-    int aa = 0;
     int size = 0;
 
     /* reallocate space in the goto table to include a new state */
-    size = (ctx->state_count + 1) * sizeof(int32_t) * 256;
+    size = ctx->allocated_state_count * sizeof(int32_t) * 256;
     ptmp = SCRealloc(ctx->goto_table, size);
     if (ptmp == NULL) {
         SCFree(ctx->goto_table);
@@ -550,13 +539,8 @@ static inline int SCACTileInitNewState(MpmCtx *mpm_ctx)
     }
     ctx->goto_table = ptmp;
 
-    /* set all transitions for the newly assigned state as FAIL transitions */
-    for (aa = 0; aa < ctx->alphabet_size; aa++) {
-        ctx->goto_table[ctx->state_count][aa] = SC_AC_TILE_FAIL;
-    }
-
     /* reallocate space in the output table for the new state */
-    size = (ctx->state_count + 1) * sizeof(SCACTileOutputTable);
+    size = ctx->allocated_state_count * sizeof(SCACTileOutputTable);
     ptmp = SCRealloc(ctx->output_table, size);
     if (ptmp == NULL) {
         SCFree(ctx->output_table);
@@ -565,6 +549,36 @@ static inline int SCACTileInitNewState(MpmCtx *mpm_ctx)
         exit(EXIT_FAILURE);
     }
     ctx->output_table = ptmp;
+}
+
+/**
+ * \internal
+ * \brief Initialize a new state in the goto and output tables.
+ *
+ * \param mpm_ctx Pointer to the mpm context.
+ *
+ * \retval The state id, of the newly created state.
+ */
+static inline int SCACTileInitNewState(MpmCtx *mpm_ctx)
+{
+    SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
+    SCACTileCtx *ctx = search_ctx->init_ctx;
+    int aa = 0;
+
+    /* Exponentially increase the allocated space when needed. */
+    if (ctx->allocated_state_count < ctx->state_count + 1) {
+        if (ctx->allocated_state_count == 0)
+            ctx->allocated_state_count = 256;
+        else
+            ctx->allocated_state_count *= 2;
+
+        SCACTileReallocState(ctx, ctx->allocated_state_count);
+    }
+
+    /* set all transitions for the newly assigned state as FAIL transitions */
+    for (aa = 0; aa < ctx->alphabet_size; aa++) {
+        ctx->goto_table[ctx->state_count][aa] = SC_AC_TILE_FAIL;
+    }
 
     memset(ctx->output_table + ctx->state_count, 0,
            sizeof(SCACTileOutputTable));
@@ -1118,6 +1132,9 @@ static void SCACTilePrepareSearch(MpmCtx *mpm_ctx)
     SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
     SCACTileCtx *ctx = search_ctx->init_ctx;
 
+    /* Resize the output table to be only as big as its final size. */
+    SCACTileReallocState(ctx, ctx->state_count);
+
     search_ctx->search = ctx->search;
     memcpy(search_ctx->translate_table, ctx->translate_table, sizeof(ctx->translate_table));
 
index 17653c65764f09f105d0ad5e87f03e3771c11a36..ce64c9b7d90943bb724124ef9239dc3d9f61ff9c 100644 (file)
@@ -102,6 +102,8 @@ typedef struct SCACTileCtx_ {
 
     /* Number of states used by ac-tile */
     int state_count;
+    /* Number of states allocated for ac-tile. */
+    int allocated_state_count;
 
     /* Largest Pattern Identifier. */
     uint16_t max_pat_id;