Skip to content

Performance enhancement calling expand_macros() #3010

Open
@marcstern

Description

@marcstern

Hello,
I have a (very simple) proposal to optimize a portion of the code that is used more than 40 times in the project:

msc_string *str = (msc_string *)apr_pcalloc(msr->mp, sizeof(msc_string));
str->value = (char *)input_string;
str->value_len = strlen(str->value);
expand_macros(msr, str, rule, msr->mp);
output_string = str->value;

We could optimize that code by first checking that input_string contains a '%' (or even "%{"). If not, no need to allocate the memory, call the function, etc. We add a (very) little overhead for the check, but avoid a much bigger overhead in most of the cases where the value doesn't contain a macro.
That could be done in a wrapper function (the name could be adapted), that would simplify the code as well:

char* expand_macros_wrapper(modsec_rec *msr, char *var, msre_rule *rule, apr_pool_t *mptmp) {
 if (!strstr(var, "%{")) return var;
 msc_string *str = (msc_string *)apr_pcalloc(msr->mp, sizeof(msc_string));
 str->value = var;
 str->value_len = strlen(str->value);
 int changed = expand_macros(msr, str, rule, msr->mp);
if (!changed) return var; // Does this line add anything? Should we always return str->value?
 return str->value;
}

The calling code would simply be:
output_string = expand_macros_wrapper(msr, input_string, rule, msr->mp);

Any remark?

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.xRelated to ModSecurity version 2.xenhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions