Yelp / bravado-core

Other
109 stars 98 forks source link

Improve memoize by id if used with kwargs only #337

Closed macisamuele closed 5 years ago

macisamuele commented 5 years ago

This PR aims to improve the performances of memoize_by_id decorator while preserving the external signature.

By profiling the code I've noticed that a lot of time is spent into the generation of the cache key of @memoize_by_id decorator (specifically on the execution ofinspect.getcallargs(func, *args, **kwargs).

This PR adds a small optimisation on the make_key functionality if the function is invoked with kwargs only parameters. In such condition we don't need to use getcallargs to determine the mappings between parameter names and associated values.

In order to preserve the fact that getcallargs resolves default attributes as well the added code does manually deal with them (it's a bit ugly but by doing so we can get additional performance improvements).

Data

Raw data on: https://gist.github.com/macisamuele/89fa006dbaf5c27a85200c96deb2a459

Unmarshaling performance improvement:

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-0.1%) to 97.78% when pulling ef76678cb73e1da7d9be6016706bec48e73fade8 on macisamuele:maci-improve-memoize-by-id-if-used-with-kwargs-only into 815f95e89bac42c6efd2d28a54b955017a5b4428 on Yelp:master.