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.
pull/1071/head
Ken Steele 11 years ago committed by Victor Julien
parent a6dbf627b2
commit 033ad9e974

@ -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));

@ -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;

Loading…
Cancel
Save