Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid copying of immutables from cache and None on retry #3207

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

frost-nzcr4
Copy link

@frost-nzcr4 frost-nzcr4 commented Apr 17, 2024

Pull Request check-list

Please make sure to review and check all of these items:

  • Do tests and lints pass with this change?
  • Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Is there an example added to the examples folder (if applicable)?
  • Was the change added to CHANGES file?

NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.

Description of change

  • The first commit is to stop copying the None value, which improves performance by 13x (it also needs to be cherry-picked to 5.0 branch):
    from timeit import timeit
    timeit("deepcopy(retry)", "from copy import deepcopy; retry = None")
    # 0.11874631093814969
    timeit("retry if retry is None else deepcopy(retry)", "from copy import deepcopy; retry = None")
    # 0.009135799948126078
  • The second commit is to stop copying immutable objects, which improves performance by 2x for None and by 6x at least for bytes and strings:
    timeit(
    """deepcopy(cache["get"]["response"])""",
    """
    from collections import OrderedDict
    from copy import deepcopy
    
    cache = OrderedDict()
    cache["get"] = {"response": None}
    """
    )
    # 0.14101774094160646
    
    timeit(
    """cache["get"]["response"] if isinstance(cache["get"]["response"], IMMUTABLES) else deepcopy(cache["get"]["response"])""",
    """
    from collections import OrderedDict
    from copy import deepcopy
    
    IMMUTABLES = (bytes, str, int, float, bool, type(None), tuple)
    cache = OrderedDict()
    cache["get"] = {"response": None}
    """
    )
    # 0.079819873906672
    
    timeit(
    """response if isinstance(response, IMMUTABLES) else deepcopy(response)""",
    """
    from collections import OrderedDict
    from copy import deepcopy
    
    IMMUTABLES = (bytes, str, int, float, bool, type(None), tuple)
    cache = OrderedDict()
    cache["get"] = {"response": None}
    response = cache["get"]["response"]
    """
    )
    # 0.05251034500543028

@dvora-h dvora-h added the maintenance Maintenance (CI, Releases, etc) label May 8, 2024
@frost-nzcr4
Copy link
Author

Fixed the linter error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Maintenance (CI, Releases, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants