-
Notifications
You must be signed in to change notification settings - Fork 106
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
Add option to insert list in ets:insert, ets:lookup refactor #1405
base: main
Are you sure you want to change the base?
Add option to insert list in ets:insert, ets:lookup refactor #1405
Conversation
Signed-off-by: Tomasz Sobkiewicz <tomasz.sobkiewicz@swmansion.com>
} | ||
EtsErrorCode result = ets_table_insert(ets_table, tuple, ctx); | ||
if (result != EtsOk) { | ||
AVM_ABORT(); // Abort because operation might not be atomic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually don't do VM abort: calling AVM_ABORT() means that an unrecoverable happened, such as memory corruption, a bad internal bug and any other kind of situation that required an entire VM crash and reboot.
Are we in this specific situation?
return EtsBadEntry; | ||
} | ||
EtsErrorCode result = ets_table_insert(ets_table, tuple, ctx); | ||
if (result != EtsOk) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like an unlikely branch, so we can use UNLIKELY(result != EtsOk)
to help compiler making efficient code for the most likely branch.
result = ets_table_insert(ets_table, entry, ctx); | ||
} else if (term_is_list(entry)) { | ||
result = ets_table_insert_list(ets_table, entry, ctx); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My personal opinion:
} else {
result = EtsBadEntry;
}
Looks slightly more readable.
{ | ||
while (term_is_nonempty_list(list)) { | ||
term tuple = term_get_list_head(list); | ||
if (!term_is_tuple(tuple) && term_get_tuple_arity(tuple) < 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this line cannot work: !term_is_tuple(tuple) && term_get_tuple_arity(tuple) < 1
. I think ||
should fix this.
Tid = ets:new(test_insert_list, []), | ||
true = ets:insert(Tid, [{foo, tapas}, {batat, batat}, {patat, patat}]), | ||
[{patat, patat}] = ets:lookup(Tid, patat), | ||
[{batat, batat}] = ets:lookup(Tid, batat), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is really important to test also different error situations, so we can spot bugs such as the one at line !term_is_tuple(tuple) && term_get_tuple_arity(tuple) < 1
.
We should try to approach 100% coverage.
In addition let's also test special cases, such as:
ets:insert(Tid, [])
ets:insert(Tid, [{foo, tapas} | {patat, patat}])
|
||
return EtsOk; | ||
} | ||
|
||
EtsErrorCode ets_lookup(term ref, term key, term *ret, Context *ctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are in the middle of a refactor, it might be a good idea to rename term ref
to term name_or_ref
or something like that so we make it clear that it is one of the two.
return result; | ||
} | ||
|
||
EtsErrorCode ets_table_lookup(struct EtsTable *ets_table, term key, term *ret, Context *ctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, let's mark with static
keyword any function that is not supposed to be called outside this source file.
This function looks like one of them.
These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
Changes:
Use Cases for the Helper Functions:
The new helper functions can be utilized in the following ETS operations to reduce code duplication:
Every mentioned function will be implemented after merging of this PR.