Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Limit the size of the HomeServerConfig cache in trial test runs
Browse files Browse the repository at this point in the history
...to try to control memory usage. `HomeServerConfig`s hold on to
many Jinja2 objects, which come out to over 0.5 MiB per config.

Signed-off-by: Sean Quah <seanq@matrix.org>
  • Loading branch information
Sean Quah committed May 22, 2023
1 parent 703a8f9 commit c3807cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
1 change: 1 addition & 0 deletions changelog.d/15645.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Limit the size of the `HomeServerConfig` cache in trial test runs.
23 changes: 7 additions & 16 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
import gc
import hashlib
import hmac
Expand Down Expand Up @@ -150,7 +151,11 @@ def deepcopy_config(config: _TConfig) -> _TConfig:
return new_config


_make_homeserver_config_obj_cache: Dict[str, Union[RootConfig, Config]] = {}
@functools.lru_cache(maxsize=8)
def _parse_config_dict(config: str) -> RootConfig:
config_obj = HomeServerConfig()
config_obj.parse_config_dict(json.loads(config), "", "")
return config_obj


def make_homeserver_config_obj(config: Dict[str, Any]) -> RootConfig:
Expand All @@ -164,21 +169,7 @@ def make_homeserver_config_obj(config: Dict[str, Any]) -> RootConfig:
but it keeps a cache of `HomeServerConfig` instances and deepcopies them as needed,
to avoid validating the whole configuration every time.
"""
cache_key = json.dumps(config)

if cache_key in _make_homeserver_config_obj_cache:
# Cache hit: reuse the existing instance
config_obj = _make_homeserver_config_obj_cache[cache_key]
else:
# Cache miss; create the actual instance
config_obj = HomeServerConfig()
config_obj.parse_config_dict(config, "", "")

# Add to the cache
_make_homeserver_config_obj_cache[cache_key] = config_obj

assert isinstance(config_obj, RootConfig)

config_obj = _parse_config_dict(json.dumps(config, sort_keys=True))
return deepcopy_config(config_obj)


Expand Down

0 comments on commit c3807cc

Please sign in to comment.