Skip to content

Commit 372b967

Browse files
committed
Optimize sprintf() into a ROPE
1 parent 1387b00 commit 372b967

File tree

2 files changed

+292
-0
lines changed

2 files changed

+292
-0
lines changed

Zend/zend_compile.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4712,6 +4712,135 @@ 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+
zval *format_string = zend_ast_get_zval(args->child[0]);
4730+
if (Z_TYPE_P(format_string) != IS_STRING) {
4731+
return FAILURE;
4732+
}
4733+
if (Z_STRLEN_P(format_string) >= 256) {
4734+
return FAILURE;
4735+
}
4736+
4737+
char *p;
4738+
char *end;
4739+
uint32_t string_placeholder_count;
4740+
4741+
string_placeholder_count = 0;
4742+
p = Z_STRVAL_P(format_string);
4743+
end = p + Z_STRLEN_P(format_string);
4744+
4745+
for (;;) {
4746+
p = memchr(p, '%', end - p);
4747+
if (!p) {
4748+
break;
4749+
}
4750+
4751+
char *q = p + 1;
4752+
if (q == end) {
4753+
return FAILURE;
4754+
}
4755+
4756+
switch (*q) {
4757+
case 's':
4758+
string_placeholder_count++;
4759+
break;
4760+
case '%':
4761+
break;
4762+
default:
4763+
return FAILURE;
4764+
}
4765+
4766+
p = q;
4767+
p++;
4768+
}
4769+
4770+
if (string_placeholder_count != (args->children - 1)) {
4771+
return FAILURE;
4772+
}
4773+
4774+
znode *elements = NULL;
4775+
4776+
if (string_placeholder_count > 0) {
4777+
elements = safe_emalloc(sizeof(*elements), string_placeholder_count, 0);
4778+
}
4779+
4780+
for (size_t i = 0; i < string_placeholder_count; i++) {
4781+
zend_compile_expr(elements + i, args->child[1 + i]);
4782+
if (elements[i].op_type == IS_CONST) {
4783+
if (Z_TYPE(elements[i].u.constant) == IS_ARRAY) {
4784+
zend_emit_op_tmp(&elements[i], ZEND_CAST, &elements[i], NULL)->extended_value = IS_STRING;
4785+
} else {
4786+
convert_to_string(&elements[i].u.constant);
4787+
}
4788+
}
4789+
}
4790+
4791+
uint32_t rope_elements = 0;
4792+
uint32_t rope_init_lineno = get_next_op_number();
4793+
zend_op *opline;
4794+
4795+
string_placeholder_count = 0;
4796+
p = Z_STRVAL_P(format_string);
4797+
end = p + Z_STRLEN_P(format_string);
4798+
char *offset = p;
4799+
for(;;) {
4800+
p = memchr(p, '%', end - p);
4801+
if (!p) {
4802+
break;
4803+
}
4804+
4805+
char *q = p + 1;
4806+
ZEND_ASSERT(q < end);
4807+
ZEND_ASSERT(*q == 's' || *q == '%');
4808+
4809+
if (*q == '%') {
4810+
p++;
4811+
}
4812+
4813+
if (p != offset) {
4814+
znode const_node;
4815+
const_node.op_type = IS_CONST;
4816+
ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4817+
opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4818+
}
4819+
4820+
if (*q == 's') {
4821+
opline = zend_compile_rope_add(result, rope_elements++, &elements[string_placeholder_count]);
4822+
4823+
string_placeholder_count++;
4824+
}
4825+
4826+
p = q;
4827+
p++;
4828+
offset = p;
4829+
}
4830+
if (end != offset) {
4831+
znode const_node;
4832+
const_node.op_type = IS_CONST;
4833+
ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
4834+
opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4835+
}
4836+
4837+
zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
4838+
zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
4839+
efree(elements);
4840+
4841+
return SUCCESS;
4842+
}
4843+
47154844
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc, uint32_t type) /* {{{ */
47164845
{
47174846
if (zend_string_equals_literal(lcname, "strlen")) {
@@ -4778,6 +4907,8 @@ static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *
47784907
return zend_compile_func_array_slice(result, args);
47794908
} else if (zend_string_equals_literal(lcname, "array_key_exists")) {
47804909
return zend_compile_func_array_key_exists(result, args);
4910+
} else if (zend_string_equals_literal(lcname, "sprintf")) {
4911+
return zend_compile_func_sprintf(result, args);
47814912
} else {
47824913
return FAILURE;
47834914
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
echo "Done";
96+
?>
97+
--EXPECTF--
98+
string(5) "const"
99+
100+
string(3) "foo"
101+
102+
string(7) "foo/bar"
103+
104+
ArgumentCountError: 4 arguments are required, 3 given in %s:32
105+
Stack trace:
106+
#0 %s(32): sprintf('%s/%s/%s', 'foo', 'bar')
107+
#1 {main}
108+
109+
Error: Object of class stdClass could not be converted to string in %s:36
110+
Stack trace:
111+
#0 {main}
112+
113+
string(4) "BAZ/"
114+
115+
string(4) "/BAZ"
116+
117+
string(5) "/BAZ/"
118+
119+
string(12) "foobarBAZfoo"
120+
121+
string(15) "foo:bar/BAZ-BAZ"
122+
123+
Called!
124+
string(3) "foo"
125+
126+
string(%d) "%ssprintf_rope_optimization.php-%d-1"
127+
128+
string(9) "abcdefghi"
129+
130+
Called
131+
Called
132+
Called
133+
Error: Object of class Foo could not be converted to string in %s:73
134+
Stack trace:
135+
#0 {main}
136+
137+
object(Closure)#3 (2) {
138+
["function"]=>
139+
string(7) "sprintf"
140+
["parameter"]=>
141+
array(2) {
142+
["$format"]=>
143+
string(10) "<required>"
144+
["$values"]=>
145+
string(10) "<optional>"
146+
}
147+
}
148+
149+
string(2) "%s"
150+
151+
string(2) "%s"
152+
153+
154+
Warning: Array to string conversion in %s on line 89
155+
156+
Warning: Array to string conversion in %s on line 89
157+
158+
Warning: Array to string conversion in %s on line 89
159+
string(17) "Array-Array-Array"
160+
161+
Done

0 commit comments

Comments
 (0)