Skip to content

Commit 4ce16ac

Browse files
committed
fix #90
improve performance by 10-300% (more for deeply nested data)
1 parent c006288 commit 4ce16ac

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

msgpack_unpack.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static zval *msgpack_var_push(msgpack_unserialize_data_t *var_hashx) /* {{{ */ {
7979
return NULL;
8080
}
8181

82-
var_hash = var_hashx->first;
82+
var_hash = var_hashx->last;
8383

8484
while (var_hash && var_hash->used_slots == VAR_ENTRIES_MAX) {
8585
prev = var_hash;
@@ -96,6 +96,7 @@ static zval *msgpack_var_push(msgpack_unserialize_data_t *var_hashx) /* {{{ */ {
9696
} else {
9797
prev->next = var_hash;
9898
}
99+
var_hashx->last = var_hash;
99100
}
100101

101102
return &var_hash->data[var_hash->used_slots++];
@@ -140,7 +141,7 @@ static zval *msgpack_stack_push(msgpack_unserialize_data_t *var_hashx) /* {{{ */
140141
return NULL;
141142
}
142143

143-
var_hash = var_hashx->first_dtor;
144+
var_hash = var_hashx->last_dtor;
144145

145146
while (var_hash && var_hash->used_slots == VAR_ENTRIES_MAX) {
146147
prev = var_hash;
@@ -157,14 +158,15 @@ static zval *msgpack_stack_push(msgpack_unserialize_data_t *var_hashx) /* {{{ */
157158
} else {
158159
prev->next = var_hash;
159160
}
161+
var_hashx->last_dtor = var_hash;
160162
}
161163

162164
return &var_hash->data[var_hash->used_slots++];
163165
}
164166
/* }}} */
165167

166168
static void msgpack_stack_pop(msgpack_unserialize_data_t *var_hashx, zval *v) /* {{{ */ {
167-
var_entries *var_hash = var_hashx->first_dtor;
169+
var_entries *var_hash = var_hashx->last_dtor;
168170

169171
while (var_hash && var_hash->used_slots == VAR_ENTRIES_MAX) {
170172
var_hash = var_hash->next;
@@ -289,8 +291,8 @@ static zend_class_entry* msgpack_unserialize_class(zval **container, zend_string
289291
/* }}} */
290292

291293
void msgpack_unserialize_var_init(msgpack_unserialize_data_t *var_hashx) /* {{{ */ {
292-
var_hashx->first = 0;
293-
var_hashx->first_dtor = 0;
294+
var_hashx->first = var_hashx->last = NULL;
295+
var_hashx->first_dtor = var_hashx->last_dtor = NULL;
294296
}
295297
/* }}} */
296298

@@ -474,7 +476,7 @@ int msgpack_unserialize_ext(msgpack_unserialize_data *unpack, const char* base,
474476
int msgpack_unserialize_array(msgpack_unserialize_data *unpack, unsigned int count, zval **obj) /* {{{ */ {
475477
MSGPACK_UNSERIALIZE_ALLOC_VALUE(unpack);
476478

477-
array_init(*obj);
479+
array_init_size(*obj, count);
478480

479481
if (count) {
480482
unpack->stack[unpack->deps++] = count;
@@ -506,6 +508,7 @@ int msgpack_unserialize_map(msgpack_unserialize_data *unpack, unsigned int count
506508
}
507509

508510
unpack->type = MSGPACK_SERIALIZE_TYPE_NONE;
511+
unpack->count = count;
509512

510513
if (count == 0) {
511514
if (MSGPACK_G(php_only)) {
@@ -692,7 +695,7 @@ int msgpack_unserialize_map_item(msgpack_unserialize_data *unpack, zval **contai
692695
}
693696
} else {
694697
if (Z_TYPE_P(container_val) != IS_ARRAY) {
695-
array_init(container_val);
698+
array_init_size(container_val, unpack->count);
696699
}
697700
switch (Z_TYPE_P(key)) {
698701
case IS_LONG:

msgpack_unpack.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ typedef struct _msgpack_unserialize_data {
2727

2828
typedef struct {
2929
zval *retval;
30-
long deps;
30+
const char *eof;
3131
msgpack_unserialize_data_t *var_hash;
32-
long stack[MSGPACK_EMBED_STACK_SIZE];
3332
int type;
34-
const char *eof;
33+
unsigned int count;
34+
long deps;
35+
long stack[MSGPACK_EMBED_STACK_SIZE];
3536
} msgpack_unserialize_data;
3637

3738
void msgpack_unserialize_var_init(msgpack_unserialize_data_t *var_hashx);

0 commit comments

Comments
 (0)