Skip to content

Commit 5f2d1af

Browse files
committed
👌 Avoid eager option schema generation (aiidateam#7505)
Commit 161fda9 added generic deprecated-option handling to Manager.get_option(), making normal option reads call get_option() before profile or config overrides can be returned. Since get_option() eagerly regenerated GlobalOptionsSchema.model_json_schema(), hot paths such as configure_logging repeatedly paid the full Pydantic schema generation cost.
1 parent c02d530 commit 5f2d1af

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

‎src/aiida/manage/configuration/options.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
###########################################################################
99
"""Definition of known configuration options and methods to parse and get option values."""
1010

11+
import copy
12+
from functools import lru_cache
1113
from typing import Any
1214

1315
from aiida.common.exceptions import ConfigurationError
@@ -36,7 +38,7 @@ def valid_type(self) -> Any:
3638

3739
@property
3840
def schema(self) -> dict[str, Any]:
39-
return self._schema
41+
return copy.deepcopy(self._schema)
4042

4143
@property
4244
def default(self) -> Any:
@@ -111,6 +113,14 @@ def get_option_names() -> list[str]:
111113
return [key.replace('__', '.') for key in GlobalOptionsSchema.model_fields]
112114

113115

116+
@lru_cache(maxsize=1)
117+
def _get_options_schema_properties() -> dict[str, Any]:
118+
"""Return the JSON schema properties for the global options schema."""
119+
from .config import GlobalOptionsSchema
120+
121+
return GlobalOptionsSchema.model_json_schema()['properties']
122+
123+
114124
def get_option(name: str) -> Option:
115125
"""Return option."""
116126
from .config import GlobalOptionsSchema
@@ -119,7 +129,7 @@ def get_option(name: str) -> Option:
119129
option_name = name.replace('.', '__')
120130
if option_name not in options:
121131
raise ConfigurationError(f'the option {name} does not exist')
122-
return Option(name, GlobalOptionsSchema.model_json_schema()['properties'][option_name], options[option_name])
132+
return Option(name, _get_options_schema_properties()[option_name], options[option_name])
123133

124134

125135
def resolve_deprecated_option_name(option_name: str, stacklevel: int = 4) -> str:

0 commit comments

Comments
 (0)