Commit 2101993133e for php.net
commit 2101993133e849f3e84da9c478126cc7cb791996
Author: Weilin Du <weilindu@php.net>
Date: Mon Sep 14 00:03:40 2026 +0800
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.
diff --git a/UPGRADING b/UPGRADING
index 7c7b727759b..07223cd7a21 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 577d4267ed2..f7c2ed37cf6 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;
}