Skip to content

Commit caffc1c

Browse files
committed
Preallocate zend_hash instead of allocation/deallocation it on each request
1 parent f33837f commit caffc1c

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

Zend/zend_stack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ ZEND_API int zend_stack_destroy(zend_stack *stack)
9696
for (i = 0; i < stack->top; i++) {
9797
efree(stack->elements[i]);
9898
}
99-
10099
efree(stack->elements);
100+
stack->elements = NULL;
101101
}
102102

103103
return SUCCESS;

main/output.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ PHPAPI int php_output_activate(TSRMLS_D)
116116
memset(&output_globals, 0, sizeof(zend_output_globals));
117117
#endif
118118

119-
OG(handlers) = emalloc(sizeof(zend_stack));
120-
if (SUCCESS != zend_stack_init(OG(handlers))) {
121-
return FAILURE;
122-
}
119+
zend_stack_init(&OG(handlers));
123120

124121
return SUCCESS;
125122
}
@@ -135,14 +132,12 @@ PHPAPI void php_output_deactivate(TSRMLS_D)
135132
OG(running) = NULL;
136133

137134
/* release all output handlers */
138-
if (OG(handlers)) {
139-
while (SUCCESS == zend_stack_top(OG(handlers), (void *) &handler)) {
135+
if (OG(handlers).elements) {
136+
while (SUCCESS == zend_stack_top(&OG(handlers), (void *) &handler)) {
140137
php_output_handler_free(handler TSRMLS_CC);
141-
zend_stack_del_top(OG(handlers));
138+
zend_stack_del_top(&OG(handlers));
142139
}
143-
zend_stack_destroy(OG(handlers));
144-
efree(OG(handlers));
145-
OG(handlers) = NULL;
140+
zend_stack_destroy(&OG(handlers));
146141
}
147142
}
148143
/* }}} */
@@ -218,9 +213,9 @@ PHPAPI int php_output_flush(TSRMLS_D)
218213
php_output_context_init(&context, PHP_OUTPUT_HANDLER_FLUSH TSRMLS_CC);
219214
php_output_handler_op(OG(active), &context);
220215
if (context.out.data && context.out.used) {
221-
zend_stack_del_top(OG(handlers));
216+
zend_stack_del_top(&OG(handlers));
222217
php_output_write(context.out.data, context.out.used TSRMLS_CC);
223-
zend_stack_push(OG(handlers), &OG(active), sizeof(php_output_handler *));
218+
zend_stack_push(&OG(handlers), &OG(active), sizeof(php_output_handler *));
224219
}
225220
php_output_context_dtor(&context);
226221
return SUCCESS;
@@ -264,7 +259,7 @@ PHPAPI void php_output_clean_all(TSRMLS_D)
264259

265260
if (OG(active)) {
266261
php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN TSRMLS_CC);
267-
zend_stack_apply_with_argument(OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_clean, &context);
262+
zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_clean, &context);
268263
}
269264
}
270265

@@ -312,7 +307,7 @@ PHPAPI void php_output_discard_all(TSRMLS_D)
312307
Get output buffering level, ie. how many output handlers the stack contains */
313308
PHPAPI int php_output_get_level(TSRMLS_D)
314309
{
315-
return OG(active) ? zend_stack_count(OG(handlers)) : 0;
310+
return OG(active) ? zend_stack_count(&OG(handlers)) : 0;
316311
}
317312
/* }}} */
318313

@@ -500,7 +495,7 @@ PHPAPI int php_output_handler_start(php_output_handler *handler TSRMLS_DC)
500495
}
501496
}
502497
/* zend_stack_push never returns SUCCESS but FAILURE or stack level */
503-
if (FAILURE == (handler->level = zend_stack_push(OG(handlers), &handler, sizeof(php_output_handler *)))) {
498+
if (FAILURE == (handler->level = zend_stack_push(&OG(handlers), &handler, sizeof(php_output_handler *)))) {
504499
return FAILURE;
505500
}
506501
OG(active) = handler;
@@ -516,7 +511,7 @@ PHPAPI int php_output_handler_started(const char *name, size_t name_len TSRMLS_D
516511
int i, count = php_output_get_level(TSRMLS_C);
517512

518513
if (count) {
519-
handlers = *(php_output_handler ***) zend_stack_base(OG(handlers));
514+
handlers = *(php_output_handler ***) zend_stack_base(&OG(handlers));
520515

521516
for (i = 0; i < count; ++i) {
522517
if (name_len == handlers[i]->name_len && !memcmp(handlers[i]->name, name, name_len)) {
@@ -977,13 +972,13 @@ static inline void php_output_op(int op, const char *str, size_t len TSRMLS_DC)
977972
* - apply op to the one active handler; note that OG(active) might be popped off the stack on a flush
978973
* - or apply op to the handler stack
979974
*/
980-
if (OG(active) && (obh_cnt = zend_stack_count(OG(handlers)))) {
975+
if (OG(active) && (obh_cnt = zend_stack_count(&OG(handlers)))) {
981976
context.in.data = (char *) str;
982977
context.in.used = len;
983978

984979
if (obh_cnt > 1) {
985-
zend_stack_apply_with_argument(OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_op, &context);
986-
} else if ((SUCCESS == zend_stack_top(OG(handlers), (void *) &active)) && (!((*active)->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
980+
zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_op, &context);
981+
} else if ((SUCCESS == zend_stack_top(&OG(handlers), (void *) &active)) && (!((*active)->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
987982
php_output_handler_op(*active, &context);
988983
} else {
989984
php_output_context_pass(&context);
@@ -1161,8 +1156,8 @@ static inline int php_output_stack_pop(int flags TSRMLS_DC)
11611156
}
11621157

11631158
/* pop it off the stack */
1164-
zend_stack_del_top(OG(handlers));
1165-
if (SUCCESS == zend_stack_top(OG(handlers), (void *) &current)) {
1159+
zend_stack_del_top(&OG(handlers));
1160+
if (SUCCESS == zend_stack_top(&OG(handlers), (void *) &current)) {
11661161
OG(active) = *current;
11671162
} else {
11681163
OG(active) = NULL;
@@ -1411,7 +1406,7 @@ PHP_FUNCTION(ob_list_handlers)
14111406
return;
14121407
}
14131408

1414-
zend_stack_apply_with_argument(OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_list, return_value);
1409+
zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_list, return_value);
14151410
}
14161411
/* }}} */
14171412

@@ -1430,7 +1425,7 @@ PHP_FUNCTION(ob_get_status)
14301425

14311426
array_init(return_value);
14321427
if (full_status) {
1433-
zend_stack_apply_with_argument(OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_status, return_value);
1428+
zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_status, return_value);
14341429
} else {
14351430
php_output_handler_status(OG(active), return_value);
14361431
}

main/php_output.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ typedef struct _php_output_handler {
142142

143143
ZEND_BEGIN_MODULE_GLOBALS(output)
144144
int flags;
145-
zend_stack *handlers;
145+
zend_stack handlers;
146146
php_output_handler *active;
147147
php_output_handler *running;
148148
char *output_start_filename;

0 commit comments

Comments
 (0)