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

Commit cc53c96

Browse files
authored
Limit the size of the HomeServerConfig cache in trial test runs (#15646)
...to try to control memory usage. `HomeServerConfig`s hold on to many Jinja2 objects, which come out to over 0.5 MiB per config. Over the course of a full test run, the cache grows to ~360 entries. Limit it to 8 entries. Part of #15622. Signed-off-by: Sean Quah <seanq@matrix.org>
1 parent a47b206 commit cc53c96

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

changelog.d/15646.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Limit the size of the `HomeServerConfig` cache in trial test runs.

tests/unittest.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
import functools
1617
import gc
1718
import hashlib
1819
import hmac
@@ -150,7 +151,11 @@ def deepcopy_config(config: _TConfig) -> _TConfig:
150151
return new_config
151152

152153

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

155160

156161
def make_homeserver_config_obj(config: Dict[str, Any]) -> RootConfig:
@@ -164,21 +169,7 @@ def make_homeserver_config_obj(config: Dict[str, Any]) -> RootConfig:
164169
but it keeps a cache of `HomeServerConfig` instances and deepcopies them as needed,
165170
to avoid validating the whole configuration every time.
166171
"""
167-
cache_key = json.dumps(config)
168-
169-
if cache_key in _make_homeserver_config_obj_cache:
170-
# Cache hit: reuse the existing instance
171-
config_obj = _make_homeserver_config_obj_cache[cache_key]
172-
else:
173-
# Cache miss; create the actual instance
174-
config_obj = HomeServerConfig()
175-
config_obj.parse_config_dict(config, "", "")
176-
177-
# Add to the cache
178-
_make_homeserver_config_obj_cache[cache_key] = config_obj
179-
180-
assert isinstance(config_obj, RootConfig)
181-
172+
config_obj = _parse_config_dict(json.dumps(config, sort_keys=True))
182173
return deepcopy_config(config_obj)
183174

184175

0 commit comments

Comments
 (0)