ext/intl: Reduce stack usage in collator_regular_compare_function() - #23627
Open
LamentXU123 wants to merge 2 commits into
Open
ext/intl: Reduce stack usage in collator_regular_compare_function()#23627LamentXU123 wants to merge 2 commits into
LamentXU123 wants to merge 2 commits into
Conversation
devnexen
reviewed
Sep 9, 2026
| zval str1, str2; | ||
| zval num1, num2; | ||
| zval norm1, norm2; | ||
| /* The numeric and normalized values are mutually exclusive for each operand. */ |
Member
There was a problem hiding this comment.
I do not know if you plan to create more PR with a similar hack but here I would prefer if you solve this in a more classic manner ; same 32 bytes gain.
diff --git a/ext/intl/collator/collator_sort.cpp b/ext/intl/collator/collator_sort.cpp
index f2674b9c8ff..3f2e1cf543d 100644
--- a/ext/intl/collator/collator_sort.cpp
+++ b/ext/intl/collator/collator_sort.cpp
@@ -65,8 +65,7 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
{
int rc = SUCCESS;
zval str1, str2;
- zval num1, num2;
- zval norm1, norm2;
+ zval tmp1, tmp2;
zval *num1_p = nullptr, *num2_p = nullptr;
zval *norm1_p = nullptr, *norm2_p = nullptr;
zval *str1_p = nullptr, *str2_p = nullptr;
@@ -87,8 +86,8 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
/* If both args are strings AND either of args is not numeric string
* then use ICU-compare. Otherwise PHP-compare. */
if( Z_TYPE_P(str1_p) == IS_STRING && Z_TYPE_P(str2_p) == IS_STRING &&
- ( str1_p == ( num1_p = collator_convert_string_to_number_if_possible( str1_p, &num1 ) ) ||
- str2_p == ( num2_p = collator_convert_string_to_number_if_possible( str2_p, &num2 ) ) ) )
+ ( str1_p == ( num1_p = collator_convert_string_to_number_if_possible( str1_p, &tmp1 ) ) ||
+ str2_p == ( num2_p = collator_convert_string_to_number_if_possible( str2_p, &tmp2 ) ) ) )
{
/* Compare the strings using ICU. */
ZEND_ASSERT(INTL_G(current_collator) != nullptr);
@@ -99,49 +98,28 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
}
else
{
- /* num1 is set if str1 and str2 are strings. */
+ /* num1 is set only if str1 and str2 are both numeric strings. */
if( num1_p )
{
- if( num1_p == str1_p )
- {
- /* str1 is string but not numeric string
- * just convert it to utf8.
- */
- norm1_p = collator_convert_zstr_utf16_to_utf8( str1_p, &norm1 );
- if( norm1_p == nullptr ) {
- rc = FAILURE;
- goto cleanup;
- }
-
- /* num2 is not set but str2 is string => do normalization. */
- norm2_p = collator_normalize_sort_argu
- if( norm2_p == nullptr ) {
- rc = FAILURE;
- goto cleanup;
- }
- }
- else
- {
- /* str1 is numeric strings => passthru
- Z_TRY_ADDREF_P(num1_p);
- norm1_p = num1_p;
+ /* str1 is numeric strings => passthru to PHP-compare. */
+ Z_TRY_ADDREF_P(num1_p);
+ norm1_p = num1_p;
- /* str2 is numeric strings => passthru to PHP-compare. */
- Z_TRY_ADDREF_P(num2_p);
- norm2_p = num2_p;
- }
+ /* str2 is numeric strings => passthru to PHP-compare. */
+ Z_TRY_ADDREF_P(num2_p);
+ norm2_p = num2_p;
}
else
{
/* num1 is not set if str1 or str2 is not a string => do normalization. */
- norm1_p = collator_normalize_sort_argument( st
+ norm1_p = collator_normalize_sort_argument( str1_p, &tmp1 );
if( norm1_p == nullptr ) {
rc = FAILURE;
goto cleanup;
}
/* if num1 is not set then num2 is not set as well => do normalization. */
- norm2_p = collator_normalize_sort_argument( st
+ norm2_p = collator_normalize_sort_argument( str2_p, &tmp2 );
if( norm2_p == nullptr ) {
rc = FAILURE;
goto cleanup;
Member
Author
There was a problem hiding this comment.
I love this :)
I do not know if you plan to create more PR with a similar hack
No. I open this PR because I came across this idea when I am implementing experimental stuffs on top of this code.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.