Skip to content

Commit 7b9d54e

Browse files
authored
perf: avoid stack accessor calls for output buffering (#25300)
* avoid stack accessor calls for output buffering * move to header with zend_always_inline instead * move macro definition * move other small functions too
1 parent 7efad6d commit 7b9d54e

3 files changed

Lines changed: 31 additions & 42 deletions

File tree

‎Zend/zend_stack.c‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "zend.h"
2020
#include "zend_stack.h"
2121

22-
#define ZEND_STACK_ELEMENT(stack, n) ((void *)((char *) (stack)->elements + (stack)->size * (n)))
23-
2422
ZEND_API void zend_stack_init(zend_stack *stack, int size)
2523
{
2624
stack->size = size;
@@ -41,22 +39,6 @@ ZEND_API int zend_stack_push(zend_stack *stack, const void *element)
4139
}
4240

4341

44-
ZEND_API void *zend_stack_top(const zend_stack *stack)
45-
{
46-
if (stack->top > 0) {
47-
return ZEND_STACK_ELEMENT(stack, stack->top - 1);
48-
} else {
49-
return NULL;
50-
}
51-
}
52-
53-
54-
ZEND_API void zend_stack_del_top(zend_stack *stack)
55-
{
56-
--stack->top;
57-
}
58-
59-
6042
ZEND_API int zend_stack_int_top(const zend_stack *stack)
6143
{
6244
int *e = zend_stack_top(stack);
@@ -68,12 +50,6 @@ ZEND_API int zend_stack_int_top(const zend_stack *stack)
6850
}
6951

7052

71-
ZEND_API bool zend_stack_is_empty(const zend_stack *stack)
72-
{
73-
return stack->top == 0;
74-
}
75-
76-
7753
ZEND_API void zend_stack_destroy(zend_stack *stack)
7854
{
7955
if (stack->elements) {
@@ -83,18 +59,6 @@ ZEND_API void zend_stack_destroy(zend_stack *stack)
8359
}
8460

8561

86-
ZEND_API void *zend_stack_base(const zend_stack *stack)
87-
{
88-
return stack->elements;
89-
}
90-
91-
92-
ZEND_API int zend_stack_count(const zend_stack *stack)
93-
{
94-
return stack->top;
95-
}
96-
97-
9862
ZEND_API void zend_stack_apply(zend_stack *stack, int type, int (*apply_function)(void *element))
9963
{
10064
int i;

‎Zend/zend_stack.h‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct _zend_stack {
2828

2929

3030
#define STACK_BLOCK_SIZE 16
31+
#define ZEND_STACK_ELEMENT(stack, n) ((void *)((char *) (stack)->elements + (stack)->size * (n)))
3132

3233
typedef enum {
3334
ZEND_STACK_APPLY_TOPDOWN,
@@ -37,16 +38,40 @@ typedef enum {
3738
BEGIN_EXTERN_C()
3839
ZEND_API void zend_stack_init(zend_stack *stack, int size);
3940
ZEND_API int zend_stack_push(zend_stack *stack, const void *element);
40-
ZEND_API void *zend_stack_top(const zend_stack *stack);
41-
ZEND_API void zend_stack_del_top(zend_stack *stack);
4241
ZEND_API int zend_stack_int_top(const zend_stack *stack);
43-
ZEND_API bool zend_stack_is_empty(const zend_stack *stack);
4442
ZEND_API void zend_stack_destroy(zend_stack *stack);
45-
ZEND_API void *zend_stack_base(const zend_stack *stack);
46-
ZEND_API int zend_stack_count(const zend_stack *stack);
4743
ZEND_API void zend_stack_apply(zend_stack *stack, int type, int (*apply_function)(void *element));
4844
ZEND_API void zend_stack_apply_with_argument(zend_stack *stack, zend_stack_apply_direction type, int (*apply_function)(void *element, void *arg), void *arg);
4945
ZEND_API void zend_stack_clean(zend_stack *stack, void (*func)(void *), bool free_elements);
5046
END_EXTERN_C()
5147

48+
static zend_always_inline void *zend_stack_top(const zend_stack *stack)
49+
{
50+
if (stack->top > 0) {
51+
return ZEND_STACK_ELEMENT(stack, stack->top - 1);
52+
} else {
53+
return NULL;
54+
}
55+
}
56+
57+
static zend_always_inline void zend_stack_del_top(zend_stack *stack)
58+
{
59+
--stack->top;
60+
}
61+
62+
static zend_always_inline bool zend_stack_is_empty(const zend_stack *stack)
63+
{
64+
return stack->top == 0;
65+
}
66+
67+
static zend_always_inline void *zend_stack_base(const zend_stack *stack)
68+
{
69+
return stack->elements;
70+
}
71+
72+
static zend_always_inline int zend_stack_count(const zend_stack *stack)
73+
{
74+
return stack->top;
75+
}
76+
5277
#endif /* ZEND_STACK_H */

‎main/output.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ static inline void php_output_op(int op, const char *str, size_t len)
11111111

11121112
if (obh_cnt > 1) {
11131113
zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_op, &context);
1114-
} else if ((active = zend_stack_top(&OG(handlers))) && (!((*active)->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
1114+
} else if ((active = zend_stack_base(&OG(handlers))) && (!((*active)->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
11151115
php_output_handler_op(*active, &context);
11161116
} else {
11171117
php_output_context_pass(&context);

0 commit comments

Comments
 (0)