Skip to content

Commit b6bdf18

Browse files
committed
tools/mpy-tool.py: Compute the hash value for str/bytes objects.
This makes it more efficient at runtime to hash str/bytes objects.
1 parent b4790af commit b6bdf18

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

tools/mpy-tool.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,15 @@ def freeze(self, parent_name):
283283
# generate constant objects
284284
for i, obj in enumerate(self.objs):
285285
obj_name = 'const_obj_%s_%u' % (self.escaped_name, i)
286-
if is_str_type(obj):
287-
obj = bytes_cons(obj, 'utf8')
288-
print('STATIC const mp_obj_str_t %s = '
289-
'{{&mp_type_str}, 0, %u, (const byte*)"%s"};'
290-
% (obj_name, len(obj), ''.join(('\\x%02x' % b) for b in obj)))
291-
elif is_bytes_type(obj):
292-
print('STATIC const mp_obj_str_t %s = '
293-
'{{&mp_type_bytes}, 0, %u, (const byte*)"%s"};'
294-
% (obj_name, len(obj), ''.join(('\\x%02x' % b) for b in obj)))
286+
if is_str_type(obj) or is_bytes_type(obj):
287+
if is_str_type(obj):
288+
obj = bytes_cons(obj, 'utf8')
289+
obj_type = 'mp_type_str'
290+
else:
291+
obj_type = 'mp_type_bytes'
292+
print('STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
293+
% (obj_name, obj_type, qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
294+
len(obj), ''.join(('\\x%02x' % b) for b in obj)))
295295
elif is_int_type(obj):
296296
if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
297297
# TODO check if we can actually fit this long-int into a small-int

0 commit comments

Comments
 (0)