Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory usage of ART #1907

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/include/art.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ typedef int(*art_callback)(void *data, const unsigned char *key, uint32_t key_le
* of all the various node sizes
*/
typedef struct {
uint32_t partial_len;
uint8_t type;
uint8_t num_children;
uint32_t partial_len;
unsigned char partial[MAX_PREFIX_LEN];
} art_node;

Expand Down Expand Up @@ -103,11 +103,13 @@ typedef struct {
* Represents a leaf. These are
* of arbitrary size, as they include the key.
*/
#pragma pack(push, 4)
typedef struct {
void *value;
uint32_t key_len;
unsigned char key[0];
} art_leaf;
#pragma pack(pop)

/**
* Main struct, points to root.
Expand Down
25 changes: 12 additions & 13 deletions src/lib/art.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ static void destroy_node(art_node *n) {
case NODE48:
p.p3 = (art_node48*)n;
for (i=0;i<256;i++) {
idx = ((art_node48*)n)->keys[i];
if (!idx) continue;
idx = p.p3->keys[i];
if (!idx) continue;
destroy_node(p.p3->children[idx-1]);
}
break;
Expand Down Expand Up @@ -194,7 +194,7 @@ static art_node** find_child(art_node *n, unsigned char c) {
// __m128i cmp;
// cmp = _mm_cmpeq_epi8(_mm_set1_epi8(c),
// _mm_loadu_si128((__m128i*)p.p2->keys));

// // Use a mask to ignore children that don't exist
// mask = (1 << n->num_children) - 1;
// bitfield = _mm_movemask_epi8(cmp) & mask;
Expand Down Expand Up @@ -393,7 +393,7 @@ static art_leaf* make_leaf(const unsigned char *key, int key_len, void *value) {
if (l == NULL) {
return NULL;
}

l->value = value;
l->key_len = key_len;
memcpy(l->key, key, key_len);
Expand Down Expand Up @@ -447,7 +447,7 @@ static void add_child48(art_node48 *n, art_node **ref, unsigned char c, void *ch
static void add_child16(art_node16 *n, art_node **ref, unsigned char c, void *child) {
if (n->n.num_children < 16) {
unsigned mask = (1 << n->n.num_children) - 1;

// support non-x86 architectures
// #ifdef __i386__
// __m128i cmp;
Expand Down Expand Up @@ -478,7 +478,7 @@ static void add_child16(art_node16 *n, art_node **ref, unsigned char c, void *ch
}

// Use a mask to ignore children that don't exist
bitfield &= mask;
bitfield &= mask;
#endif
// #endif

Expand Down Expand Up @@ -979,13 +979,12 @@ int art_iter_prefix(art_tree *t, const unsigned char *key, int key_len, art_call
prefix_len = n->partial_len;
}

// If there is no match, search is terminated
if (!prefix_len) {
return 0;

// If we've matched the prefix, iterate on this node
} else if (depth + prefix_len == key_len) {
if (depth + prefix_len == key_len) {
return recursive_iter(n, cb, data);
// If there is a mismatch, search is terminated
} else if ((uint32_t)prefix_len < n->partial_len) {
return 0;
}

// if there is a full match, go deeper
Expand Down Expand Up @@ -1025,7 +1024,7 @@ void *art_substring(const art_tree *t, const unsigned char *str, int str_len, un
{
art_node **child;
art_node *n = t->root;
art_node *m;
art_node *m;
art_leaf *found = NULL;
int prefix_len, depth = 0;

Expand Down Expand Up @@ -1078,7 +1077,7 @@ void art_substring_walk(const art_tree *t, const unsigned char *str, int str_len
{
art_node **child;
art_node *n = t->root;
art_node *m;
art_node *m;
art_leaf *found = NULL;
int prefix_len, depth = 0;
int stop_search = 0;
Expand Down