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

[Heap Trace Standalone] perf bug causes double traversal (IDFGH-11713) #12820

Closed
chipweinberger opened this issue Dec 17, 2023 · 4 comments
Closed
Assignees
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally

Comments

@chipweinberger
Copy link
Contributor

chipweinberger commented Dec 17, 2023

General issue report

relevant issue: #11173

[v5.1] [Heap Trace Standalone] hash map uses doubly linked list, but single linked would be better (IDFGH-9846)

In response to that issue^^, we added this code:

static HEAP_IRAM_ATTR heap_trace_record_t* map_find_and_remove(void *p)
{
    size_t idx = hash_idx(p);
    heap_trace_record_t *r_cur = NULL;
    SLIST_FOREACH(r_cur, &hash_map[idx], slist_hashmap) {
        if (r_cur->address == p) {
            total_hashmap_hits++;
            SLIST_REMOVE(&hash_map[idx], r_cur, heap_trace_record_t, slist_hashmap);
            return r_cur;
        }
    }
    total_hashmap_miss++;
    return NULL;
}

however, it uses SLIST_REMOVE which causes another traversal. We should have used SLIST_REMOVE_AFTER

something like this:

static HEAP_IRAM_ATTR heap_trace_record_t* map_find_and_remove(void *p)
{
    size_t idx = hash_idx(p);
    heap_trace_record_t *r_cur = NULL;
    heap_trace_record_t *r_prev = NULL;
    SLIST_FOREACH(r_cur, &hash_map[idx], slist_hashmap) {
        if (r_cur->address == p) {
            total_hashmap_hits++;
            if (r_prev) {
               SLIST_REMOVE_AFTER(r_prev, slist_hashmap);
            } else {
                SLIST_REMOVE_HEAD(r_cur, slist_hashmap);
            }
            return r_cur;
        }
        r_prev = r_cur;
    }
    total_hashmap_miss++;
    return NULL;
}
@chipweinberger
Copy link
Contributor Author

chipweinberger commented Dec 17, 2023

@SoucheSouche

@igrr , not sure if Guillaume is still working.

@espressif-bot espressif-bot added the Status: Opened Issue is new label Dec 17, 2023
@github-actions github-actions bot changed the title [Heap Trace Standalone] perf bug causes double traversal [Heap Trace Standalone] perf bug causes double traversal (IDFGH-11713) Dec 17, 2023
@SoucheSouche
Copy link
Collaborator

@chipweinberger, I will have a look at it during the week.

@chipweinberger
Copy link
Contributor Author

thanks!

@SoucheSouche
Copy link
Collaborator

@chipweinberger, I will update map_find_and_remove as suggested. I forgot that SLIST_REMOVE does go through the list to remove element unlike TAILQ_REMOVE used in the former implementation. Thanks for noticing :)

@espressif-bot espressif-bot added Status: In Progress Work is in progress Status: Done Issue is done internally Resolution: NA Issue resolution is unavailable and removed Status: Opened Issue is new Status: In Progress Work is in progress labels Dec 18, 2023
espressif-bot pushed a commit that referenced this issue Feb 20, 2024
Remove the use of SLIST_REMOVE in map_find_and_remove to prevent the hashmap
list to be traversed twice in the function.

Closes #12820
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

3 participants