4 ms·
I recently wrote a version of this that I use in my projects, some things I do differently that you may or may not care about: - from your code it seems you're
by mpeg 3y ago
I recently wrote a version of this that I use in my projects, some things I do differently that you may or may not care about:
- from your code it seems you're not sorting kwargs, I would strongly recommend sorting them so that whether you call f(a=1, b=2) or f(b=2, a=1) the cache key is the same
- I use inspect.signature to convert all args to kwargs, this way it doesn't matter how a function gets called, the cache logic is always consistent. I know this is relatively slow but it only gets called once per function (I call it outside the wrapper) and the DX benefits are nice (in this same note, you could probably move the inspect.getsource call outside your wrapper fn for a speed boost)
I also took the opposite approach to ignore_params, and made the __dict__ params that get hashed opt-in, which works well when caching instance methods
- AlecSchueler 3y agoVery insightful comment, but can I ask what DX stands for? Maybe I'm missing something obvious.
- oulipo 3y ago"Developper Experience", eg good developper tools / libs
- by_the_bay 3y agoDeveloper experience
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- williamzeng0 3y agoMaking the __dict__ opt-in makes it a lot more user-friendly at the expense of a little verbosity. That makes sense. These tips make sense, we often use named args in our function calls (not using them has caused so many bugs), but we don't really enforce the order. Copilot doesn't always get it right either. By moving inspect.getsource out of the wrapper, do you mean initializing it when the module is imported? I'm curious how that improves performance.
- mpeg 3y agoYeah I too try to avoid positional args as much as possible, huge source of bugs and time wasting especially when refactoring code Re inspect.getsource, I'm not sure if it'd be a huge performance impact, but if it's in the wrapper fn it will get called every time the function gets called, while if it's outside it will be called only when the decorator runs (eg when the module containing the function being decorated is imported). eg: https://gist.github.com/mpeg/ff1d99fde06f39916b5aaadd76b534f4 https://gist.github.com/mpeg/ff1d99fde06f39916b5aaadd76b534f... EDIT: on a quick test, over 100k function calls, with inspect.getsource inside the wrapper it runs in 2.7s on my Apple M2, and that's not even including the md5 hash, so I suspect this should dramatically improve performance for you
- williamzeng0 3y agoThis is great, thanks for the suggestion! We've just updated our docs and code with this change: https://github.com/sweepai/sweep/pull/3332 https://github.com/sweepai/sweep/pull/3332