Skip to content
Snippets Groups Projects
Commit b6bdf18d authored by Damien George's avatar Damien George
Browse files

tools/mpy-tool.py: Compute the hash value for str/bytes objects.

This makes it more efficient at runtime to hash str/bytes objects.
parent b4790afd
Branches
No related tags found
No related merge requests found
...@@ -283,15 +283,15 @@ class RawCode: ...@@ -283,15 +283,15 @@ class RawCode:
# generate constant objects # generate constant objects
for i, obj in enumerate(self.objs): for i, obj in enumerate(self.objs):
obj_name = 'const_obj_%s_%u' % (self.escaped_name, i) obj_name = 'const_obj_%s_%u' % (self.escaped_name, i)
if is_str_type(obj) or is_bytes_type(obj):
if is_str_type(obj): if is_str_type(obj):
obj = bytes_cons(obj, 'utf8') obj = bytes_cons(obj, 'utf8')
print('STATIC const mp_obj_str_t %s = ' obj_type = 'mp_type_str'
'{{&mp_type_str}, 0, %u, (const byte*)"%s"};' else:
% (obj_name, len(obj), ''.join(('\\x%02x' % b) for b in obj))) obj_type = 'mp_type_bytes'
elif is_bytes_type(obj): print('STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
print('STATIC const mp_obj_str_t %s = ' % (obj_name, obj_type, qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
'{{&mp_type_bytes}, 0, %u, (const byte*)"%s"};' len(obj), ''.join(('\\x%02x' % b) for b in obj)))
% (obj_name, len(obj), ''.join(('\\x%02x' % b) for b in obj)))
elif is_int_type(obj): elif is_int_type(obj):
if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE: if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
# TODO check if we can actually fit this long-int into a small-int # TODO check if we can actually fit this long-int into a small-int
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment