Skip to content

Commit

Permalink
Road to 3.1.1: performance improvements. Avoid continuous hashmap loo…
Browse files Browse the repository at this point in the history
…kups and store module* and its context* inside self_t.
  • Loading branch information
FedeDP committed Nov 23, 2018
1 parent d0ea037 commit 2879553
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 143 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.3.2)

project(module VERSION 3.1.0 LANGUAGES C CXX)
project(module VERSION 3.1.1 LANGUAGES C CXX)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
Expand Down
29 changes: 11 additions & 18 deletions Lib/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ static map_ret_code hashmap_rehash(map_t *m) {
* Add a pointer to the hashmap with strdupped key if dupkey is true
*/
map_ret_code map_put(map_t *m, const char *key, void *value, const bool dupkey) {
MOD_ASSERT(m, "NULL map.", MAP_ERR);
MOD_ASSERT(key, "NULL key.", MAP_ERR);
MOD_ASSERT(value, "NULL value.", MAP_ERR);
MOD_ASSERT(m, "NULL map.", MAP_WRONG_PARAM);
MOD_ASSERT(key, "NULL key.", MAP_WRONG_PARAM);
MOD_ASSERT(value, "NULL value.", MAP_WRONG_PARAM);

/* Find a place to put our value */
return hashmap_put(m, dupkey ? mem_strdup(key) : key, value, dupkey);
Expand Down Expand Up @@ -226,14 +226,8 @@ static map_ret_code hashmap_put(map_t *m, const char *key, void *value, const bo
* Get your pointer out of the hashmap with a key
*/
void *map_get(const map_t *m, const char *key) {
if (!m) {
fprintf(stderr, "NULL map.\n");
return NULL;
}
if (!key) {
fprintf(stderr, "NULL key.\n");
return NULL;
}
MOD_ASSERT(m, "NULL map.", NULL);
MOD_ASSERT(key, "NULL key.", NULL);

/* Find data location */
int curr = hashmap_hash_int(m, key);
Expand All @@ -258,8 +252,8 @@ bool map_has_key(const map_t *m, const char *key) {
* argument and the hashmap element is the second.
*/
map_ret_code map_iterate(map_t *m, const map_cb fn, void *userptr) {
MOD_ASSERT(m, "NULL map.", MAP_ERR);
MOD_ASSERT(fn, "NULL callback.", MAP_ERR);
MOD_ASSERT(m, "NULL map.", MAP_WRONG_PARAM);
MOD_ASSERT(fn, "NULL callback.", MAP_WRONG_PARAM);

/* On empty hashmap, return immediately */
if (map_length(m) <= 0) {
Expand All @@ -281,8 +275,8 @@ map_ret_code map_iterate(map_t *m, const map_cb fn, void *userptr) {
* Remove an element with that key from the map
*/
map_ret_code map_remove(map_t *m, const char *key) {
MOD_ASSERT(m, "NULL map.", MAP_ERR);
MOD_ASSERT(key, "NULL key.", MAP_ERR);
MOD_ASSERT(m, "NULL map.", MAP_WRONG_PARAM);
MOD_ASSERT(key, "NULL key.", MAP_WRONG_PARAM);

/* Find key */
int curr = hashmap_hash_int(m, key);
Expand Down Expand Up @@ -312,7 +306,7 @@ map_ret_code map_remove(map_t *m, const char *key) {

/* Deallocate the hashmap */
map_ret_code map_free(map_t *m) {
MOD_ASSERT(m, "NULL map.", MAP_ERR);
MOD_ASSERT(m, "NULL map.", MAP_WRONG_PARAM);

for (int i = 0; i < m->table_size; i++) {
if (m->data[i].needs_free) {
Expand All @@ -326,7 +320,6 @@ map_ret_code map_free(map_t *m) {

/* Return the length of the hashmap */
int map_length(const map_t *m) {
MOD_ASSERT(m, "NULL map.", MAP_ERR);

MOD_ASSERT(m, "NULL map.", MAP_WRONG_PARAM);
return m->size;
}

0 comments on commit 2879553

Please sign in to comment.