Skip to content

Commit

Permalink
remove unnecessary check before unregister/1
Browse files Browse the repository at this point in the history
Reduce cpu cycles for `unregister/1` by removing the check to see
if it is already registered. Now it simply returns `true` if the
process is successful or raises a `badarg` error on failure,
following the expected outputs for OTP.

Signed-off-by: Winford <dwinford@pm.me>
  • Loading branch information
Winford committed Oct 16, 2022
1 parent bcb425d commit 31e5b97
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/libAtomVM/globalcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ void globalcontext_register_process(GlobalContext *glb, int atom_index, int loca
linkedlist_append(&glb->registered_processes, &registered_process->registered_processes_list_head);
}

void globalcontext_unregister_process(GlobalContext *glb, int atom_index)
bool globalcontext_unregister_process(GlobalContext *glb, int atom_index)
{
if (!glb->registered_processes) {
return;
return false;
}

struct RegisteredProcess *registered_processes = GET_LIST_ENTRY(glb->registered_processes, struct RegisteredProcess, registered_processes_list_head);
Expand All @@ -149,10 +149,12 @@ void globalcontext_unregister_process(GlobalContext *glb, int atom_index)
if (p->atom_index == atom_index) {
linkedlist_remove(&glb->registered_processes, &p->registered_processes_list_head);
free(p);
break;
return true;
}
p = GET_LIST_ENTRY(p->registered_processes_list_head.next, struct RegisteredProcess, registered_processes_list_head);
} while (p != registered_processes);

return false;
}

int globalcontext_get_registered_process(GlobalContext *glb, int atom_index)
Expand Down
10 changes: 4 additions & 6 deletions src/libAtomVM/nifs.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,14 +793,12 @@ static term nif_erlang_unregister_1(Context *ctx, int argc, term argv[])

int atom_index = term_to_atom_index(reg_name_term);

// must be a registered name.
if (UNLIKELY(globalcontext_get_registered_process(ctx->global, atom_index) == 0)) {
RAISE_ERROR(BADARG_ATOM);
bool res = globalcontext_unregister_process(ctx->global, atom_index);
if (res) {
return TRUE_ATOM;
}

globalcontext_unregister_process(ctx->global, atom_index);

return TRUE_ATOM;
RAISE_ERROR(BADARG_ATOM);
}

static term nif_erlang_whereis_1(Context *ctx, int argc, term argv[])
Expand Down

0 comments on commit 31e5b97

Please sign in to comment.