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

Reduce memory usage for scripts with many Session objects #2934

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/enhancement-enpoints-71343.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "enhancement",
"category": "endpoints",
"description": "Fix cache implementation to reduce memory consumption."
}
4 changes: 2 additions & 2 deletions botocore/endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import logging
import re
from enum import Enum
from functools import lru_cache
from string import Formatter
from typing import NamedTuple

Expand All @@ -36,6 +35,7 @@
InvalidArnException,
is_valid_ipv4_endpoint_url,
is_valid_ipv6_endpoint_url,
lru_cache_weakref,
normalize_url_path,
percent_encode,
)
Expand Down Expand Up @@ -708,7 +708,7 @@ class EndpointProvider:
def __init__(self, ruleset_data, partition_data):
self.ruleset = RuleSet(**ruleset_data, partitions=partition_data)

@lru_cache(maxsize=CACHE_SIZE)
@lru_cache_weakref(maxsize=CACHE_SIZE)
def resolve_endpoint(self, **input_parameters):
"""Match input parameters to a rule.

Expand Down
28 changes: 28 additions & 0 deletions botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,34 @@ def _cache_guard(self, *args, **kwargs):
return _cache_guard


def lru_cache_weakref(*cache_args, **cache_kwargs):
"""
Version of functools.lru_cache that stores a weak reference to ``self``.

Serves the same purpose as :py:func:`instance_cache` but uses Python's
functools implementation which offers ``max_size`` and ``typed`` properties.

lru_cache is a global cache even when used on a method. The cache's
reference to ``self`` will prevent garbace collection of the object. This
wrapper around functools.lru_cache replaces the reference to ``self`` with
a weak reference to not interfere with garbage collection.
"""

def wrapper(func):
@functools.lru_cache(*cache_args, **cache_kwargs)
def func_with_weakref(weakref_to_self, *args, **kwargs):
return func(weakref_to_self(), *args, **kwargs)

@functools.wraps(func)
def inner(self, *args, **kwargs):
return func_with_weakref(weakref.ref(self), *args, **kwargs)

inner.cache_info = func_with_weakref.cache_info
return inner

return wrapper


def switch_host_s3_accelerate(request, operation_name, **kwargs):
"""Switches the current s3 endpoint with an S3 Accelerate endpoint"""

Expand Down
39 changes: 39 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import datetime
import io
import operator
from sys import getrefcount

import pytest
from dateutil.tz import tzoffset, tzutc
Expand Down Expand Up @@ -78,6 +79,7 @@
is_valid_ipv6_endpoint_url,
is_valid_uri,
lowercase_dict,
lru_cache_weakref,
merge_dicts,
normalize_url_path,
parse_key_val_file,
Expand Down Expand Up @@ -3388,3 +3390,40 @@ def test_is_s3_accelerate_url(url, expected):
def test_get_encoding_from_headers(headers, default, expected):
charset = get_encoding_from_headers(HeadersDict(headers), default=default)
assert charset == expected


def test_lru_cache_weakref():
class ClassWithCachedMethod:
@lru_cache_weakref(maxsize=10)
def cached_fn(self, a, b):
return a + b

cls1 = ClassWithCachedMethod()
cls2 = ClassWithCachedMethod()

assert cls1.cached_fn.cache_info().currsize == 0
assert getrefcount(cls1) == 2
assert getrefcount(cls2) == 2
# "The count returned is generally one higher than you might expect, because
# it includes the (temporary) reference as an argument to getrefcount()."
# https://docs.python.org/3.8/library/sys.html#getrefcount

cls1.cached_fn(1, 1)
cls2.cached_fn(1, 1)

# The cache now has two entries, but the reference count remains the same as
# before.
assert cls1.cached_fn.cache_info().currsize == 2
assert getrefcount(cls1) == 2
assert getrefcount(cls2) == 2

# Deleting one of the objects does not interfere with the cache entries
# related to the other object.
del cls1
assert cls2.cached_fn.cache_info().currsize == 2
assert cls2.cached_fn.cache_info().hits == 0
assert cls2.cached_fn.cache_info().misses == 2
cls2.cached_fn(1, 1)
assert cls2.cached_fn.cache_info().currsize == 2
assert cls2.cached_fn.cache_info().hits == 1 # the call was a cache hit
assert cls2.cached_fn.cache_info().misses == 2