Skip to content

Commit b6f1fb1

Browse files
committed
Optimize sprintf() into a ROPE
1 parent 0a96c6d commit b6f1fb1

File tree

2 files changed

+316
-0
lines changed

2 files changed

+316
-0
lines changed

Zend/zend_compile.c

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4712,6 +4712,153 @@ static void zend_compile_ns_call(znode *result, znode *name_node, zend_ast *args
47124712
}
47134713
/* }}} */
47144714

4715+
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4716+
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4717+
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4718+
4719+
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4720+
{
4721+
if (args->children < 1) {
4722+
return FAILURE;
4723+
}
4724+
4725+
zend_eval_const_expr(&args->child[0]);
4726+
if (args->child[0]->kind != ZEND_AST_ZVAL) {
4727+
return FAILURE;
4728+
}
4729+
4730+
zval *format_string = zend_ast_get_zval(args->child[0]);
4731+
if (Z_TYPE_P(format_string) != IS_STRING) {
4732+
return FAILURE;
4733+
}
4734+
if (Z_STRLEN_P(format_string) >= 256) {
4735+
return FAILURE;
4736+
}
4737+
4738+
char *p;
4739+
char *end;
4740+
uint32_t string_placeholder_count;
4741+
4742+
string_placeholder_count = 0;
4743+
p = Z_STRVAL_P(format_string);
4744+
end = p + Z_STRLEN_P(format_string);
4745+
4746+
for (;;) {
4747+
p = memchr(p, '%', end - p);
4748+
if (!p) {
4749+
break;
4750+
}
4751+
4752+
char *q = p + 1;
4753+
if (q == end) {
4754+
return FAILURE;
4755+
}
4756+
4757+
switch (*q) {
4758+
case 's':
4759+
string_placeholder_count++;
4760+
break;
4761+
case '%':
4762+
break;
4763+
default:
4764+
return FAILURE;
4765+
}
4766+
4767+
p = q;
4768+
p++;
4769+
}
4770+
4771+
/* Bail out if the number of placeholders does not match the number of values. */
4772+
if (string_placeholder_count != (args->children - 1)) {
4773+
return FAILURE;
4774+
}
4775+
4776+
znode *elements = NULL;
4777+
4778+
if (string_placeholder_count > 0) {
4779+
elements = safe_emalloc(sizeof(*elements), string_placeholder_count, 0);
4780+
}
4781+
4782+
/* Compile the value expressions first for error handling that is consistent
4783+
* with a function call: Values that fail to convert to a string may emit errors.
4784+
*/
4785+
for (size_t i = 0; i < string_placeholder_count; i++) {
4786+
zend_compile_expr(elements + i, args->child[1 + i]);
4787+
if (elements[i].op_type == IS_CONST) {
4788+
if (Z_TYPE(elements[i].u.constant) == IS_ARRAY) {
4789+
zend_emit_op_tmp(&elements[i], ZEND_CAST, &elements[i], NULL)->extended_value = IS_STRING;
4790+
} else {
4791+
convert_to_string(&elements[i].u.constant);
4792+
}
4793+
}
4794+
}
4795+
4796+
uint32_t rope_elements = 0;
4797+
uint32_t rope_init_lineno = get_next_op_number();
4798+
zend_op *opline = NULL;
4799+
4800+
string_placeholder_count = 0;
4801+
p = Z_STRVAL_P(format_string);
4802+
end = p + Z_STRLEN_P(format_string);
4803+
char *offset = p;
4804+
for(;;) {
4805+
p = memchr(p, '%', end - p);
4806+
if (!p) {
4807+
break;
4808+
}
4809+
4810+
char *q = p + 1;
4811+
ZEND_ASSERT(q < end);
4812+
ZEND_ASSERT(*q == 's' || *q == '%');
4813+
4814+
if (*q == '%') {
4815+
/* Optimization to not create a dedicated rope element for the literal '%':
4816+
* Include the first '%' within the "constant" part instead of dropping the
4817+
* full placeholder.
4818+
*/
4819+
p++;
4820+
}
4821+
4822+
if (p != offset) {
4823+
znode const_node;
4824+
const_node.op_type = IS_CONST;
4825+
ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4826+
opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4827+
}
4828+
4829+
if (*q == 's') {
4830+
opline = zend_compile_rope_add(result, rope_elements++, &elements[string_placeholder_count]);
4831+
4832+
string_placeholder_count++;
4833+
}
4834+
4835+
p = q;
4836+
p++;
4837+
offset = p;
4838+
}
4839+
if (end != offset) {
4840+
/* Add the constant part after the last placeholder. */
4841+
znode const_node;
4842+
const_node.op_type = IS_CONST;
4843+
ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
4844+
opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4845+
}
4846+
if (rope_elements == 0) {
4847+
/* Handle empty format strings. */
4848+
znode const_node;
4849+
const_node.op_type = IS_CONST;
4850+
ZVAL_EMPTY_STRING(&const_node.u.constant);
4851+
opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4852+
}
4853+
ZEND_ASSERT(opline != NULL);
4854+
4855+
zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
4856+
zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
4857+
efree(elements);
4858+
4859+
return SUCCESS;
4860+
}
4861+
47154862
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc, uint32_t type) /* {{{ */
47164863
{
47174864
if (zend_string_equals_literal(lcname, "strlen")) {
@@ -4778,6 +4925,8 @@ static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *
47784925
return zend_compile_func_array_slice(result, args);
47794926
} else if (zend_string_equals_literal(lcname, "array_key_exists")) {
47804927
return zend_compile_func_array_key_exists(result, args);
4928+
} else if (zend_string_equals_literal(lcname, "sprintf")) {
4929+
return zend_compile_func_sprintf(result, args);
47814930
} else {
47824931
return FAILURE;
47834932
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
--TEST--
2+
Test sprintf() function : Rope Optimization
3+
--FILE--
4+
<?php
5+
function func($str) {
6+
return strtoupper($str);
7+
}
8+
function sideeffect() {
9+
echo "Called!\n";
10+
return "foo";
11+
}
12+
class Foo {
13+
public function __construct() {
14+
echo "Called\n";
15+
}
16+
}
17+
18+
$a = "foo";
19+
$b = "bar";
20+
$c = new stdClass();
21+
22+
try {
23+
var_dump(sprintf("const"));
24+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
25+
26+
try {
27+
var_dump(sprintf("%s", $a));
28+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
29+
30+
try {
31+
var_dump(sprintf("%s/%s", $a, $b));
32+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
33+
34+
try {
35+
var_dump(sprintf("%s/%s/%s", $a, $b));
36+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
37+
38+
try {
39+
var_dump(sprintf("%s/%s/%s", $a, $b, $c));
40+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
41+
42+
try {
43+
var_dump(sprintf("%s/", func("baz")));
44+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
45+
46+
try {
47+
var_dump(sprintf("/%s", func("baz")));
48+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
49+
50+
try {
51+
var_dump(sprintf("/%s/", func("baz")));
52+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
53+
54+
try {
55+
var_dump(sprintf("%s%s%s%s", $a, $b, func("baz"), $a));
56+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
57+
58+
try {
59+
var_dump(sprintf("%s/%s", sprintf("%s:%s", $a, $b), sprintf("%s-%s", func('baz'), func('baz'))));
60+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
61+
62+
try {
63+
var_dump(sprintf(sideeffect()));
64+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
65+
66+
try {
67+
var_dump(sprintf("%s-%s-%s", __FILE__, __LINE__, 1));
68+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
69+
70+
try {
71+
$values = range('a', 'z');
72+
var_dump(sprintf("%s%s%s", "{$values[0]}{$values[1]}{$values[2]}", "{$values[3]}{$values[4]}{$values[5]}", "{$values[6]}{$values[7]}{$values[8]}"));
73+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
74+
75+
try {
76+
var_dump(sprintf("%s%s%s", new Foo(), new Foo(), new Foo(), ));
77+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
78+
79+
try {
80+
var_dump(sprintf(...));
81+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
82+
83+
try {
84+
var_dump(sprintf('%%s'));
85+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
86+
87+
try {
88+
var_dump(sprintf('%%s', 'test'));
89+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
90+
91+
try {
92+
var_dump(sprintf('%s-%s-%s', [], [], []));
93+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
94+
95+
try {
96+
var_dump(sprintf(""));
97+
} catch (\Throwable $e) {echo $e, PHP_EOL; } echo PHP_EOL;
98+
99+
echo "Done";
100+
?>
101+
--EXPECTF--
102+
string(5) "const"
103+
104+
string(3) "foo"
105+
106+
string(7) "foo/bar"
107+
108+
ArgumentCountError: 4 arguments are required, 3 given in %s:32
109+
Stack trace:
110+
#0 %s(32): sprintf('%s/%s/%s', 'foo', 'bar')
111+
#1 {main}
112+
113+
Error: Object of class stdClass could not be converted to string in %s:36
114+
Stack trace:
115+
#0 {main}
116+
117+
string(4) "BAZ/"
118+
119+
string(4) "/BAZ"
120+
121+
string(5) "/BAZ/"
122+
123+
string(12) "foobarBAZfoo"
124+
125+
string(15) "foo:bar/BAZ-BAZ"
126+
127+
Called!
128+
string(3) "foo"
129+
130+
string(%d) "%ssprintf_rope_optimization.php-%d-1"
131+
132+
string(9) "abcdefghi"
133+
134+
Called
135+
Called
136+
Called
137+
Error: Object of class Foo could not be converted to string in %s:73
138+
Stack trace:
139+
#0 {main}
140+
141+
object(Closure)#3 (2) {
142+
["function"]=>
143+
string(7) "sprintf"
144+
["parameter"]=>
145+
array(2) {
146+
["$format"]=>
147+
string(10) "<required>"
148+
["$values"]=>
149+
string(10) "<optional>"
150+
}
151+
}
152+
153+
string(2) "%s"
154+
155+
string(2) "%s"
156+
157+
158+
Warning: Array to string conversion in %s on line 89
159+
160+
Warning: Array to string conversion in %s on line 89
161+
162+
Warning: Array to string conversion in %s on line 89
163+
string(17) "Array-Array-Array"
164+
165+
string(0) ""
166+
167+
Done

0 commit comments

Comments
 (0)