Przeglądaj źródła

Speed up `_auto_encoder()` by 15% in `embedchain/helpers/json_serializable.py` (#1265)

Saurabh Misra 1 rok temu
rodzic
commit
622ed4a7c9
1 zmienionych plików z 4 dodań i 5 usunięć
  1. 4 5
      embedchain/helpers/json_serializable.py

+ 4 - 5
embedchain/helpers/json_serializable.py

@@ -97,10 +97,8 @@ class JSONSerializable:
             dict: A dictionary representation of the object.
         """
         if hasattr(obj, "__dict__"):
-            dct = obj.__dict__.copy()
-            for key, value in list(
-                dct.items()
-            ):  # We use list() to get a copy of items to avoid dictionary size change during iteration.
+            dct = {}
+            for key, value in obj.__dict__.items():
                 try:
                     # Recursive: If the value is an instance of a subclass of JSONSerializable,
                     # serialize it using the JSONSerializable serialize method.
@@ -120,8 +118,9 @@ class JSONSerializable:
                     # NOTE: Keep in mind that this logic needs to be applied to the decoder too.
                     else:
                         json.dumps(value)  # Try to serialize the value.
+                        dct[key] = value
                 except TypeError:
-                    del dct[key]  # If it fails, remove the key-value pair from the dictionary.
+                    pass  # If it fails, simply pass to skip this key-value pair of the dictionary.
 
             dct["__class__"] = obj.__class__.__name__
             return dct