Skip to content

Commit 0af87e9

Browse files
committed
Improve performance of intval('+0b...', 2) and intval('0b...', 2)
For this benchmark: ```php <?php for ($i = 0; $i < 10000000; $i++) { intval('+0b11111111111100000000', 2); } ``` On an i7-4790: ``` Benchmark 1: ./sapi/cli/php x.php Time (mean ± σ): 527.3 ms ± 8.1 ms [User: 523.5 ms, System: 2.4 ms] Range (min … max): 515.4 ms … 545.1 ms 10 runs Benchmark 2: ./sapi/cli/php_old x.php Time (mean ± σ): 629.3 ms ± 6.0 ms [User: 625.9 ms, System: 1.8 ms] Range (min … max): 622.8 ms … 643.2 ms 10 runs Summary ./sapi/cli/php x.php ran 1.19 ± 0.02 times faster than ./sapi/cli/php_old x.php ``` Closes GH-20357.
1 parent 5ab594c commit 0af87e9

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,4 @@ PHP 8.6 UPGRADE NOTES
127127
. Improved performance of array_fill_keys().
128128
. Improved performance of array_unshift().
129129
. Improved performance of array_walk().
130+
. Improved performance of intval('+0b...', 2) and intval('0b...', 2).

ext/standard/type.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,17 @@ PHP_FUNCTION(intval)
173173
}
174174

175175
if (strval[offset] == '0' && (strval[offset + 1] == 'b' || strval[offset + 1] == 'B')) {
176+
if (strval[0] != '-') {
177+
/* Either "+0b", or "0b" */
178+
RETURN_LONG(ZEND_STRTOL(strval + 2 + offset, NULL, 2));
179+
}
180+
176181
char *tmpval;
177182
strlen -= 2; /* Removing "0b" */
178183
tmpval = emalloc(strlen + 1);
179184

180-
/* Place the unary symbol at pos 0 if there was one */
181-
if (offset) {
182-
tmpval[0] = strval[0];
183-
}
185+
/* Place the unary symbol at pos 0 */
186+
tmpval[0] = '-';
184187

185188
/* Copy the data from after "0b" to the end of the buffer */
186189
memcpy(tmpval + offset, strval + offset + 2, strlen - offset);

0 commit comments

Comments
 (0)