Skip to content

Commit

Permalink
refactor: Speed up function _serialize_series by 234% in PR #6044 (…
Browse files Browse the repository at this point in the history
…`refactor-serialization`) (#6079)

* feat: Implement serialization functions for various data types and add a unified serialize method

* optmize conditional

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>

* fix: Update string and list truncation to include ellipsis for clarity

* ⚡️ Speed up function `_serialize_series` by 234% in PR #6044 (`refactor-serialization`)
Certainly! Here is a more optimized version of the program.



Changes made.
1. Replaced the `apply` method with dictionary comprehension. This avoids creating an intermediate Series, which can be an expensive operation.
2. Moved `_truncate_value` outside of the main function to keep the main function concise and focused.

* refactor: Remove unused `_truncate_value` function from serialization module

---------

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
  • Loading branch information
codeflash-ai[bot] and ogabrielluiz authored Feb 3, 2025
1 parent 5ef9d34 commit 4fd2b2f
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions src/backend/base/langflow/serialization/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ def _serialize_series(obj: pd.Series, max_length: int | None, max_items: int | N
"""Serialize pandas Series to a dictionary format."""
if max_items is not None and len(obj) > max_items:
obj = obj.head(max_items)
obj = obj.apply(lambda x: _truncate_value(x, max_length, max_items))
return obj.to_dict()
return {index: _truncate_value(value, max_length, max_items) for index, value in obj.items()}


def _is_numpy_type(obj: Any) -> bool:
Expand Down

0 comments on commit 4fd2b2f

Please sign in to comment.