-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Cache pcre subpattern table #14424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache pcre subpattern table #14424
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea seems to be sensible
ext/pcre/php_pcre.c
Outdated
subpat_names[name_idx] = zend_string_init(name, strlen(name), persistent); | ||
if (persistent) { | ||
GC_MAKE_PERSISTENT_LOCAL(subpat_names[name_idx]); | ||
} | ||
if (is_numeric_string(ZSTR_VAL(subpat_names[name_idx]), ZSTR_LEN(subpat_names[name_idx]), NULL, NULL, 0) > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Complete aside, but this could use is_numeric_str_function
as a future refactoring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to cover this branch in a test, but I'm not so sure we can get here. Compilation seems to fail with a similar error before we get here it seems.
EDIT: this was tested in the past but since ab32d36 this isn't covered anymore, seems like the pcre upgrade made this obsolete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed this code (and redundant test I added) as per my previous comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly merge those commits already and rebase?
@@ -767,6 +767,9 @@ PHP 8.4 UPGRADE NOTES | |||
- MySQLnd: | |||
. Improved the performance of MySQLnd quoting. | |||
|
|||
- PCRE: | |||
. Improved the performance of named capture groups. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the 1st commit - 569c291 - wrong GH issue is mentioned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Multitasking gone wrong, I'll fix it on merge when I squash this.
Looks very good 😍 and thank you @nielsdos very much for this PR! |
@@ -163,6 +167,9 @@ static void php_free_pcre_cache(zval *data) /* {{{ */ | |||
{ | |||
pcre_cache_entry *pce = (pcre_cache_entry *) Z_PTR_P(data); | |||
if (!pce) return; | |||
if (pce->subpats_table) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it s better if free_subpats_table carries the burden to check it wdyt ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally don't like free functions that accept NULL
, so I'd prefer it as-is.
ext/pcre/php_pcre.c
Outdated
@@ -49,9 +49,11 @@ char *php_pcre_version; | |||
|
|||
struct _pcre_cache_entry { | |||
pcre2_code *re; | |||
/* Pointer is not NULL when there are named captures. | |||
* NULL-terminated, length is equal to capture_count + 1. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not NULL-terminated (I believe that length is capture_count+1 to account for group 0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from @arnaud-lb 's comment, LGTM!
ext/pcre/php_pcre.c
Outdated
php_error_docref(NULL, E_WARNING, "Numeric named subpatterns are not allowed"); | ||
free_subpats_table(subpat_names, num_subpats); | ||
return NULL; | ||
/* Note: if we're making persistent strings, they will only be used within this thread. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we may clarify that they are persistent so they survive until the next request, rather than for being shared across threads.
ext/pcre/php_pcre.c
Outdated
while (ni++ < name_cnt) { | ||
unsigned short name_idx = 0x100 * (unsigned char)name_table[0] + (unsigned char)name_table[1]; | ||
const char *name = name_table + 2; | ||
subpat_names[name_idx] = zend_string_init(name, strlen(name), 0); | ||
if (is_numeric_string(ZSTR_VAL(subpat_names[name_idx]), ZSTR_LEN(subpat_names[name_idx]), NULL, NULL, 0) > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just making sure: Was this code dead? PCRE seems to requrie:
Compilation failed: subpattern name must start with a non-digit at offset 8
for digits, or:
Compilation failed: subpattern name expected at offset 8
For symbols like .
/+
/-
etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is dead because it is checked during compilation of the regular expression and this check in php-src executes after compilation. The compilation failure causes this code not to execute in the first place.
EDIT: I'll commit it anyway as per Girgias's request, so even if this would be wrong after all we can easily revert it.
Recreating this over and over is pointless, cache this as well. Fixes phpGH-14423.
076464d
to
ae5444e
Compare
Recreating this over and over is pointless, cache this as well.
Fixes GH-14423.