Skip to content

ext/mbstring: Optimize mb_str_pad() using doubling copies - #23667

Merged
LamentXU123 merged 2 commits into
php:masterfrom
LamentXU123:opt-st-m
Sep 12, 2026
Merged

LamentXU123 merged 2 commits into
php:masterfrom
LamentXU123:opt-st-m

Conversation

@LamentXU123

Copy link
Copy Markdown
Member

Follow-up #23661. Use the same optimization on mb_str_pad.

Comment thread ext/mbstring/mbstring.c Outdated
}
/* }}} */

static char *php_mb_str_pad_fill(char *buffer, zend_string *pad, size_t pad_bytes)

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.

nit: I think pad can be const, Gina would probably agrees on it :)

@devnexen devnexen left a comment

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.

I think it s alright, please wait maintainers review first tough.

Co-Authored-By: David CARLIER <devnexen@gmail.com>

@youkidearitai youkidearitai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I confirmed improve performance when using STR_PAD_BOTH. LGTM.

script

<?php
for ($loop = 0; $loop < 100000; $loop++) {
        (mb_str_pad('▶▶', 2000, '❤❓❇', STR_PAD_BOTH));
}
?>

before

real 0m0.270s
user 0m0.234s
sys 0m0.000s

after

real 0m0.053s
user 0m0.047s
sys 0m0.012s

@LamentXU123
LamentXU123 merged commit ae62043 into php:master Sep 12, 2026
18 checks passed
@LamentXU123

Copy link
Copy Markdown
Member Author

Thank you!

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.

3 participants