-
Notifications
You must be signed in to change notification settings - Fork 7
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 faster implementation of _IonNature.ion_hash #22
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from amazon.ion.core import IonType | ||
from amazon.ion.simple_types import IonPyNull | ||
|
||
from functools import cmp_to_key | ||
|
||
from ionhash.hasher import _bytearray_comparator, _scalar_or_null_split_parts, _serialize_null, \ | ||
_UPDATE_SCALAR_HASH_BYTES_JUMP_TABLE, _BEGIN_MARKER, _TQ, _END_MARKER, \ | ||
_BEGIN_MARKER_BYTE, _END_MARKER_BYTE, _TQ_ANNOTATED_VALUE, _escape, _TQ_SYMBOL_SID0 | ||
|
||
|
||
class _IonEventDuck: | ||
"""Looks like an IonEvent, quacks like an IonEvent... | ||
Used for sending scalar values to the existing ion_binary_writer serializers. | ||
""" | ||
def __init__(self, value, ion_type): | ||
self.value = value | ||
self.ion_type = ion_type | ||
|
||
|
||
# H(value) → h(s(value)) | ||
def hash_value(value, hfp): | ||
"""An implementation of the [Ion Hash algorithm](https://github.com/amzn/ion-hash/blob/gh-pages/docs/spec.md) | ||
for the Ion data model that doesn't instantiate any ion_readers or ion_writers. | ||
|
||
Args: | ||
value: the Ion value to hash | ||
hfp: hash function provider | ||
|
||
Returns: | ||
Ion Hash digest of the given Ion value | ||
""" | ||
hash_fn = hfp() | ||
hash_fn.update(serialize_value(value, hfp)) | ||
return hash_fn.digest() | ||
|
||
|
||
# s(value) → serialized bytes | ||
def serialize_value(value, hfp): | ||
"""Transforms an Ion value to its Ion Hash serialized representation. | ||
|
||
Args: | ||
value: the Ion value to serialize | ||
hfp: hash function provider | ||
|
||
Returns: | ||
bytes representing the given Ion value, serialized according to the Ion Hash algorithm | ||
""" | ||
if value.ion_annotations: | ||
return _s_annotated_value(value, hfp) | ||
else: | ||
return _s_value(value, hfp) | ||
|
||
|
||
# s(annotated value) → B || TQ || s(annotation1) || s(annotation2) || ... || s(annotationn) || s(value) || E | ||
def _s_annotated_value(value, hfp): | ||
return _BEGIN_MARKER + _TQ_ANNOTATED_VALUE + b''.join([_write_symbol(a) for a in value.ion_annotations]) \ | ||
+ _s_value(value, hfp) + _END_MARKER | ||
|
||
|
||
# s(struct) → B || TQ || escape(concat(sort(H(field1), H(field2), ..., H(fieldn)))) || E | ||
# s(list) or s(sexp) → B || TQ || s(value1) || s(value2) || ... || s(valuen)) || E | ||
# s(scalar) → B || TQ || escape(representation) || E | ||
def _s_value(value, hfp): | ||
ion_type = value.ion_type | ||
is_ion_null = isinstance(value, IonPyNull) | ||
if ion_type == IonType.STRUCT and not is_ion_null: | ||
field_hashes = [_h_field(field_name, field_value, hfp) for [field_name, field_value] in value.iteritems()] | ||
field_hashes.sort(key=cmp_to_key(_bytearray_comparator)) | ||
return _BEGIN_MARKER + bytes([_TQ[IonType.STRUCT]]) + _escape(b''.join(field_hashes)) + _END_MARKER | ||
elif ion_type in [IonType.LIST, IonType.SEXP] and not is_ion_null: | ||
return _BEGIN_MARKER + bytes([_TQ[ion_type]]) \ | ||
+ b''.join([bytes(serialize_value(child, hfp)) for child in value]) + _END_MARKER | ||
else: | ||
serializer = _serialize_null if is_ion_null else _UPDATE_SCALAR_HASH_BYTES_JUMP_TABLE[ion_type] | ||
scalar_bytes = serializer(_IonEventDuck(None if is_ion_null else value, ion_type)) | ||
[tq, representation] = _scalar_or_null_split_parts(ion_type, scalar_bytes) | ||
if len(representation) == 0: | ||
return bytes([_BEGIN_MARKER_BYTE, tq, _END_MARKER_BYTE]) | ||
else: | ||
return b''.join([_BEGIN_MARKER, bytes([tq]), _escape(representation), _END_MARKER]) | ||
|
||
|
||
# H(field) → h(s(fieldname) || s(fieldvalue)) | ||
def _h_field(field_name, field_value, hfp): | ||
hash_fn = hfp() | ||
hash_fn.update(_write_symbol(field_name) + serialize_value(field_value, hfp)) | ||
return hash_fn.digest() | ||
|
||
|
||
# Function for writing symbol tokens (annotations and field names) | ||
# Has simplified logic compared to regular function because we can make some assumptions about it | ||
# Namely, that this value does not have annotations, it is always type "symbol" | ||
def _write_symbol(text_or_symbol_token): | ||
text = getattr(text_or_symbol_token, 'text', text_or_symbol_token) | ||
if text is None: | ||
return bytes([_BEGIN_MARKER_BYTE, _TQ_SYMBOL_SID0, _END_MARKER_BYTE]) | ||
else: | ||
return _BEGIN_MARKER + bytes([_TQ[IonType.SYMBOL]]) \ | ||
+ _escape(bytearray(text, encoding="utf-8")) + _END_MARKER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we avoid recomputing
bytes([...])
on each call by storing the values?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 don't know. I have also heard that looking up non-local values can be expensive in Python, and if we want to compute once and store the value, then it would need to be a non-local value. I'll create a followup issue to see if it would improve performance.
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.
That's fine with me, although it does seem like a simple enough thing to try given that you already have a basic perf testing setup.