Skip to content

Commit 1cc4262

Browse files
committed
decoder: Code style
1 parent b7522ac commit 1cc4262

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/simdjson_decoder.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static zend_always_inline void simdjson_set_zval_to_string(zval *v, const char *
192192

193193
#if PHP_VERSION_ID >= 80200
194194
// Copy of PHP method zend_hash_str_find_bucket that is not exported without checking if p->key is not null
195-
static zend_always_inline Bucket *simdjson_zend_hash_str_find_bucket(const HashTable *ht, const char *str, size_t len, zend_ulong h) {
195+
static zend_always_inline Bucket *simdjson_hash_str_find_bucket(const HashTable *ht, const char *str, size_t len, zend_ulong h) {
196196
uint32_t nIndex;
197197
uint32_t idx;
198198
Bucket *p, *arData;
@@ -229,12 +229,13 @@ static zend_always_inline void simdjson_init_reused_key_strings(HashTable *ht) {
229229
ht->nNumUsed = 0;
230230
ht->nTableSize = SIMDJSON_DEDUP_STRING_COUNT;
231231
// zend_hash_real_init_mixed
232-
void * data = emalloc(HT_SIZE_EX(SIMDJSON_DEDUP_STRING_COUNT, HT_SIZE_TO_MASK(SIMDJSON_DEDUP_STRING_COUNT)));
232+
void *data = emalloc(HT_SIZE_EX(SIMDJSON_DEDUP_STRING_COUNT, HT_SIZE_TO_MASK(SIMDJSON_DEDUP_STRING_COUNT)));
233233
ht->nTableMask = HT_SIZE_TO_MASK(SIMDJSON_DEDUP_STRING_COUNT);
234234
HT_SET_DATA_ADDR(ht, data);
235235
HT_HASH_RESET(ht);
236236
} else if (ht->nNumUsed > SIMDJSON_DEDUP_STRING_COUNT / 2) {
237-
// more than half of hash table is already full, cleanup
237+
// more than half of hash table is already full before decoding new structure, so we will make space for new keys
238+
// by removing old keys
238239
simdjson_release_reused_key_strings(ht);
239240
ZEND_ASSERT(ht->nTableMask == HT_SIZE_TO_MASK(SIMDJSON_DEDUP_STRING_COUNT));
240241
HT_HASH_RESET(ht);
@@ -253,15 +254,15 @@ static zend_always_inline zend_string* simdjson_dedup_key(HashTable *ht, const c
253254
Bucket *p;
254255
zend_string *key;
255256

256-
if (len > SIMDJSON_MAX_DEDUP_LENGTH) {
257+
if (UNEXPECTED(len > SIMDJSON_MAX_DEDUP_LENGTH)) {
257258
goto init_new_string;
258259
}
259260

260261
// This should make computation faster, as we know array size
261262
ZEND_ASSERT(ht != NULL);
262263
ZEND_ASSERT(ht->nTableMask == HT_SIZE_TO_MASK(SIMDJSON_DEDUP_STRING_COUNT));
263264

264-
p = simdjson_zend_hash_str_find_bucket(ht, str, len, h);
265+
p = simdjson_hash_str_find_bucket(ht, str, len, h);
265266
if (p) { // Key already exists, reuse
266267
GC_ADDREF(p->key); // raise reference counter by one
267268
return p->key;
@@ -274,7 +275,7 @@ static zend_always_inline zend_string* simdjson_dedup_key(HashTable *ht, const c
274275
idx = ht->nNumUsed++;
275276
p = ht->arData + idx;
276277
p->key = simdjson_string_init(str, len); // initialize new string for key
277-
GC_ADDREF(p->key); // raise gc counter by one, so it will be 2
278+
GC_SET_REFCOUNT(p->key, 2);
278279
p->h = ZSTR_H(p->key) = h;
279280
//ZVAL_NULL(&p->val); // we dont need set value to null, as we don't use it and destructor is set to NULL
280281
nIndex = h | ht->nTableMask;
@@ -291,7 +292,7 @@ static zend_always_inline zend_string* simdjson_dedup_key(HashTable *ht, const c
291292
* - initialized array as zend_hash_real_init_mixed
292293
* - exact size must be known in advance
293294
*/
294-
static zend_always_inline void simdjson_zend_hash_str_add_or_update(HashTable *ht, const char *str, size_t len, zval *pData, HashTable *dedup_key_strings) {
295+
static zend_always_inline void simdjson_hash_str_add_or_update(HashTable *ht, const char *str, size_t len, zval *pData, HashTable *dedup_key_strings) {
295296
uint32_t nIndex;
296297
uint32_t idx;
297298
Bucket *p;
@@ -306,7 +307,7 @@ static zend_always_inline void simdjson_zend_hash_str_add_or_update(HashTable *h
306307
// Compute key hash
307308
h = zend_inline_hash_func(str, len);
308309

309-
p = simdjson_zend_hash_str_find_bucket(ht, str, len, h);
310+
p = simdjson_hash_str_find_bucket(ht, str, len, h);
310311
if (UNEXPECTED(p)) { // Key already exists, replace value
311312
zval *data;
312313
ZEND_ASSERT(&p->val != pData);
@@ -318,8 +319,7 @@ static zend_always_inline void simdjson_zend_hash_str_add_or_update(HashTable *h
318319
ht->nNumOfElements++;
319320
p = ht->arData + idx;
320321
p->key = simdjson_dedup_key(dedup_key_strings, str, len, h); // initialize new string for key
321-
//p->key = simdjson_string_init(str, len);
322-
p->h = /* ZSTR_H(p->key) =*/ h;
322+
p->h = h;
323323
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
324324
ZVAL_COPY_VALUE(&p->val, pData);
325325
nIndex = h | ht->nTableMask;
@@ -333,13 +333,14 @@ static zend_always_inline void simdjson_add_key_to_symtable(HashTable *ht, const
333333
#if PHP_VERSION_ID >= 80200
334334
zend_ulong idx;
335335
if (UNEXPECTED(ZEND_HANDLE_NUMERIC_STR(buf, len, idx))) {
336-
zend_hash_index_update(ht, idx, value); // if index is inter in string format, use integer as index
336+
// if index is inter in string format, use integer as index
337+
zend_hash_index_update(ht, idx, value);
337338
} else if (UNEXPECTED(len <= 1)) {
338339
// Use interned string
339340
zend_string *key = len == 1 ? ZSTR_CHAR((unsigned char)buf[0]) : ZSTR_EMPTY_ALLOC();
340341
zend_hash_update(ht, key, value);
341342
} else {
342-
simdjson_zend_hash_str_add_or_update(ht, buf, len, value, dedup_key_strings);
343+
simdjson_hash_str_add_or_update(ht, buf, len, value, dedup_key_strings);
343344
}
344345
#else
345346
if (len <= 1) {

0 commit comments

Comments
 (0)