From: Ken Steele Date: Thu, 3 Jul 2014 16:42:12 +0000 (-0400) Subject: Reduce reallocation in AC Tile MPM creation. X-Git-Tag: suricata-2.1beta1~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=033ad9e97429d0f01b3601c9b3a30dae4c5d9acc;p=thirdparty%2Fsuricata.git Reduce reallocation in AC Tile MPM creation. 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. --- diff --git a/src/util-mpm-ac-tile.c b/src/util-mpm-ac-tile.c index 6851143819..f74f04d215 100644 --- a/src/util-mpm-ac-tile.c +++ b/src/util-mpm-ac-tile.c @@ -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)); diff --git a/src/util-mpm-ac-tile.h b/src/util-mpm-ac-tile.h index 17653c6576..ce64c9b7d9 100644 --- a/src/util-mpm-ac-tile.h +++ b/src/util-mpm-ac-tile.h @@ -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;