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
26 changes: 26 additions & 0 deletions licenses/LICENSE-xxhash
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
xxHash Library
Copyright (c) 2012-2021 Yann Collet
All rights reserved.

BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 changes: 20 additions & 0 deletions native/hashmap_get.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
#define XXH_IMPLEMENTATION /* access definitions */

#include <string.h>
#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) % len;
int64_t id = -1;

if(fmap.bucket[hash] != NULL \
&& fmap.bucket[hash]->name->len == key->len \
&& memcmp(fmap.bucket[hash]->name->buf, key->buf, key->len)){
id = fmap.bucket[hash]->id;
}
liuq19 marked this conversation as resolved.
Show resolved Hide resolved
return id;
}
14 changes: 14 additions & 0 deletions native/native.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ typedef struct {
int64_t vt[MAX_RECURSE];
} StateMachine;

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 +162,7 @@ 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);

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

#endif
38 changes: 38 additions & 0 deletions native/unittest/test_hashgetset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <inttypes.h>
#include "../native.h"
#include "../hashmap_get.c"

int main() {
FieldHashMap map;
map.N = 10000;
map.bucket = NULL;

GoString key1;
GoString key2;
GoString key3;

int64_t value1;
int64_t value2;
int64_t value3;

key1.buf = "Hello";
key1.len = 5;
key2.buf = "World";
key2.len = 5;
key3.buf = "!!!!";
key3.len = 4;

field_hashmap_set(&map, &key1, 1);
liuq19 marked this conversation as resolved.
Show resolved Hide resolved
field_hashmap_set(&map, &key2, 2);
value1 = field_hashmap_get(&map, &key1);
value2 = field_hashmap_get(&map, &key2);
printf("The value1 is: %" PRId64 "\n", value1);
printf("The value2 is: %" PRId64 "\n", value2);

value3 = field_hashmap_get(&map, &key3);
printf("The value3 is: %" PRId64 "\n", value3);
field_hashmap_set(&map, &key3, 3);
value3 = field_hashmap_get(&map, &key3);
printf("The value3 is: %" PRId64 "\n", value3);
}
liuq19 marked this conversation as resolved.
Show resolved Hide resolved