Skip to content

Commit 2879553

Browse files
committed
Road to 3.1.1: performance improvements. Avoid continuous hashmap lookups and store module* and its context* inside self_t.
1 parent d0ea037 commit 2879553

File tree

11 files changed

+175
-143
lines changed

11 files changed

+175
-143
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required (VERSION 3.3.2)
22

3-
project(module VERSION 3.1.0 LANGUAGES C CXX)
3+
project(module VERSION 3.1.1 LANGUAGES C CXX)
44

55
if(NOT CMAKE_BUILD_TYPE)
66
set(CMAKE_BUILD_TYPE Release)

Lib/map.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ static map_ret_code hashmap_rehash(map_t *m) {
195195
* Add a pointer to the hashmap with strdupped key if dupkey is true
196196
*/
197197
map_ret_code map_put(map_t *m, const char *key, void *value, const bool dupkey) {
198-
MOD_ASSERT(m, "NULL map.", MAP_ERR);
199-
MOD_ASSERT(key, "NULL key.", MAP_ERR);
200-
MOD_ASSERT(value, "NULL value.", MAP_ERR);
198+
MOD_ASSERT(m, "NULL map.", MAP_WRONG_PARAM);
199+
MOD_ASSERT(key, "NULL key.", MAP_WRONG_PARAM);
200+
MOD_ASSERT(value, "NULL value.", MAP_WRONG_PARAM);
201201

202202
/* Find a place to put our value */
203203
return hashmap_put(m, dupkey ? mem_strdup(key) : key, value, dupkey);
@@ -226,14 +226,8 @@ static map_ret_code hashmap_put(map_t *m, const char *key, void *value, const bo
226226
* Get your pointer out of the hashmap with a key
227227
*/
228228
void *map_get(const map_t *m, const char *key) {
229-
if (!m) {
230-
fprintf(stderr, "NULL map.\n");
231-
return NULL;
232-
}
233-
if (!key) {
234-
fprintf(stderr, "NULL key.\n");
235-
return NULL;
236-
}
229+
MOD_ASSERT(m, "NULL map.", NULL);
230+
MOD_ASSERT(key, "NULL key.", NULL);
237231

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

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

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

313307
/* Deallocate the hashmap */
314308
map_ret_code map_free(map_t *m) {
315-
MOD_ASSERT(m, "NULL map.", MAP_ERR);
309+
MOD_ASSERT(m, "NULL map.", MAP_WRONG_PARAM);
316310

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

327321
/* Return the length of the hashmap */
328322
int map_length(const map_t *m) {
329-
MOD_ASSERT(m, "NULL map.", MAP_ERR);
330-
323+
MOD_ASSERT(m, "NULL map.", MAP_WRONG_PARAM);
331324
return m->size;
332325
}

0 commit comments

Comments
 (0)