Skip to content

Optimize performances of str_pad() #19272

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5737,6 +5737,27 @@ PHP_FUNCTION(substr_count)
}
/* }}} */

static void php_str_pad_fill(zend_string *result, size_t pad_chars, const char *pad_str, size_t pad_str_len) {
char *p = ZSTR_VAL(result) + ZSTR_LEN(result);

if (pad_str_len == 1) {
memset(p, pad_str[0], pad_chars);
ZSTR_LEN(result) += pad_chars;
return;
}

const char *end = p + pad_chars;
while (p + pad_str_len <= end) {
p = zend_mempcpy(p, pad_str, pad_str_len);
}

if (p < end) {
memcpy(p, pad_str, end - p);
}

ZSTR_LEN(result) += pad_chars;
}

/* {{{ Returns input string padded on the left or right to specified length with pad_string */
PHP_FUNCTION(str_pad)
{
Expand All @@ -5749,7 +5770,7 @@ PHP_FUNCTION(str_pad)
char *pad_str = " "; /* Pointer to padding string */
size_t pad_str_len = 1;
zend_long pad_type_val = PHP_STR_PAD_RIGHT; /* The padding type value */
size_t i, left_pad=0, right_pad=0;
size_t left_pad=0, right_pad=0;
zend_string *result = NULL; /* Resulting string */

ZEND_PARSE_PARAMETERS_START(2, 4)
Expand Down Expand Up @@ -5799,16 +5820,18 @@ PHP_FUNCTION(str_pad)
}

/* First we pad on the left. */
for (i = 0; i < left_pad; i++)
ZSTR_VAL(result)[ZSTR_LEN(result)++] = pad_str[i % pad_str_len];
if (left_pad > 0) {
php_str_pad_fill(result, left_pad, pad_str, pad_str_len);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
php_str_pad_fill(result, left_pad, pad_str, pad_str_len);
php_str_pad_fill(ZSTR_VAL(result) + ZSTR_LEN(result), left_pad, pad_str, pad_str_len);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SakiTakamachi advised otherwise if I get it right?

Copy link
Member

Choose a reason for hiding this comment

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

It appears so, but the suggestion doesn't really make sense, since pad_chars cannot be piggy-backed on the zend_string. Perhaps Saki meant to pass pad_str and pad_str_len as a zend_string* (which would make sense to me).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It makes the code a bit slower, going from ~48ms per run to ~58ms. Do we still want this?

}

/* Then we copy the input string. */
memcpy(ZSTR_VAL(result) + ZSTR_LEN(result), ZSTR_VAL(input), ZSTR_LEN(input));
ZSTR_LEN(result) += ZSTR_LEN(input);

/* Finally, we pad on the right. */
for (i = 0; i < right_pad; i++)
ZSTR_VAL(result)[ZSTR_LEN(result)++] = pad_str[i % pad_str_len];
if (right_pad > 0) {
php_str_pad_fill(result, right_pad, pad_str, pad_str_len);
}

ZSTR_VAL(result)[ZSTR_LEN(result)] = '\0';

Expand Down