Skip to content

Commit

Permalink
Better fix for #12
Browse files Browse the repository at this point in the history
this will perfer reference in serialize, which will reduce the
serialized length in PHP(with php_only)
  • Loading branch information
laruence committed Feb 18, 2013
1 parent 71c0e03 commit 813e886
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
10 changes: 2 additions & 8 deletions msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ static void msgpack_init_globals(zend_msgpack_globals *msgpack_globals)
msgpack_globals->php_only = 1;

msgpack_globals->illegal_key_insert = 0;
msgpack_globals->serialize.var_hash = NULL;
msgpack_globals->serialize.level = 0;
}

static ZEND_MINIT_FUNCTION(msgpack)
Expand Down Expand Up @@ -147,11 +149,7 @@ PS_SERIALIZER_ENCODE_FUNC(msgpack)

msgpack_serialize_var_init(&var_hash);

#if PHP_API_VERSION < 20100412
msgpack_serialize_zval(&buf, PS(http_session_vars), &var_hash TSRMLS_CC);
#else
msgpack_serialize_zval(&buf, PS(http_session_vars), var_hash TSRMLS_CC);
#endif

if (newlen)
{
Expand Down Expand Up @@ -235,11 +233,7 @@ PHP_MSGPACK_API void php_msgpack_serialize(smart_str *buf, zval *val TSRMLS_DC)

msgpack_serialize_var_init(&var_hash);

#if PHP_API_VERSION < 20100412
msgpack_serialize_zval(buf, val, &var_hash TSRMLS_CC);
#else
msgpack_serialize_zval(buf, val, var_hash TSRMLS_CC);
#endif

msgpack_serialize_var_destroy(&var_hash);
}
Expand Down
4 changes: 0 additions & 4 deletions msgpack_pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

#include "ext/standard/php_var.h"

#if PHP_API_VERSION < 20100412
#define msgpack_serialize_data_t HashTable
#else
typedef HashTable* msgpack_serialize_data_t;
#endif

enum msgpack_serialize_type
{
Expand Down
31 changes: 13 additions & 18 deletions msgpack_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,30 +282,25 @@ inline static zend_class_entry* msgpack_unserialize_class(

void msgpack_serialize_var_init(msgpack_serialize_data_t *var_hash)
{

#if PHP_API_VERSION < 20100412
HashTable *var_hash_ptr = (HashTable *)var_hash;
zend_hash_init(var_hash_ptr, 10, NULL, NULL, 0);
#else
HashTable **var_hash_ptr = (HashTable **)var_hash;
ALLOC_HASHTABLE(*var_hash_ptr);
zend_hash_init(*var_hash_ptr, 10, NULL, NULL, 0);
#endif

if (MSGPACK_G(serialize).level) {
*var_hash_ptr = MSGPACK_G(serialize).var_hash;
} else {
ALLOC_HASHTABLE(*var_hash_ptr);
zend_hash_init(*var_hash_ptr, 10, NULL, NULL, 0);
MSGPACK_G(serialize).var_hash = *var_hash_ptr;
}
MSGPACK_G(serialize).level++;
}

void msgpack_serialize_var_destroy(msgpack_serialize_data_t *var_hash)
{

#if PHP_API_VERSION < 20100412
HashTable *var_hash_ptr = (HashTable *)var_hash;
zend_hash_destroy(var_hash_ptr);
#else
HashTable **var_hash_ptr = (HashTable **)var_hash;
zend_hash_destroy(*var_hash_ptr);
FREE_HASHTABLE(*var_hash_ptr);
#endif

--MSGPACK_G(serialize).level;
if (!MSGPACK_G(serialize).level) {
zend_hash_destroy(*var_hash_ptr);
FREE_HASHTABLE(*var_hash_ptr);
}
}

void msgpack_unserialize_var_init(msgpack_unserialize_data_t *var_hashx)
Expand Down
4 changes: 4 additions & 0 deletions php_msgpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ ZEND_BEGIN_MODULE_GLOBALS(msgpack)
zend_bool error_display;
zend_bool php_only;
zend_bool illegal_key_insert;
struct {
void *var_hash;
unsigned level;
} serialize;
ZEND_END_MODULE_GLOBALS(msgpack)

ZEND_EXTERN_MODULE_GLOBALS(msgpack)
Expand Down

0 comments on commit 813e886

Please sign in to comment.