[php-src] master: ext/standard: Optimize octal escape generation in addcslashes() (#23680)
| From: | Weilin Du via GitHub | Date: | Sun, 13 Sep 2026 16:03:43 +0000 |
| Subject: | [php-src] master: ext/standard: Optimize octal escape generation in addcslashes() (#23680) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141187@lists.php.net to get a copy of this message | ||
Author: Weilin Du (LamentXU123)
Committer: GitHub (web-flow)
Pusher: LamentXU123
Date: 2026-09-14T00:03:40+08:00
Commit: https://github.com/php/php-src/commit/2101993133e849f3e84da9c478126cc7cb791996
Raw diff: https://github.com/php/php-src/commit/2101993133e849f3e84da9c478126cc7cb791996.diff
ext/standard: Optimize octal escape generation in addcslashes() (#23680)
This hack is slightly faster than the original one.
My local benchmarks show approximately 39% lower
runtime for a single NUL byte.
Changed paths:
M UPGRADING
M ext/standard/string.c
Diff:
diff --git a/UPGRADING b/UPGRADING
index 7c7b727759b6..07223cd7a21f 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -1098,6 +1098,7 @@ PHP 8.6 UPGRADE NOTES
. Reduced temporary allocations when iterating Phar directories.
- Standard:
+ . Improved performance of addcslashes() when generating octal escapes.
. Improved performance of sorting single-element arrays.
. Improved performance of array_fill_keys().
. Improved performance of array_intersect().
diff --git a/ext/standard/string.c b/ext/standard/string.c
index 577d4267ed2f..f7c2ed37cf64 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -3904,7 +3904,11 @@ PHPAPI zend_string *php_addcslashes_str(const char *str, size_t len, const
char
case '\v': *target++ = 'v'; break;
case '\b': *target++ = 'b'; break;
case '\f': *target++ = 'f'; break;
- default: target += snprintf(target, 4, "%03o", (unsigned char) c);
+ default:
+ /* Write the byte as three octal digits, including leading zeros. */
+ *target++ = ((unsigned char) c >> 6) + '0';
+ *target++ = (((unsigned char) c >> 3) & 7) + '0';
+ *target++ = ((unsigned char) c & 7) + '0';
}
continue;
}