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

Limit the size of the HomeServerConfig cache in trial test runs #15646

Merged
merged 1 commit into from
May 22, 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
1 change: 1 addition & 0 deletions changelog.d/15646.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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anyway to know how many of these actually get re-used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are actually 359 distinct configs used in tests, not 80 as I had written.
With a size of 8, the LRU cache reports CacheInfo(hits=1676, misses=380, maxsize=8, currsize=8).
With an unlimited size, the LRU cache reports CacheInfo(hits=1697, misses=359, maxsize=None, currsize=359).

The histogram of config usage looks like
1249, 57, 33, 30, 20, 20, 19, 16, 14, 13, 12, 12, 11, 11, 11, 10, 9, 8,
8, 8, 8, 7, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
... followed by a long tail of ones.

There's one main config that's used by most tests and then some others.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like 8 is a reasonable size then. 😄

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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freezing the config dict into an immutabledict was over twice as slow as JSON serialization when I tested it. And created problems with the config parsing, because all our type checks expect lists instead of tuples and dicts instead of immutabledicts.

return deepcopy_config(config_obj)


Expand Down