mirror of
https://github.com/ARM-software/devlib.git
synced 2025-02-07 05:30:44 +00:00
memoized: further fixes; hash objects whre possible
This is further to commit 6d854fd4dc59936142fff811e575cd034a308c43 Due to the way certain objects are handled, getting the frist few bytes of an object may not be enough (e.g. strings do not store their values inline). To further mitigate the issue, hash the object where possible.
This commit is contained in:
parent
f8440cf354
commit
09ec88e946
@ -33,6 +33,7 @@ import ctypes
|
|||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
from collections import Hashable
|
||||||
|
|
||||||
import wrapt
|
import wrapt
|
||||||
|
|
||||||
@ -557,19 +558,22 @@ def __get_memo_id(obj):
|
|||||||
ID string.
|
ID string.
|
||||||
"""
|
"""
|
||||||
obj_id = id(obj)
|
obj_id = id(obj)
|
||||||
obj_pyobj = ctypes.cast(obj_id, ctypes.py_object)
|
if isinstance(obj, Hashable):
|
||||||
# TODO: Note: there is still a possibility of a clash here. If Two
|
return '{}/{}'.format(obj_id, hash(obj))
|
||||||
# different objects get assigned the same ID, an are large and are
|
else:
|
||||||
# identical in the first thirty two bytes. This shouldn't be much of an
|
obj_pyobj = ctypes.cast(obj_id, ctypes.py_object)
|
||||||
# issue in the current application of memoizing Target calls, as it's very
|
# TODO: Note: there is still a possibility of a clash here. If Two
|
||||||
# unlikely that a target will get passed large params; but may cause
|
# different objects get assigned the same ID, an are large and are
|
||||||
# problems in other applications, e.g. when memoizing results of operations
|
# identical in the first thirty two bytes. This shouldn't be much of an
|
||||||
# on large arrays. I can't really think of a good way around that apart
|
# issue in the current application of memoizing Target calls, as it's very
|
||||||
# form, e.g., md5 hashing the entire raw object, which will have an
|
# unlikely that a target will get passed large params; but may cause
|
||||||
# undesirable impact on performance.
|
# problems in other applications, e.g. when memoizing results of operations
|
||||||
num_bytes = min(ctypes.sizeof(obj_pyobj), 32)
|
# on large arrays. I can't really think of a good way around that apart
|
||||||
obj_bytes = ctypes.string_at(ctypes.addressof(obj_pyobj), num_bytes)
|
# form, e.g., md5 hashing the entire raw object, which will have an
|
||||||
return '{}/{}'.format(obj_id, obj_bytes)
|
# undesirable impact on performance.
|
||||||
|
num_bytes = min(ctypes.sizeof(obj_pyobj), 32)
|
||||||
|
obj_bytes = ctypes.string_at(ctypes.addressof(obj_pyobj), num_bytes)
|
||||||
|
return '{}/{}'.format(obj_id, obj_bytes)
|
||||||
|
|
||||||
|
|
||||||
@wrapt.decorator
|
@wrapt.decorator
|
||||||
|
Loading…
x
Reference in New Issue
Block a user