8000 Speed up config.__getattr__ with a factor 10 corresponding to 5-10% a… · holoviz/panel@eb3ea19 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb3ea19

Browse files
Speed up config.__getattr__ with a factor 10 corresponding to 5-10% app speed up. (#5851)
1 parent 32cb022 commit eb3ea19

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

panel/config.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
_PATH = os.path.abspath(os.path.dirname(__file__))
4545

46-
4746
def validate_config(config, parameter, value):
4847
"""
4948
Validates parameter setting on a hidden config parameter.
@@ -319,14 +318,14 @@ class _config(_base_config):
319318
The theme to apply to components.""")
320319

321320
# Global parameters that are shared across all sessions
322-
_globals = [
321+
_globals = {
323322
'admin_plugins', 'autoreload', 'comms', 'cookie_secret',
324323
'nthreads', 'oauth_provider', 'oauth_expiry', 'oauth_key',
325324
'oauth_secret', 'oauth_jwt_user', 'oauth_redirect_uri',
326325
'oauth_encryption_key', 'oauth_extra_params', 'npm_cdn',
327326
'layout_compatibility', 'oauth_refresh_tokens', 'oauth_guest_endpoints',
328-
'oauth_optional'
329-
]
327+
'oauth_optional', 'admin'
328+
}
330329

331330
_truthy = ['True', 'true', '1', True, 1]
332331

@@ -335,8 +334,8 @@ class _config(_base_config):
335334
def __init__(self, **params):
336335
super().__init__(**params)
337336
self._validating = False
338-
for p in self.param:
339-
if p.startswith('_') and p[1:] not in self._globals:
337+
for p in self._parameter_set:
338+
if p.startswith('_') and p[1:] not in _config._globals:
340339
setattr(self, p+'_', None)
341340
if self.log_level:
342341
panel_log_handler.setLevel(self.log_level)
@@ -371,8 +370,8 @@ def _enable_notifications(self):
371370
def set(self, **kwargs):
372371
values = [(k, v) for k, v in self.param.values().items() if k != 'name']
373372
overrides = [
374-
(k, getattr(self, k+'_')) for k in self.param
375-
if k.startswith('_') and k[1:] not in self._globals
373+
(k, getattr(self, k+'_')) for k in _config._parameter_set
374+
if k.startswith('_') and k[1:] not in _config._globals
376375
]
377376
for k, v in kwargs.items():
378377
setattr(self, k, v)
@@ -394,12 +393,12 @@ def __setattr__(self, attr, value):
394393
if not init or (attr.startswith('_') and attr.endswith('_')) or attr == '_validating':
395394
return super().__setattr__(attr, value)
396395
value = getattr(self, f'_{attr}_hook', lambda x: x)(value)
397-
if attr in self._globals or self.param._TRIGGER:
396+
if attr in _config._globals or self.param._TRIGGER:
398397
super().__setattr__(attr if attr in self.param else f'_{attr}', value)
399398
elif state.curdoc is not None:
400-
if attr in self.param:
399+
if attr in _config._parameter_set:
401400
validate_config(self, attr, value)
402-
elif f'_{attr}' in self.param:
401+
elif f'_{attr}' in _config._parameter_set:
403402
validate_config(self, f'_{attr}', value)
404403
else:
405404
raise AttributeError(f'{attr!r} is not a valid config parameter.')
@@ -409,7 +408,7 @@ def __setattr__(self, attr, value):
409408
watchers = param_watchers(self).get(attr, {}).get('value', [])
410409
for w in watchers:
411410
w.fn()
412-
elif f'_{attr}' in self.param and hasattr(self, f'_{attr}_'):
411+
elif f'_{attr}' in _config._parameter_set and hasattr(self, f'_{attr}_'):
413412
validate_config(self, f'_{attr}', value)
414413
super().__setattr__(f'_{attr}_', value)
415414
else:
@@ -431,21 +430,11 @@ def __getattribute__(self, attr):
431430
ensure that even on first access mutable parameters do not
432431
end up being modified.
433432
"""
434-
from .io.state import state
433+
if attr in ('_param__private', '_globals', '_parameter_set', '__class__', 'param'):
434+
return super().__getattribute__(attr)
435435

436-
if attr == '_param__private':
437-
return super().__getattribute__('_param__private')
436+
from .io.state import state
438437

439-
# _param__private added in Param 2
440-
try:
441-
init = super().__getattribute__('_param__private').initialized
442-
except AttributeError:
443-
init = super().__getattribute__('initialized')
444-
global_params = super().__getattribute__('_globals')
445-
if init and not attr.startswith('__'):
446-
params = super().__getattribute__('param')
447-
else:
448-
params = []
449438
session_config = super().__getattribute__('_session_config')
450439
curdoc = state.curdoc
451440
if curdoc and curdoc not in session_config:
@@ -454,11 +443,11 @@ def __getattribute__(self, attr):
454443
curdoc and attr not in session_config[curdoc]):
455444
new_obj = copy.copy(super().__getattribute__(attr))
456445
setattr(self, attr, new_obj)
457-
if attr in global_params or attr == 'theme':
446+
if attr in _config._globals or attr == 'theme':
458447
return super().__getattribute__(attr)
459448
elif curdoc and curdoc in session_config and attr in session_config[curdoc]:
460449
return session_config[curdoc][attr]
461-
elif f'_{attr}' in params and getattr(self, f'_{attr}_') is not None:
450+
elif f'_{attr}' in _config._parameter_set and getattr(self, f'_{attr}_') is not None:
462451
return super().__getattribute__(f'_{attr}_')
463452
return super().__getattribute__(attr)
464453

@@ -624,7 +613,7 @@ def theme(self):
624613
_params = _config.param.objects()
625614< 5DCE /code>
else:
626615
_params = _config.param.params()
627-
616+
_config._parameter_set = set(_params)
628617
config = _config(**{k: None if p.allow_None else getattr(_config, k)
629618
for k, p in _params.items() if k != 'name'})
630619

0 commit comments

Comments
 (0)
0