-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Improve performance of unpack() with nameless repetitions #18803
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
Open
nielsdos
wants to merge
2
commits into
php:master
Choose a base branch
from
nielsdos:unpack-repeats
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
−18
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -885,12 +885,15 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
if ((inputpos + size) <= inputlen) { | ||||||||||||||||||||
|
||||||||||||||||||||
zend_string* real_name; | ||||||||||||||||||||
zend_long long_key = 0; | ||||||||||||||||||||
zval val; | ||||||||||||||||||||
|
||||||||||||||||||||
if (repetitions == 1 && namelen > 0) { | ||||||||||||||||||||
/* Use a part of the formatarg argument directly as the name. */ | ||||||||||||||||||||
real_name = zend_string_init_fast(name, namelen); | ||||||||||||||||||||
|
||||||||||||||||||||
} else if (namelen == 0) { | ||||||||||||||||||||
real_name = NULL; | ||||||||||||||||||||
long_key = i + 1; | ||||||||||||||||||||
Comment on lines
+894
to
+896
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GH is not letting me do a single suggested change across "deleted lines"... But if you reorder the ifs you don't need to check the
Suggested change
|
||||||||||||||||||||
} else { | ||||||||||||||||||||
/* Need to add the 1-based element number to the name */ | ||||||||||||||||||||
char buf[MAX_LENGTH_OF_LONG + 1]; | ||||||||||||||||||||
|
@@ -912,7 +915,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
size = len; | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_STRINGL(&val, &input[inputpos], len); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
case 'A': { | ||||||||||||||||||||
|
@@ -939,7 +941,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_STRINGL(&val, &input[inputpos], len + 1); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
/* New option added for Z to remain in-line with the Perl implementation */ | ||||||||||||||||||||
|
@@ -964,7 +965,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
len = s; | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_STRINGL(&val, &input[inputpos], len); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -979,7 +979,9 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
if (size > INT_MAX / 2) { | ||||||||||||||||||||
zend_string_release(real_name); | ||||||||||||||||||||
if (real_name) { | ||||||||||||||||||||
zend_string_release_ex(real_name, false); | ||||||||||||||||||||
} | ||||||||||||||||||||
zend_argument_value_error(1, "repeater must be less than or equal to %d", INT_MAX / 2); | ||||||||||||||||||||
RETURN_THROWS(); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
@@ -1016,7 +1018,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
ZSTR_VAL(buf)[len] = '\0'; | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_STR(&val, buf); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -1026,7 +1027,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
zend_long v = (type == 'c') ? (int8_t) x : x; | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_LONG(&val, v); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -1046,7 +1046,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_LONG(&val, v); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -1062,7 +1061,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_LONG(&val, v); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -1082,8 +1080,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_LONG(&val, v); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
|
||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -1104,7 +1100,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_LONG(&val, v); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
#endif | ||||||||||||||||||||
|
@@ -1124,7 +1119,6 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_DOUBLE(&val, v); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -1143,13 +1137,12 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
ZVAL_DOUBLE(&val, v); | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
case 'x': | ||||||||||||||||||||
/* Do nothing with input, just skip it */ | ||||||||||||||||||||
break; | ||||||||||||||||||||
goto no_output; | ||||||||||||||||||||
|
||||||||||||||||||||
case 'X': | ||||||||||||||||||||
if (inputpos < size) { | ||||||||||||||||||||
|
@@ -1160,7 +1153,7 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
php_error_docref(NULL, E_WARNING, "Type %c: outside of string", type); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
break; | ||||||||||||||||||||
goto no_output; | ||||||||||||||||||||
|
||||||||||||||||||||
case '@': | ||||||||||||||||||||
if (repetitions <= inputlen) { | ||||||||||||||||||||
|
@@ -1170,10 +1163,19 @@ PHP_FUNCTION(unpack) | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
i = repetitions - 1; /* Done, break out of for loop */ | ||||||||||||||||||||
break; | ||||||||||||||||||||
goto no_output; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
zend_string_release(real_name); | ||||||||||||||||||||
if (real_name) { | ||||||||||||||||||||
zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); | ||||||||||||||||||||
} else { | ||||||||||||||||||||
zend_hash_index_update(Z_ARRVAL_P(return_value), long_key, &val); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
no_output: | ||||||||||||||||||||
if (real_name) { | ||||||||||||||||||||
zend_string_release_ex(real_name, false); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
inputpos += size; | ||||||||||||||||||||
if (inputpos < 0) { | ||||||||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.