[REF] core: simplify some base methods of BaseModel

Simplify __bool__(), __repr__(), __hash__() by removing calls to
getattr/hasattr.  We can expect a performance improvement of these
highly used method (for a single record):
 - __bool__(): 145 -> 108 ns
 - __hash__(): 330 -> 289 ns

Discard support for user id in sudo(), the method now only accepts a
bool.

Part-of: odoo/odoo#83687
This commit is contained in:
Rémy Voet (ryv)
2022-02-21 10:28:15 +00:00
committed by Raphael Collet
parent ce7704e836
commit c2ab0a5c54
+5 -9
View File
@@ -5287,9 +5287,7 @@ Fields:
The returned recordset has the same prefetch object as ``self``.
"""
if not isinstance(flag, bool):
_logger.warning("deprecated use of sudo(user), use with_user(user) instead", stack_info=True)
return self.with_user(flag)
assert isinstance(flag, bool)
return self.with_env(self.env(su=flag))
def with_user(self, user):
@@ -5769,7 +5767,8 @@ Fields:
def __bool__(self):
""" Test whether ``self`` is nonempty. """
return bool(getattr(self, '_ids', True))
return True if self._ids else False # fast version of bool(self._ids)
__nonzero__ = __bool__
def __len__(self):
@@ -5915,13 +5914,10 @@ Fields:
return self.id or 0
def __repr__(self):
return "%s%s" % (self._name, getattr(self, '_ids', ""))
return f"{self._name}{self._ids}"
def __hash__(self):
if hasattr(self, '_ids'):
return hash((self._name, frozenset(self._ids)))
else:
return hash(self._name)
return hash((self._name, frozenset(self._ids)))
def __getitem__(self, key):
""" If ``key`` is an integer or a slice, return the corresponding record