Skip to content

ext/pcre: simplify locale char table lookup and pattern info error ha…#21312

Open
devnexen wants to merge 2 commits intophp:masterfrom
devnexen:pcre_simpl1
Open

ext/pcre: simplify locale char table lookup and pattern info error ha…#21312
devnexen wants to merge 2 commits intophp:masterfrom
devnexen:pcre_simpl1

Conversation

@devnexen
Copy link
Member

…ndling

Use zend_hash_str_find_ptr/zend_hash_str_add_new_ptr for the locale character tables hash, avoiding a temporary zend_string allocation. Consolidate two pcre2_pattern_info calls with identical error handling into a single conditional.

@devnexen devnexen marked this pull request as ready for review February 27, 2026 19:19
@devnexen devnexen requested a review from ndossche February 27, 2026 19:32

if (key != regex) {
tables = (uint8_t *)zend_hash_find_ptr(&char_tables, BG(ctype_string));
tables = (uint8_t *)zend_hash_str_find_ptr(&char_tables, ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be slower because it will repeatedly hash the string

zend_hash_add_ptr(&char_tables, _k, (void *)tables);
zend_string_release(_k);

zend_hash_str_add_new_ptr(&char_tables, ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)), (void *)tables);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually faster? There are some trade-offs here
If you want to make this code faster then perhaps using the _lookup variant is better anyway


rc = pcre2_pattern_info(re, PCRE2_INFO_NAMECOUNT, &new_entry.name_count);
if (rc < 0) {
if ((rc = pcre2_pattern_info(re, PCRE2_INFO_CAPTURECOUNT, &new_entry.capture_count)) < 0 ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems good but it doesn't belong to the same commit

zend_string_release(_k);
ZVAL_PTR(zv, (void *)tables);
} else {
tables = (uint8_t *)Z_PTR_P(zv);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the cast is necessary.

GC_MAKE_PERSISTENT_LOCAL(_k);
zend_hash_add_ptr(&char_tables, _k, (void *)tables);
zend_string_release(_k);
ZVAL_PTR(zv, (void *)tables);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the cast is necessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm I have const qualifier warning if I do not.

Replace the separate zend_hash_find_ptr + zend_string_init +
zend_hash_add_ptr + zend_string_release sequence with a single
zend_hash_str_lookup() call which handles find-or-insert in one
hash traversal and manages persistent key creation internally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants