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

opt: the get&set way of hashmap using xxhash #454

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions native/hashmap_get.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
#define XXH_IMPLEMENTATION /* access definitions */

#include "xxhash.h"

// Get hashmap, return the matching ID, and if not found, return -1.
int64_t field_hashmap_get(FieldHashMap *fmap, const GoString* key){
XXH64_hash_t seed = 123456789;
size_t len = fmap->N;
uint64_t hash = XXH64(key->name, len, seed);

if(fmap.bucket[hash] != NULL){
liuq19 marked this conversation as resolved.
Show resolved Hide resolved
return fmap.bucket[hash]->id;
}
liuq19 marked this conversation as resolved.
Show resolved Hide resolved
}
16 changes: 16 additions & 0 deletions native/hashmap_set.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
#define XXH_IMPLEMENTATION /* access definitions */

#include "xxhash.h"

void field_hashmap_set(FieldHashMap *fmap, const GoString* key, int64_t id){
XXH64_hash_t seed = 123456789;
size_t len = fmap->N;
uint64_t hash = XXH64(key->name, len, seed);
liuq19 marked this conversation as resolved.
Show resolved Hide resolved
FieldEntry bucket;
bucket.name = key;
bucket.hash = hash;
bucket.id = id;
fmap.bucket[hash] = bucket;
return;
}
20 changes: 20 additions & 0 deletions native/native.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ typedef struct {
int64_t vt[MAX_RECURSE];
} StateMachine;

typedef struct {
const char * buf;
size_t len;
} GoString;
liuq19 marked this conversation as resolved.
Show resolved Hide resolved

typedef struct {
GoString name;
uint64_t hash;
int64_t id;
} FieldEntry;

typedef struct {
size_t N;
FieldEntry* bucket;
} FieldHashMap;

int f64toa(char *out, double val);
int i64toa(char *out, int64_t val);
int u64toa(char *out, uint64_t val);
Expand Down Expand Up @@ -151,4 +167,8 @@ long validate_utf8_fast(const GoString *src);

long skip_one_fast(const GoString *src, long *p);
long get_by_path(const GoString *src, long *p, const GoSlice *path, StateMachine* sm);

void field_hashmap_set(FieldHashMap *fmap, const GoString* key, int64_t id);
int64_t field_hashmap_get(FieldHashMap *fmap, const GoString* key);

#endif