From e2e61d976dab68a0deeb185e3342e4fec22526b8 Mon Sep 17 00:00:00 2001 From: Google Earth Engine Authors Date: Sat, 13 Jul 2024 23:56:55 -0700 Subject: [PATCH] Cache the result of getInfo(). PiperOrigin-RevId: 652182575 --- python/ee/computedobject.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/ee/computedobject.py b/python/ee/computedobject.py index 4cf4ec31b..44536b2c0 100644 --- a/python/ee/computedobject.py +++ b/python/ee/computedobject.py @@ -57,6 +57,9 @@ class ComputedObject(encodable.Encodable, metaclass=ComputedObjectMetaclass): # False until the client has initialized the dynamic attributes. _initialized: bool + # Caching the result of getInfo() to avoid recomputing it every time. + _info: Optional[Any] = None + @_utils.accept_opt_prefix('opt_varName') def __init__( self, @@ -104,7 +107,9 @@ def getInfo(self) -> Optional[Any]: Returns: The object can evaluate to anything. """ - return data.computeValue(self) + if self._info is None: + self._info = data.computeValue(self) + return self._info def encode(self, encoder: Optional[Callable[..., Any]]) -> Dict[str, Any]: """Encodes the object in a format compatible with Serializer."""