Skip to content

Commit 5275e55

Browse files
committed
Changed zend_smart_str allocation granularity to do the better job together with Zend MM and avoid useless calls to erealloc().
1 parent a759967 commit 5275e55

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

Zend/zend_alloc.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,6 @@ struct _zend_mm_bin {
287287
char bytes[ZEND_MM_PAGE_SIZE * 8];
288288
};
289289

290-
#if ZEND_DEBUG
291-
typedef struct _zend_mm_debug_info {
292-
size_t size;
293-
const char *filename;
294-
const char *orig_filename;
295-
uint lineno;
296-
uint orig_lineno;
297-
} zend_mm_debug_info;
298-
#endif
299-
300290
struct _zend_mm_free_slot {
301291
zend_mm_free_slot *next_free_slot;
302292
};

Zend/zend_alloc.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ typedef struct _zend_leak_info {
5050
uint orig_lineno;
5151
} zend_leak_info;
5252

53+
#if ZEND_DEBUG
54+
typedef struct _zend_mm_debug_info {
55+
size_t size;
56+
const char *filename;
57+
const char *orig_filename;
58+
uint lineno;
59+
uint orig_lineno;
60+
} zend_mm_debug_info;
61+
62+
# define ZEND_MM_OVERHEAD ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))
63+
#else
64+
# define ZEND_MM_OVERHEAD 0
65+
#endif
66+
5367
BEGIN_EXTERN_C()
5468

5569
ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length) ZEND_ATTRIBUTE_MALLOC;

Zend/zend_smart_str.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222
#include <zend.h>
2323
#include "zend_smart_str_public.h"
2424

25-
#ifndef SMART_STR_PREALLOC
26-
#define SMART_STR_PREALLOC 128
25+
#define SMART_STR_OVERHEAD (ZEND_MM_OVERHEAD + _STR_HEADER_SIZE)
26+
27+
#ifndef SMART_STR_PAGE
28+
# define SMART_STR_PAGE 4096
2729
#endif
2830

2931
#ifndef SMART_STR_START_SIZE
30-
#define SMART_STR_START_SIZE 78
32+
# define SMART_STR_START_SIZE (256 - SMART_STR_OVERHEAD - 1)
3133
#endif
3234

35+
#define SMART_STR_NEW_SIZE(newlen) \
36+
(((newlen + SMART_STR_OVERHEAD + SMART_STR_PAGE) & ~(SMART_STR_PAGE - 1)) - SMART_STR_OVERHEAD - 1)
37+
3338
#define smart_str_appends_ex(dest, src, what) \
3439
smart_str_appendl_ex((dest), (src), strlen(src), (what))
3540
#define smart_str_appends(dest, src) \
@@ -55,14 +60,14 @@ static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, zen
5560
newlen = len;
5661
str->a = newlen < SMART_STR_START_SIZE
5762
? SMART_STR_START_SIZE
58-
: newlen + SMART_STR_PREALLOC;
63+
: SMART_STR_NEW_SIZE(newlen);
5964
str->s = zend_string_alloc(str->a, persistent);
6065
str->s->len = 0;
6166
} else {
6267
newlen = str->s->len + len;
6368
if (newlen >= str->a) {
64-
str->a = newlen + SMART_STR_PREALLOC;
65-
str->s = (zend_string *) perealloc(str->s, _STR_HEADER_SIZE + str->a + 1, persistent);
69+
str->a = SMART_STR_NEW_SIZE(newlen);
70+
str->s = (zend_string *) perealloc2(str->s, _STR_HEADER_SIZE + str->a + 1, _STR_HEADER_SIZE + str->s->len + 1, persistent);
6671
}
6772
}
6873
return newlen;

0 commit comments

Comments
 (0)