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

Convert hash API to use MP_UNARY_OP_HASH instead of ad-hoc function #1250

Closed
wants to merge 1 commit into from

Conversation

dpgeorge
Copy link
Member

Hashing is now done using mp_unary_op function with MP_UNARY_OP_HASH as
the operator argument. Hashing for int, str and bytes still go via
fast-path in mp_unary_op since they are the most common objects which
need to be hashed.

This lead to quite a bit of code cleanup, and should be more efficient
if anything. It saves 176 bytes code space on Thumb2, and 360 bytes on
x86.

The only loss is that the error message "unhashable type" is now the
more generic "unsupported type for hash".

To address issue #1246.

@pfalcon it turned out that I could save a significant number of bytes and at the same time (hopefully) cover all your points. Please take a critical look!

Hashing is now done using mp_unary_op function with MP_UNARY_OP_HASH as
the operator argument.  Hashing for int, str and bytes still go via
fast-path in mp_unary_op since they are the most common objects which
need to be hashed.

This lead to quite a bit of code cleanup, and should be more efficient
if anything.  It saves 176 bytes code space on Thumb2, and 360 bytes on
x86.

The only loss is that the error message "unhashable type" is now the
more generic "unsupported type for __hash__".
mp_obj_t val = mp_call_function_1(member[0], self_in);
// __hash__ must return a small int
if (op == MP_UNARY_OP_HASH) {
val = MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(val));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this should be mp_obj_get_int_truncated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.01%) to 93.47% when pulling c9ae888 on hash-unary-op into a7c02c4 on master.

@pfalcon
Copy link
Contributor

pfalcon commented May 12, 2015

Well, I imagined more localized refactor, where mp_obj_hash() stays the same for simple types and dispatches to ->unary_op for more complex ones, but if that's what allowed to get more code size savings, looks good, thanks for going for that!

@dpgeorge
Copy link
Member Author

It was natural to put small-int hash in unary op with all the other small-int unary ops there already. And then only str hashing was left as the most common one needing a fast path (and actually needed a better fast path than existing in mp_obj_hash, as per old comment in objstr.c). So mp_obj_hash became reduntant and everything worked out for the best.

@dpgeorge
Copy link
Member Author

Merged in c2a4e4e.

@stinos you can now hopefully fix your original problem with hashing of native types.

@dpgeorge dpgeorge closed this May 12, 2015
@dpgeorge dpgeorge deleted the hash-unary-op branch May 12, 2015 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants