Commit 8ca3fe5148d for php
commit 8ca3fe5148da03464d9c5302e5acbc351b95667c
Author: Weilin Du <weilindu@php.net>
Date: Mon Oct 5 20:03:43 2026 +0800
ext/standard: Optimize pack() by avoiding redundant padding in pack() string formats (#24133)
diff --git a/UPGRADING b/UPGRADING
index a6cdf9829ab..9ef58ba6007 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -99,6 +99,8 @@ PHP 8.7 UPGRADE NOTES
. Improved performance of array_splice() when inserting without removing
elements.
. Improved performance of str_rot13().
+ . Improved performance of pack() for large strings using the a, A and Z
+ formats.
- MBString:
. Improved performance of mb_strlen for UTF-8 strings.
diff --git a/ext/standard/pack.c b/ext/standard/pack.c
index 1da93228f71..c8cbf8613f6 100644
--- a/ext/standard/pack.c
+++ b/ext/standard/pack.c
@@ -559,9 +559,12 @@ PHP_FUNCTION(pack)
zend_string *tmp_str;
zend_string *str = zval_get_tmp_string(&argv[currentarg++], &tmp_str);
- memset(&ZSTR_VAL(output)[outputpos], (code == 'a' || code == 'Z') ? '\0' : ' ', arg);
- memcpy(&ZSTR_VAL(output)[outputpos], ZSTR_VAL(str),
- (ZSTR_LEN(str) < arg_cp) ? ZSTR_LEN(str) : arg_cp);
+ arg_cp = MIN(ZSTR_LEN(str), arg_cp);
+ if (arg_cp < arg) {
+ memset(&ZSTR_VAL(output)[outputpos + arg_cp],
+ (code == 'a' || code == 'Z') ? '\0' : ' ', arg - arg_cp);
+ }
+ memcpy(&ZSTR_VAL(output)[outputpos], ZSTR_VAL(str), arg_cp);
outputpos += arg;
zend_tmp_string_release(tmp_str);