Skip to content

Commit

Permalink
optimise
Browse files Browse the repository at this point in the history
  • Loading branch information
salsaman committed Jul 18, 2019
1 parent bd8a4cc commit af24c74
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
2 changes: 1 addition & 1 deletion libweed/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ docs-doxygen:
@echo "Doxygen not installed, skipping sourcecode documentation"
endif

WEED_SO_VERSION = 0:10:0
WEED_SO_VERSION = 0:11:0
WEED_PSO_VERSION = 0:14:0

LIBS =
Expand Down
29 changes: 22 additions & 7 deletions libweed/weed-gslice.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ static int weed_seed_is_ptr(int seed) GNU_CONST;
static int weed_strcmp(const char *st1, const char *st2) GNU_HOT;
static size_t weed_strlen(const char *string) GNU_PURE;
static char *weed_slice_strdup(const char *string) GNU_PURE GNU_HOT GNU_FLATTEN;
static uint32_t weed_hash(const char *string) GNU_PURE;

/* host only functions */
static void _weed_plant_free(weed_plant_t *plant) GNU_FLATTEN;
Expand Down Expand Up @@ -168,6 +169,14 @@ static inline int weed_strcmp(const char *st1, const char *st2) {
}


static inline uint32_t weed_hash(const char *string) {
char c;
uint32_t hash = 5381;
while ((c = *(string++)) != 0) hash += (hash << 5) + c;
return hash;
}


