Skip to content

Commit b4fbf1d

Browse files
committed
Optimize serializing class names
Because of the memcpy, compilers can't infer that ZSTR_LEN (i.e. class_name->len) did not change, so they copy it out of memory into a register for the last two accesses. php_var_serialize_string already does something similar. Closes GH-6734
1 parent b652267 commit b4fbf1d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

ext/standard/var.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,9 +733,10 @@ static inline bool php_var_serialize_class_name(smart_str *buf, zval *struc) /*
733733
PHP_CLASS_ATTRIBUTES;
734734

735735
PHP_SET_CLASS_ATTRIBUTES(struc);
736-
s = zend_print_long_to_buf(b + sizeof(b) - 1, ZSTR_LEN(class_name));
736+
size_t class_name_len = ZSTR_LEN(class_name);
737+
s = zend_print_long_to_buf(b + sizeof(b) - 1, class_name_len);
737738
l = b + sizeof(b) - 1 - s;
738-
new_len = smart_str_alloc(buf, 2 + l + 2 + ZSTR_LEN(class_name) + 2, 0);
739+
new_len = smart_str_alloc(buf, 2 + l + 2 + class_name_len + 2, 0);
739740
res = ZSTR_VAL(buf->s) + ZSTR_LEN(buf->s);
740741
ZSTR_LEN(buf->s) = new_len;
741742
memcpy(res, "O:", 2);
@@ -744,8 +745,8 @@ static inline bool php_var_serialize_class_name(smart_str *buf, zval *struc) /*
744745
res += l;
745746
memcpy(res, ":\"", 2);
746747
res += 2;
747-
memcpy(res, ZSTR_VAL(class_name), ZSTR_LEN(class_name));
748-
res += ZSTR_LEN(class_name);
748+
memcpy(res, ZSTR_VAL(class_name), class_name_len);
749+
res += class_name_len;
749750
memcpy(res, "\":", 2);
750751
PHP_CLEANUP_CLASS_ATTRIBUTES();
751752
return incomplete_class;

0 commit comments

Comments
 (0)