From 004990e53e0365e533eea3f405e354954fb0a4e0 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 18 Dec 2024 16:36:31 +0530 Subject: [PATCH] perf: Make `frappe._dict` great again (#28824) * perf: Restore dict's flat overrides Using `super()` is unnecessary cost. This class is used A LOT. Ref: https://github.com/frappe/frappe/pull/16449/ Please consider performance while adding types, it's almost always possible to achieve good typing without this. Also `frappe._dict` is almost always used as `dict[Any, Any]` or `dict[str, Any]`, type annotations are useless here. * ci: ugh wait for processes to exit --- .github/workflows/_base-migration.yml | 2 +- frappe/types/frappedict.py | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/.github/workflows/_base-migration.yml b/.github/workflows/_base-migration.yml index a0d71eb5a0..558f25efb2 100644 --- a/.github/workflows/_base-migration.yml +++ b/.github/workflows/_base-migration.yml @@ -104,7 +104,7 @@ jobs: if pgrep honcho > /dev/null; then echo "Stopping honcho process..." pgrep honcho | xargs kill - sleep 5 + sleep 10 fi echo "Setting up environment..." diff --git a/frappe/types/frappedict.py b/frappe/types/frappedict.py index 63693e0270..08c60fd376 100644 --- a/frappe/types/frappedict.py +++ b/frappe/types/frappedict.py @@ -16,19 +16,13 @@ class _dict(dict[_KT, _VT]): __slots__ = () - def __getattr__(self, k: str) -> _VT | None: - return super().get(k) # type: ignore[arg-type] + # NOTE(perf): Do NOT use super() here, it's an unnecessary function call! + # Refer: https://github.com/frappe/frappe/pull/16449 - @override - def __setattr__(self, k: str, v: _VT) -> None: - return super().__setitem__(k, v) # type: ignore[index] - - @override - def __delattr__(self, k: str): - return super().__delitem__(k) # type: ignore[arg-type] - - def __setstate__(self, m: Mapping[_KT, _VT]) -> None: - return super().update(m) + __getattr__ = dict.get + __setattr__ = dict.__setitem__ + __delattr__ = dict.__delitem__ + __setstate__ = dict.update @override def __getstate__(self) -> Self: