Skip to content

Commit

Permalink
Optimizing memory (zephir-lang#1882)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamsxin committed Nov 6, 2019
1 parent 8515248 commit 5eb70ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
30 changes: 6 additions & 24 deletions kernels/ZendEngine3/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,16 @@ void ZEPHIR_FASTCALL zephir_memory_grow_stack(zephir_method_globals *g, const ch
zephir_memory_entry *active_memory;
size_t i;

active_memory = (zephir_memory_entry *) pecalloc(1, sizeof(zephir_memory_entry), 1);
active_memory = (zephir_memory_entry *) pecalloc(1, sizeof(zephir_memory_entry), 0);

active_memory->addresses = pecalloc(24, sizeof(zval*), 1);
active_memory->addresses = pecalloc(24, sizeof(zval*), 0);
active_memory->capacity = 24;
active_memory->hash_addresses = pecalloc(8, sizeof(zval*), 1);
active_memory->hash_capacity = 8;

g->active_memory = active_memory;
}

assert(g->active_memory != NULL);
assert(g->active_memory->pointer == 0);
assert(g->active_memory->hash_pointer == 0);

#ifndef ZEPHIR_RELEASE
g->active_memory->func = func;
Expand Down Expand Up @@ -139,17 +136,6 @@ void ZEPHIR_FASTCALL zephir_memory_restore_stack(zephir_method_globals *g, const
}
}

/* Check for non freed hash key zvals, mark as null to avoid string freeing */
for (i = 0; i < active_memory->hash_pointer; ++i) {
assert(active_memory->hash_addresses[i] != NULL);
if (!Z_REFCOUNTED_P(active_memory->hash_addresses[i])) continue;
if (Z_REFCOUNT_P(active_memory->hash_addresses[i]) <= 1) {
ZVAL_NULL(active_memory->hash_addresses[i]);
} else {
zval_copy_ctor(active_memory->hash_addresses[i]);
}
}

#ifndef ZEPHIR_RELEASE
for (i = 0; i < active_memory->pointer; ++i) {
if (active_memory->addresses[i] != NULL) {
Expand Down Expand Up @@ -193,15 +179,11 @@ void ZEPHIR_FASTCALL zephir_memory_restore_stack(zephir_method_globals *g, const
active_memory->func = NULL;
#endif

if (active_memory->hash_addresses != NULL) {
pefree(active_memory->hash_addresses, 1);
}

if (active_memory->addresses != NULL) {
pefree(active_memory->addresses, 1);
pefree(active_memory->addresses, 0);
}

pefree(g->active_memory, 1);
pefree(g->active_memory, 0);
g->active_memory = NULL;

#ifndef ZEPHIR_RELEASE
Expand Down Expand Up @@ -358,7 +340,7 @@ int zephir_cleanup_fcache(void *pDest, int num_args, va_list args, zend_hash_key
return ZEND_HASH_APPLY_KEEP;
}

void zephir_do_memory_observe(zval *var, const zephir_method_globals *g)
void ZEPHIR_FASTCALL zephir_do_memory_observe(zval *var, const zephir_method_globals *g)
{
zephir_memory_entry *frame = g->active_memory;
#ifndef ZEPHIR_RELEASE
Expand All @@ -370,7 +352,7 @@ void zephir_do_memory_observe(zval *var, const zephir_method_globals *g)
#endif

if (UNEXPECTED(frame->pointer == frame->capacity)) {
void *buf = perealloc(frame->addresses, sizeof(zval *) * (frame->capacity + 16), 1);
void *buf = perealloc(frame->addresses, sizeof(zval *) * (frame->capacity + 16), 0);
if (EXPECTED(buf != NULL)) {
frame->capacity += 16;
frame->addresses = buf;
Expand Down
9 changes: 3 additions & 6 deletions kernels/ZendEngine3/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ typedef struct _zephir_memory_entry {
size_t pointer;
size_t capacity;
zval **addresses;
size_t hash_pointer;
size_t hash_capacity;
zval **hash_addresses;
#ifndef ZEPHIR_RELEASE
int permanent;
const char *func;
Expand All @@ -66,12 +63,12 @@ void ZEPHIR_FASTCALL zephir_memory_grow_stack(zephir_method_globals *g, const ch
void ZEPHIR_FASTCALL zephir_memory_restore_stack(zephir_method_globals *g, const char *func);

#define ZEPHIR_MM_GROW() \
ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 1); \
ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0); \
zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);

#define ZEPHIR_MM_RESTORE() \
zephir_memory_restore_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__); \
pefree(ZEPHIR_METHOD_GLOBALS_PTR, 1); \
pefree(ZEPHIR_METHOD_GLOBALS_PTR, 0); \
ZEPHIR_METHOD_GLOBALS_PTR = NULL;

void zephir_initialize_memory(zend_zephir_globals_def *zephir_globals_ptr);
Expand All @@ -81,7 +78,7 @@ void zephir_deinitialize_memory();
#define zephir_dtor(x) zval_dtor(x)
#define zephir_ptr_dtor(x) zval_ptr_dtor(x)

void zephir_do_memory_observe(zval *var, const zephir_method_globals *g);
void ZEPHIR_FASTCALL zephir_do_memory_observe(zval *var, const zephir_method_globals *g);
#define zephir_memory_observe(var) zephir_do_memory_observe(var, ZEPHIR_METHOD_GLOBALS_PTR);

#define zephir_safe_zval_ptr_dtor(pzval)
Expand Down

0 comments on commit 5eb70ee

Please sign in to comment.