Skip to content

Commit 5bff86d

Browse files
authored
Merge pull request phpredis#924 from yatsukhnenko/develop
integer_length optimization + redis_array_impl.c refactoring
2 parents 77c8c0f + 9950a11 commit 5bff86d

File tree

4 files changed

+20
-38
lines changed

4 files changed

+20
-38
lines changed

library.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -556,18 +556,20 @@ void add_constant_long(zend_class_entry *ce, char *name, int value) {
556556

557557
int
558558
integer_length(int i) {
559-
int sz = 0;
560-
int ci = abs(i);
561-
while (ci > 0) {
562-
ci /= 10;
559+
int sz = 1;
560+
561+
if (i < 0) { /* allow for neg sign as well. */
562+
i = -i;
563563
sz++;
564564
}
565-
if (i == 0) { /* log 0 doesn't make sense. */
566-
sz = 1;
567-
} else if (i < 0) { /* allow for neg sign as well. */
568-
sz++;
565+
for (;;) {
566+
if (i < 10) return sz;
567+
if (i < 100) return sz + 1;
568+
if (i < 1000) return sz + 2;
569+
// Skip ahead by 4 orders of magnitude
570+
i /= 10000U;
571+
sz += 4;
569572
}
570-
return sz;
571573
}
572574

573575
int

redis_array_impl.c

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -425,17 +425,11 @@ ra_extract_key(RedisArray *ra, const char *key, int key_len, int *out_len TSRMLS
425425
char *start, *end;
426426
*out_len = key_len;
427427

428-
if(ra->z_fun)
428+
if(ra->z_fun) {
429429
return ra_call_extractor(ra, key, key_len, out_len TSRMLS_CC);
430-
431-
/* look for '{' */
432-
start = strchr(key, '{');
433-
if(!start) return estrndup(key, key_len);
434-
435-
/* look for '}' */
436-
end = strchr(start + 1, '}');
437-
if(!end) return estrndup(key, key_len);
438-
430+
} else if ((start = strchr(key, '{')) == NULL || (end = strchr(start + 1, '}')) == NULL) {
431+
return estrndup(key, key_len);
432+
}
439433
/* found substring */
440434
*out_len = end - start - 1;
441435
return estrndup(start + 1, *out_len);
@@ -768,17 +762,6 @@ ra_rehash_scan(zval *z_redis, char ***keys, int **key_lens, const char *cmd, con
768762
return count;
769763
}
770764

771-
static long
772-
ra_rehash_scan_index(zval *z_redis, char ***keys, int **key_lens TSRMLS_DC) {
773-
return ra_rehash_scan(z_redis, keys, key_lens, "SMEMBERS", PHPREDIS_INDEX_NAME TSRMLS_CC);
774-
}
775-
776-
/* list keys using KEYS command */
777-
static long
778-
ra_rehash_scan_keys(zval *z_redis, char ***keys, int **key_lens TSRMLS_DC) {
779-
return ra_rehash_scan(z_redis, keys, key_lens, "KEYS", "*" TSRMLS_CC);
780-
}
781-
782765
/* run TYPE to find the type */
783766
static zend_bool
784767
ra_get_key_type(zval *z_redis, const char *key, int key_len, zval *z_from, long *res TSRMLS_DC) {
@@ -1254,9 +1237,9 @@ ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool
12541237

12551238
/* list all keys */
12561239
if(b_index) {
1257-
count = ra_rehash_scan_index(z_redis, &keys, &key_lens TSRMLS_CC);
1240+
count = ra_rehash_scan(z_redis, &keys, &key_lens, "SMEMBERS", PHPREDIS_INDEX_NAME TSRMLS_CC);
12581241
} else {
1259-
count = ra_rehash_scan_keys(z_redis, &keys, &key_lens TSRMLS_CC);
1242+
count = ra_rehash_scan(z_redis, &keys, &key_lens, "KEYS", "*" TSRMLS_CC);
12601243
}
12611244

12621245
/* callback */
@@ -1274,12 +1257,11 @@ ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool
12741257
/* php_printf("move [%s] from [%s] to [%s]\n", keys[i], hostname, ra->hosts[target_pos]); */
12751258
ra_move_key(keys[i], key_lens[i], z_redis, z_target TSRMLS_CC);
12761259
}
1277-
}
12781260

1279-
/* cleanup */
1280-
for(i = 0; i < count; ++i) {
1261+
/* cleanup */
12811262
efree(keys[i]);
12821263
}
1264+
12831265
efree(keys);
12841266
efree(key_lens);
12851267
}

redis_commands.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3309,4 +3309,4 @@ void redis_unserialize_handler(INTERNAL_FUNCTION_PARAMETERS,
33093309
}
33103310
}
33113311

3312-
/* vim: set tabstop=4 softtabstops=4 noexpandtab shiftwidth=4: */
3312+
/* vim: set tabstop=4 softtabstop=4 expandtab shiftwidth=4: */

redis_session.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
+----------------------------------------------------------------------+
2121
*/
2222

23-
#include "common.h"
24-
2523
#ifdef HAVE_CONFIG_H
2624
#include "config.h"
2725
#endif

0 commit comments

Comments
 (0)