static inline int weed_seed_is_ptr(int seed) {
return (seed != WEED_SEED_BOOLEAN && seed != WEED_SEED_INT && seed != WEED_SEED_DOUBLE && seed != WEED_SEED_STRING && seed !=
WEED_SEED_INT64) ? 1 : 0;
Expand Down Expand Up @@ -231,8 +240,10 @@ static inline weed_data_t **weed_data_new(int seed_type, int num_elems, void *va


static inline weed_leaf_t *weed_find_leaf(weed_plant_t *leaf, const char *key) {
uint32_t hash = weed_hash(key);
while (leaf != NULL) {
if (!weed_strcmp((char *)leaf->key, (char *)key)) return leaf;
if (hash == leaf->key_hash)
if (!weed_strcmp((char *)leaf->key, (char *)key)) return leaf;
leaf = leaf->next;
}
return NULL;
Expand All @@ -253,6 +264,7 @@ static inline weed_leaf_t *weed_leaf_new(const char *key, int seed) {
g_slice_free(weed_leaf_t, leaf);
return NULL;
}
leaf->key_hash = weed_hash(key);
leaf->seed_type = seed;
leaf->data = NULL;
leaf->next = NULL;
Expand Down Expand Up @@ -290,13 +302,16 @@ static void _weed_plant_free(weed_plant_t *leaf) {

static int _weed_leaf_delete(weed_plant_t *plant, const char *key) {
weed_leaf_t *leaf = plant, *leafnext;
uint32_t hash = weed_hash(key);
while (leaf->next != NULL) {
if (!weed_strcmp((char *)leaf->next->key, (char *)key)) {
if (leaf->next->flags & WEED_LEAF_READONLY_HOST) return WEED_ERROR_LEAF_READONLY;
leafnext = leaf->next;
leaf->next = leaf->next->next;
weed_leaf_free(leafnext);
return WEED_NO_ERROR;
if (leaf->next->key_hash == hash) {
if (!weed_strcmp((char *)leaf->next->key, (char *)key)) {
if (leaf->next->flags & WEED_LEAF_READONLY_HOST) return WEED_ERROR_LEAF_READONLY;
leafnext = leaf->next;
leaf->next = leaf->next->next;
weed_leaf_free(leafnext);
return WEED_NO_ERROR;
}
}
leaf = leaf->next;
}
Expand Down
28 changes: 21 additions & 7 deletions libweed/weed-gslice_scripting.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ static inline int weed_strcmp(const char *st1, const char *st2) {
}


static inline uint32_t weed_hash(const char *string) {
char c;
uint32_t hash = 5381;
while ((c = *(string++)) != 0) hash += (hash << 5) + c;
return hash;
}


static inline int weed_seed_is_ptr(int seed) {
return (seed != WEED_SEED_BOOLEAN && seed != WEED_SEED_INT && seed != WEED_SEED_DOUBLE && seed != WEED_SEED_STRING && seed !=
WEED_SEED_INT64) ? 1 : 0;
Expand Down Expand Up @@ -195,8 +203,10 @@ static inline weed_data_t **weed_data_new(int seed_type, int num_elems, void *va


static inline weed_leaf_t *weed_find_leaf(weed_plant_t *leaf, const char *key) {
uint32_t hash = weed_hash(key);
while (leaf != NULL) {
if (!weed_strcmp((char *)leaf->key, (char *)key)) return leaf;
if (hash == leaf->key_hash)
if (!weed_strcmp((char *)leaf->key, (char *)key)) return leaf;
leaf = leaf->next;
}
return NULL;
Expand All @@ -217,6 +227,7 @@ static inline weed_leaf_t *weed_leaf_new(const char *key, int seed) {
g_slice_free(weed_leaf_t, leaf);
return NULL;
}
leaf->key_hash = weed_hash(key);
leaf->seed_type = seed;
leaf->data = NULL;
leaf->next = NULL;
Expand Down Expand Up @@ -254,13 +265,16 @@ void weed_plant_free(weed_plant_t *leaf) {

int weed_leaf_delete(weed_plant_t *plant, const char *key) {
weed_leaf_t *leaf = plant, *leafnext;
uint32_t hash = weed_hash(key);
while (leaf->next != NULL) {
if (!weed_strcmp((char *)leaf->next->key, (char *)key)) {
if (leaf->next->flags & WEED_LEAF_READONLY_HOST) return WEED_ERROR_LEAF_READONLY;
leafnext = leaf->next;
leaf->next = leaf->next->next;
weed_leaf_free(leafnext);
return WEED_NO_ERROR;
if (leaf->next->key_hash == hash) {
if (!weed_strcmp((char *)leaf->next->key, (char *)key)) {
if (leaf->next->flags & WEED_LEAF_READONLY_HOST) return WEED_ERROR_LEAF_READONLY;
leafnext = leaf->next;
leaf->next = leaf->next->next;
weed_leaf_free(leafnext);
return WEED_NO_ERROR;
}
}
leaf = leaf->next;
}
Expand Down
29 changes: 22 additions & 7 deletions libweed/weed.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static int weed_seed_is_ptr(int seed) GNU_CONST;

static int weed_strcmp(const char *st1, const char *st2) GNU_HOT;
static size_t weed_strlen(const char *string) GNU_PURE;
static uint32_t weed_hash(const char *string) GNU_PURE;

/* host only functions */
static void _weed_plant_free(weed_plant_t *plant) GNU_FLATTEN;
Expand Down Expand Up @@ -145,6 +146,14 @@ static inline int weed_strcmp(const char *st1, const char *st2) {
}


static inline uint32_t weed_hash(const char *string) {
char c;
uint32_t hash = 5381;
while ((c = *(string++)) != 0) hash += (hash << 5) + c;
return hash;
}


static inline int weed_seed_is_ptr(int seed) {
return (seed != WEED_SEED_BOOLEAN && seed != WEED_SEED_INT && seed != WEED_SEED_DOUBLE && seed != WEED_SEED_STRING &&
seed != WEED_SEED_INT64) ? 1 : 0;
Expand Down Expand Up @@ -208,8 +217,10 @@ static inline weed_data_t **weed_data_new(int seed_type, int num_elems, void *va


static inline weed_leaf_t *weed_find_leaf(weed_plant_t *leaf, const char *key) {
uint32_t hash = weed_hash(key);
while (leaf != NULL) {
if (!weed_strcmp((char *)leaf->key, (char *)key)) return leaf;
if (hash == leaf->key_hash)
if (!weed_strcmp((char *)leaf->key, (char *)key)) return leaf;
leaf = leaf->next;
}
return NULL;
Expand All @@ -230,6 +241,7 @@ static inline weed_leaf_t *weed_leaf_new(const char *key, int seed) {
weed_free(leaf);
return NULL;
}
leaf->key_hash = weed_hash(key);
leaf->seed_type = seed;
leaf->data = NULL;
leaf->next = NULL;
Expand Down Expand Up @@ -268,13 +280,16 @@ static void _weed_plant_free(weed_plant_t *leaf) {

static int _weed_leaf_delete(weed_plant_t *plant, const char *key) {
weed_leaf_t *leaf = plant, *leafnext;
uint32_t hash = weed_hash(key);
while (leaf->next != NULL) {
if (!weed_strcmp((char *)leaf->next->key, (char *)key)) {
if (leaf->next->flags & WEED_LEAF_READONLY_HOST) return WEED_ERROR_LEAF_READONLY;
leafnext = leaf->next;
leaf->next = leaf->next->next;
weed_leaf_free(leafnext);
return WEED_NO_ERROR;
if (leaf->next->key_hash == hash) {
if (!weed_strcmp((char *)leaf->next->key, (char *)key)) {
if (leaf->next->flags & WEED_LEAF_READONLY_HOST) return WEED_ERROR_LEAF_READONLY;
leafnext = leaf->next;
leaf->next = leaf->next->next;
weed_leaf_free(leafnext);
return WEED_NO_ERROR;
}
}
leaf = leaf->next;
}
Expand Down
1 change: 1 addition & 0 deletions libweed/weed.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ typedef size_t weed_size_t; // may be set to uint32_t or uint64_t
/* private data - these fields must NOT be accessed directly ! */
struct weed_leaf {
const char *key;
uint32_t key_hash;
int seed_type;
int num_elements;
weed_data_t **data;
Expand Down

0 comments on commit af24c74

Please sign in to comment.