Skip to content

Commit b81d712

Browse files
committed
Micro optimizations
1 parent d77aa8f commit b81d712

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

ext/pcre/php_pcre.c

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, size_t sub
987987
*match_sets = NULL; /* An array of sets of matches for each
988988
subpattern after a global match */
989989
uint32_t options; /* Execution options */
990-
int count = 0; /* Count of matched subpatterns */
990+
int count; /* Count of matched subpatterns */
991991
PCRE2_SIZE *offsets; /* Array of subpattern offsets */
992992
uint32_t num_subpats; /* Number of captured subpatterns */
993993
int matched; /* Has anything matched */
@@ -1510,7 +1510,7 @@ PHPAPI zend_string *php_pcre_replace(zend_string *regex,
15101510
PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, zend_string *subject_str, char *subject, size_t subject_len, zend_string *replace_str, size_t limit, size_t *replace_count)
15111511
{
15121512
uint32_t options; /* Execution options */
1513-
int count = 0; /* Count of matched subpatterns */
1513+
int count; /* Count of matched subpatterns */
15141514
PCRE2_SIZE *offsets; /* Array of subpattern offsets */
15151515
char **subpat_names; /* Array for named subpatterns */
15161516
uint32_t num_subpats; /* Number of captured subpatterns */
@@ -1685,9 +1685,7 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, zend_string *su
16851685
result_len += (walkbuf - (ZSTR_VAL(result) + result_len));
16861686
}
16871687

1688-
if (limit) {
1689-
limit--;
1690-
}
1688+
limit--;
16911689

