Skip to content

Commit 49765f4

Browse files
authored
Optimize getting config item values (#17461)
* remove metaclass * optimize getting config item values * add changelog entry * apply suggestions + run pre-commit
1 parent 44b110b commit 49765f4

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

‎astropy/config/configuration.py‎

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from warnings import warn
2222

2323
from astropy.extern.configobj import configobj, validate
24-
from astropy.utils import find_current_module, silence
24+
from astropy.utils import find_current_module, isiterable, silence
2525
from astropy.utils.exceptions import AstropyDeprecationWarning, AstropyWarning
2626

2727
from .paths import get_config_dir_path
@@ -64,17 +64,7 @@ class ConfigurationChangedWarning(AstropyWarning):
6464
"""
6565

6666

67-
class _ConfigNamespaceMeta(type):
68-
def __init__(cls, name, bases, dict):
69-
if cls.__bases__[0] is object:
70-
return
71-
72-
for key, val in dict.items():
73-
if isinstance(val, ConfigItem):
74-
val.name = key
75-
76-
77-
class ConfigNamespace(metaclass=_ConfigNamespaceMeta):
67+
class ConfigNamespace:
7868
"""
7969
A namespace of configuration items. Each subpackage with
8070
configuration items should define a subclass of this class,
@@ -283,14 +273,13 @@ class _Conf(config.ConfigNamespace):
283273
def __init__(
284274
self, defaultvalue="", description=None, cfgtype=None, module=None, aliases=None
285275
):
286-
from astropy.utils import isiterable
287-
288276
if module is None:
289277
module = find_current_module(2)
290278
if module is None:
291-
msg1 = "Cannot automatically determine get_config module, "
292-
msg2 = "because it is not called from inside a valid module"
293-
raise RuntimeError(msg1 + msg2)
279+
raise RuntimeError(
280+
"Cannot automatically determine get_config module, "
281+
"because it is not called from inside a valid module"
282+
)
294283
else:
295284
module = module.__name__
296285

@@ -327,13 +316,22 @@ def __init__(
327316
else:
328317
self.aliases = aliases
329318

319+
def __set_name__(self, owner, name):
320+
self.name = name
321+
330322
def __set__(self, obj, value):
331323
return self.set(value)
332324

333325
def __get__(self, obj, objtype=None):
334326
if obj is None:
335327
return self
336-
return self()
328+
# cache value on the descriptor itself, to avoid repeated accesses
329+
# to the ConfigObj object which is much slower
330+
try:
331+
return self.value
332+
except AttributeError:
333+
val = self.value = self()
334+
return val
337335

338336
def set(self, value):
339337
"""
@@ -360,9 +358,11 @@ def set(self, value):
360358
f" {e.args[0]}"
361359
)
362360

361+
# store value on the ConfigObj instance...
363362
sec = get_config(self.module, rootname=self.rootname)
364-
365363
sec[self.name] = value
364+
# and on the descriptor
365+
self.value = value
366366

367367
@contextmanager
368368
def set_temp(self, value):
@@ -386,8 +386,8 @@ def set_temp(self, value):
386386
387387
"""
388388
initval = self()
389-
self.set(value)
390389
try:
390+
self.set(value)
391391
yield
392392
finally:
393393
self.set(initval)
@@ -402,7 +402,11 @@ def reload(self):
402402
The new value loaded from the configuration file.
403403
404404
"""
405-
self.set(self.defaultvalue)
405+
try:
406+
del self.value
407+
except AttributeError:
408+
pass
409+
406410
baseobj = get_config(self.module, True, rootname=self.rootname)
407411
secname = baseobj.name
408412

‎docs/changes/config/17461.perf.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize getting config item values.

0 commit comments

Comments
 (0)