Module: kamailio
Branch: master
Commit: 6d1f59df690ace49752be7b6abe6ab5f50aa5e72
URL: 
https://github.com/kamailio/kamailio/commit/6d1f59df690ace49752be7b6abe6ab5f50aa5e72

Author: Lucian Balaceanu <lucian.balace...@1and1.ro>
Committer: Victor Seva <linuxman...@torreviejawireless.org>
Date: 2025-06-23T10:31:38+02:00

dialplan: improve performance by reusing PCRE structure

---

Modified: src/modules/dialplan/dp_repl.c

---

Diff:  
https://github.com/kamailio/kamailio/commit/6d1f59df690ace49752be7b6abe6ab5f50aa5e72.diff
Patch: 
https://github.com/kamailio/kamailio/commit/6d1f59df690ace49752be7b6abe6ab5f50aa5e72.patch

---

diff --git a/src/modules/dialplan/dp_repl.c b/src/modules/dialplan/dp_repl.c
index d78db14eb68..82f7e32584d 100644
--- a/src/modules/dialplan/dp_repl.c
+++ b/src/modules/dialplan/dp_repl.c
@@ -409,7 +409,7 @@ int rule_translate(sip_msg_t *msg, str *instr, dpl_node_t 
*rule,
        int repl_nb, offset, match_nb, rc, cap_cnt;
        struct replace_with token;
        struct subst_expr *repl_comp;
-       pcre2_match_data *pcre_md = NULL;
+       static pcre2_match_data *pcre_md = NULL;
        str match;
        pv_value_t sv;
        str *uri;
@@ -427,6 +427,14 @@ int rule_translate(sip_msg_t *msg, str *instr, dpl_node_t 
*rule,
                return 0;
        }
 
+       if(pcre_md == NULL) {
+               pcre_md = pcre2_match_data_create(MAX_REPLACE_WITH, NULL);
+               if(pcre_md == NULL) {
+                       LM_ERR("failed to allocate pcre2_match_data\n");
+                       return -1;
+               }
+       }
+
        if(subst_comp) {
                /*just in case something went wrong at load time*/
                rc = pcre2_pattern_info(subst_comp, PCRE2_INFO_CAPTURECOUNT, 
&cap_cnt);
@@ -446,7 +454,6 @@ int rule_translate(sip_msg_t *msg, str *instr, dpl_node_t 
*rule,
                }
 
                /*search for the pattern from the compiled subst_exp*/
-               pcre_md = pcre2_match_data_create_from_pattern(subst_comp, 
NULL);
                if(pcre2_match(subst_comp, (PCRE2_SPTR)instr->s, 
(PCRE2_SIZE)instr->len,
                                   0, 0, pcre_md, NULL)
                                <= 0) {
@@ -454,8 +461,6 @@ int rule_translate(sip_msg_t *msg, str *instr, dpl_node_t 
*rule,
                                   "the match_exp %.*s but not the subst_exp 
%.*s!\n",
                                        instr->len, instr->s, 
rule->match_exp.len,
                                        rule->match_exp.s, rule->subst_exp.len, 
rule->subst_exp.s);
-                       if(pcre_md)
-                               pcre2_match_data_free(pcre_md);
                        return -1;
                }
                ovector = pcre2_get_ovector_pointer(pcre_md);
@@ -472,8 +477,6 @@ int rule_translate(sip_msg_t *msg, str *instr, dpl_node_t 
*rule,
                memcpy(result->s, repl_comp->replacement.s, 
repl_comp->replacement.len);
                result->len = repl_comp->replacement.len;
                result->s[result->len] = '\0';
-               if(pcre_md)
-                       pcre2_match_data_free(pcre_md);
                return 0;
        }
 
@@ -582,15 +585,11 @@ int rule_translate(sip_msg_t *msg, str *instr, dpl_node_t 
*rule,
        }
 
        result->s[result->len] = '\0';
-       if(pcre_md)
-               pcre2_match_data_free(pcre_md);
        return 0;
 
 error:
        result->s = 0;
        result->len = 0;
-       if(pcre_md)
-               pcre2_match_data_free(pcre_md);
        return -1;
 }
 
@@ -599,7 +598,7 @@ static char dp_attrs_buf[DP_MAX_ATTRS_LEN + 1];
 int dp_translate_helper(
                sip_msg_t *msg, str *input, str *output, dpl_id_p idp, str 
*attrs)
 {
-       pcre2_match_data *pcre_md = NULL;
+       static pcre2_match_data *pcre_md = NULL;
        dpl_node_p rulep;
        dpl_index_p indexp;
        int user_len, rez;
@@ -622,6 +621,14 @@ int dp_translate_helper(
                return -1;
        }
 
+       if(pcre_md == NULL) {
+               pcre_md = pcre2_match_data_create(MAX_REPLACE_WITH, NULL);
+               if(pcre_md == NULL) {
+                       LM_ERR("failed to allocate pcre2_match_data\n");
+                       return -1;
+               }
+       }
+
 search_rule:
        for(rulep = indexp->first_rule; rulep != NULL; rulep = rulep->next) {
                switch(rulep->matchop) {
@@ -640,8 +647,6 @@ int dp_translate_helper(
                                        rez = -1;
                                        do {
                                                if(rez < 0) {
-                                                       pcre_md = 
pcre2_match_data_create_from_pattern(
-                                                                       
re_list->re, NULL);
                                                        rez = 
pcre2_match(re_list->re, (PCRE2_SPTR)input->s,
                                                                        
(PCRE2_SIZE)input->len, 0, 0, pcre_md,
                                                                        NULL);
@@ -651,18 +656,13 @@ int dp_translate_helper(
                                                        LM_DBG("match check 
skipped: [%.*s] %d\n",
                                                                        
re_list->expr.len, re_list->expr.s, rez);
                                                rt = re_list->next;
-                                               if(pcre_md)
-                                                       
pcre2_match_data_free(pcre_md);
                                                pcre2_code_free(re_list->re);
                                                pkg_free(re_list);
                                                re_list = rt;
                                        } while(re_list);
                                } else {
-                                       pcre_md = 
pcre2_match_data_create_from_pattern(
-                                                       rulep->match_comp, 
NULL);
                                        rez = pcre2_match(rulep->match_comp, 
(PCRE2_SPTR)input->s,
                                                        (PCRE2_SIZE)input->len, 
0, 0, pcre_md, 0);
-                                       pcre2_match_data_free(pcre_md);
                                }
                                break;
 

_______________________________________________
Kamailio - Development Mailing List -- sr-dev@lists.kamailio.org
To unsubscribe send an email to sr-dev-le...@lists.kamailio.org
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to