16921690
/* Advance to the next piece. */
16931691
start_offset = offsets[1];
@@ -1760,7 +1758,7 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, zend_string *su
17601758
static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_string *subject_str, char *subject, size_t subject_len, zend_fcall_info *fci, zend_fcall_info_cache *fcc, size_t limit, size_t *replace_count)
17611759
{
17621760
uint32_t options; /* Execution options */
1763-
int count = 0; /* Count of matched subpatterns */
1761+
int count; /* Count of matched subpatterns */
17641762
PCRE2_SIZE *offsets; /* Array of subpattern offsets */
17651763
char **subpat_names; /* Array for named subpatterns */
17661764
uint32_t num_subpats; /* Number of captured subpatterns */
@@ -1770,9 +1768,8 @@ static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_strin
17701768
char *match, /* The current match */
17711769
*piece; /* The current piece of subject */
17721770
size_t result_len; /* Length of result */
1773-
PCRE2_SPTR mark = NULL; /* Target for MARK name */
17741771
zend_string *result; /* Result of replacement */
1775-
zend_string *eval_result=NULL; /* Result of custom function */
1772+
zend_string *eval_result; /* Result of custom function */
17761773
pcre2_match_data *match_data;
17771774
zend_bool old_mdata_used;
17781775

@@ -1834,8 +1831,6 @@ static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_strin
18341831

18351832
piece = subject + start_offset;
18361833

1837-
mark = pcre2_get_mark(match_data);
1838-
18391834
if (count >= 0 && limit) {
18401835
/* Check for too many substrings condition. */
18411836
if (UNEXPECTED(count == 0)) {
@@ -1864,7 +1859,9 @@ static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_strin
18641859
new_len = result_len + offsets[0] - start_offset; /* part before the match */
18651860

18661861
/* Use custom function to get replacement string and its length. */
1867-
eval_result = preg_do_repl_func(fci, fcc, subject, offsets, subpat_names, count, mark);
1862+
eval_result = preg_do_repl_func(fci, fcc, subject, offsets, subpat_names, count,
1863+
pcre2_get_mark(match_data));
1864+
18681865
ZEND_ASSERT(eval_result);
18691866
new_len = zend_safe_address_guarded(1, ZSTR_LEN(eval_result), new_len);
18701867
if (new_len >= alloc_len) {
@@ -1887,9 +1884,7 @@ static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_strin
18871884
result_len += ZSTR_LEN(eval_result);
18881885
zend_string_release_ex(eval_result, 0);
18891886

1890-
if (limit) {
1891-
limit--;
1892-
}
1887+
limit--;
18931888

18941889
/* Advance to the next piece. */
18951890
start_offset = offsets[1];
@@ -2435,7 +2430,7 @@ PHPAPI void php_pcre_split_impl(pcre_cache_entry *pce, zend_string *subject_str,
24352430
{
24362431
PCRE2_SIZE *offsets; /* Array of subpattern offsets */
24372432
uint32_t options; /* Execution options */
2438-
int count = 0; /* Count of matched subpatterns */
2433+
int count; /* Count of matched subpatterns */
24392434
PCRE2_SIZE start_offset; /* Where the new search starts */
24402435
PCRE2_SIZE next_offset; /* End of the last delimiter match + 1 */
24412436
char *last_match; /* Location of last match */
@@ -2763,7 +2758,7 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
27632758
{
27642759
zval *entry; /* An entry in the input array */
27652760
uint32_t num_subpats; /* Number of captured subpatterns */
2766-
int count = 0; /* Count of matched subpatterns */
2761+
int count; /* Count of matched subpatterns */
27672762
uint32_t options; /* Execution options */
27682763
zend_string *string_key;
27692764
zend_ulong num_key;
@@ -2794,7 +2789,8 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
27942789

27952790
/* Go through the input array */
27962791
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input), num_key, string_key, entry) {
2797-
zend_string *subject_str = zval_get_string(entry);
2792+
zend_string *tmp_subject_str;
2793+
zend_string *subject_str = zval_get_tmp_string(entry, &tmp_subject_str);
27982794

27992795
/* Perform the match */
28002796
#ifdef HAVE_PCRE_JIT_SUPPORT
@@ -2807,27 +2803,39 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
28072803
options, match_data, mctx);
28082804

28092805
/* If the entry fits our requirements */
2810-
if ((count >= 0 && !invert) || (count == PCRE2_ERROR_NOMATCH && invert)) {
2806+
if (count >= 0) {
28112807
/* Check for too many substrings condition. */
28122808
if (UNEXPECTED(count == 0)) {
28132809
php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
2814-
count = num_subpats;
28152810
}
2816-
Z_TRY_ADDREF_P(entry);
2811+
if (!invert) {
2812+
Z_TRY_ADDREF_P(entry);
28172813

2818-
/* Add to return array */
2819-
if (string_key) {
2820-
zend_hash_update(Z_ARRVAL_P(return_value), string_key, entry);
2821-
} else {
2822-
zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry);
2814+
/* Add to return array */
2815+
if (string_key) {
2816+
zend_hash_update(Z_ARRVAL_P(return_value), string_key, entry);
2817+
} else {
2818+
zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry);
2819+
}
28232820
}
2824-
} else if (count < 0 && count != PCRE2_ERROR_NOMATCH) {
2821+
} else if (count == PCRE2_ERROR_NOMATCH) {
2822+
if (invert) {
2823+
Z_TRY_ADDREF_P(entry);
2824+
2825+
/* Add to return array */
2826+
if (string_key) {
2827+
zend_hash_update(Z_ARRVAL_P(return_value), string_key, entry);
2828+
} else {
2829+
zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry);
2830+
}
2831+
}
2832+
} else {
28252833
pcre_handle_exec_error(count);
2826-
zend_string_release_ex(subject_str, 0);
2834+
zend_tmp_string_release(tmp_subject_str);
28272835
break;
28282836
}
28292837

2830-
zend_string_release_ex(subject_str, 0);
2838+
zend_tmp_string_release(tmp_subject_str);
28312839
} ZEND_HASH_FOREACH_END();
28322840
if (match_data != mdata) {
28332841
pcre2_match_data_free(match_data);

0 commit comments

Comments
 (0)