Skip to content

ext/intl: various optimization#22069

Open
LamentXU123 wants to merge 4 commits into
php:masterfrom
LamentXU123:opt-intl
Open

ext/intl: various optimization#22069
LamentXU123 wants to merge 4 commits into
php:masterfrom
LamentXU123:opt-intl

Conversation

@LamentXU123
Copy link
Copy Markdown
Contributor

@LamentXU123 LamentXU123 commented May 17, 2026

  • When the array size is larger than 8, using array_init_size is slightly faster than array_init.Similar to uri: Preinitialize errors array with the correct size in fill_errors() #21560 and ext/openssl: various arrays optimisations. #18377
  • Directly use length instead of strlen(...) if it is already known to enhance readability.
  • In transliterator/transliterator_methods.cpp: counted the ICU enumeration up front with uenum_count(), reset it, and switched transliterator_list_ids() to array_init_size(). So we now use ICU APIs to get total length and use array_init_size instead of array_init directly here.
  • In resourcebundle/resourcebundle_class.cpp: ditto. counted available locales before iteration and initialized the return array with the exact capacity.

@LamentXU123 LamentXU123 marked this pull request as ready for review May 17, 2026 06:34
@LamentXU123 LamentXU123 requested a review from devnexen as a code owner May 17, 2026 06:34
@devnexen
Copy link
Copy Markdown
Member

If you have further optimisations plan you can put it there too.

@LamentXU123 LamentXU123 changed the title ext/intl: refactor to use array_init_size instead of array_init when the size is known ext/intl: various optimization May 17, 2026
@LamentXU123 LamentXU123 marked this pull request as draft May 17, 2026 08:16
@LamentXU123 LamentXU123 force-pushed the opt-intl branch 3 times, most recently from 0018bf2 to da1313e Compare May 17, 2026 12:52
@LamentXU123 LamentXU123 changed the base branch from master to PHP-8.4 May 17, 2026 12:52
Comment thread ext/intl/dateformat/dateformat_create.cpp Outdated
@LamentXU123 LamentXU123 marked this pull request as ready for review May 22, 2026 15:28
@LamentXU123
Copy link
Copy Markdown
Contributor Author

Now this should be fine to review

INTL_CHECK_STATUS(icuerror, "Cannot fetch locales list");

count = uenum_count(icuenum, &icuerror);
if (U_FAILURE(icuerror)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

are you mixing bug fixes with optim ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

also I doubt a bit more this specific optimisation. i.e. how icuenum->count callback costly is.

uenum_close(en);
INTL_CHECK_STATUS(status, "Failed to count registered transliterators");
}
uenum_reset(en, &status);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it might be more harmful here than in the other case, you re paying for something that is not necessary, i.e. the enum is already at offset 0.

}
uenum_reset( icuenum, &icuerror );
INTL_CHECK_STATUS(icuerror, "Cannot iterate locales list");
if (U_FAILURE(icuerror)) {
Copy link
Copy Markdown
Member

@devnexen devnexen May 22, 2026

Choose a reason for hiding this comment

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

please take out those bug fixes and make another PR out of it. clearly a mem leak fix.

@LamentXU123
Copy link
Copy Markdown
Contributor Author

Turns out the pattern of calculate array length in bulk and use array_init_size doesn't have obvious performance enhance (tested) and it also is a footgun we want to prevent, so I am dropping those.

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