This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Limit the size of the HomeServerConfig
cache in trial test runs
#15646
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Limit the size of the `HomeServerConfig` cache in trial test runs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. 😄