pbrady / fastcache

C implementation of Python 3 lru_cache
MIT License
154 stars 19 forks source link

Can hash numpy.arrays in decorator #37

Closed breznak closed 9 years ago

breznak commented 9 years ago

Hello, I'd like to use fastcache for my code, but my function is using ndarray.

the decorator @clru_cache cannot be used for functions with numpy.array() arguments, as those can't be hashed directly. We must hash the underlying buffer:

myarr.flags.writeable = False
hash(myarr.data)

This would have some overhead but should make it more convenient to use your library for these data. Thanks

pbrady commented 9 years ago

@breznak, it could be a rather severe performance penalty to have fastcache check the type of every argument and to see if it's an ndarray. It certainly something that someone could do on a per-project basis by wrapping clru_cache. As an example, you can take a look at the python wrapper lru_cache.

breznak commented 9 years ago

That makes sense, closing. Thanks!